Smart options (they grow up so fast)#136
Conversation
|
This is some nice syntactic sugar. I'm curious as to whether or not If we do need/want to keep it, I propose that we rename to |
|
I'm open to and excited to see other ways of achieving the same end result, but I don't see how we can get to the same outcome through inference alone. The reason is simple: we don't know what options each operation needs; some want Consider this: depending on the internal state of an object, it might only want to provide a I'm generally not wild about making names longer than they need to be (would |
|
I'm a little fuzzy on the edge cases here… What if you want to pass the object for several different types of operations? Wouldn't each operation need a different list of parameters? So maybe do something like |
|
🙀 |
|
Sorry to keep you on the edge of your seat, I've been on the go, today. 🚄 I really don't want to over-engineer this PR. Even It is true that if you want a given object to be able to be passed to multiple types of operation - something you can't even remotely do in today's CR and wasn't even on the horizon until this PR - you'll need to have After all, if we implement some kind of lookup structure eg. I already think that If you have an object with complex utility, it seems like |
|
I'd humbly suggest leaving out |
|
It's 100% opt-in - you don't have to define Could you go into a bit more detail on the edge cases that you're concerned about? I'm really excited to use this... |
|
@leastbad this looks really great! |
|
I'd like to also support a more explicit Hash return value for I also propose renaming this method to # lets keep this shorthand syntactic sugar
def to_html
"<p>neato</p>"
end
def to_dom_id
"example"
end
def to_selector
"#" + to_dom_id
end
def to_operation_payload
[:html, :dom_id]
end# but also support returning an explicit hash
def to_operation_payload
{
dom_id: "example",
selector: "#example",
html: "<p>neato</p>",
}
end |
This was inspired by conversations with @rrosen and @jaredcwhite. Rafe talked about how when he came to Ruby, he was inspired by the idea that objects should "just know what to do". And Jared's recent #135 was - at its core - about making it possible for an object to take responsibility for its DOM selector. This PR is hopefully the natural conclusion of both thoughts.
An object should be able to provide its own options
Allows objects that conform to the
to_xxxnaming convention optionally provide a value to matching options.An object can provide options and a selector
An object should be able to provide all options
If an object is the only parameter passed to an operation, and that object exposes a
to_operation_optionsmethod, and theto_operationmethod returns an array of Symbols... the operation builder will build an options hash based for each matchingto_xxxmethod that is defined.In the above case,
dom_idis added to the options but doesn't get used as theselectorbecause there would have to be a method on the class calledto_selector. This is intentional because it means a class can be passed to an operation as a selector with other options, and have itsto_dom_idmethod provide aselectorvalue... or it can be the only parameter and provide a list of options to include which may or may not includeselector. This ensures full flexibility in how the feature is used.You can also return a Hash of options from
to_operation_options: