<snusnu>
mbj: name ideas for the MethodObject gem?
<mbj>
snusnu: meot
<CraigBuchek>
Name idea: objection
<mbj>
CraigBuchek: heh, nice one!
<mbj>
class Foo
<mbj>
include Objection.new(:bar)
<mbj>
def bar
<mbj>
:baz
<mbj>
end
<mbj>
end
<mbj>
Foo.call # => :baz
<CraigBuchek>
Still trying to wrap my head around how it works.
<mbj>
CraigBuchek: Its a very simple pattern.
<mbj>
CraigBuchek: Just imagine you have to do some complex computation. And you wanna have an inheritance tree to "adjust the computation" in specific ways.
<CraigBuchek>
Also not following how including an instance of the class works.
<mbj>
class SomeAlgo
<mbj>
def initialize(foo, bar)
<mbj>
@foo, @bar = foo, bar
<mbj>
end
<mbj>
def result
<mbj>
compute_with @foo, @bar
<mbj>
end
<mbj>
end
<mbj>
I have written this stuff VERY often.
<mbj>
Initialize an object with a single query method returing a computed value.
<mbj>
To use this thing I had to do:
<mbj>
SomeAlgo.new(:state_a, :state_b).result
<mbj>
Over the time I created a .call on SomeAlgo like
<mbj>
def self.call(*arguments)
<mbj>
new(*arguments).result
<mbj>
end
<mbj>
Than we spotted, hey lots of classes have this .call on singleton, creating an instance and calling a specific method pattern....
<mbj>
So we plan to move all this repetition into the following interface:
<mbj>
class SomeAlgo
<mbj>
include Concord.new(:foo, :bar), MethodObject.new(:result)
<mbj>
def result
<CraigBuchek>
Yeah. I think I've got the use case. Just not the implementation.
<mbj>
still_do_the_same_here
<mbj>
end
<mbj>
end
<mbj>
SomeAlgo.call(....)
<mbj>
CraigBuchek: ahh, sorry.
<mbj>
I thought both, the implementation and the idea of this class is unknown for you.
<snusnu>
CraigBuchek: a subclass of class Module is basically a module instance, that can have regular state attached like any class, and then be included
<CraigBuchek>
Not familiar with include Xyz.new
<snusnu>
CraigBuchek: so it allows you to "customize" a module
<CraigBuchek>
Yeah. I've seen that pattern a few times (in ROM, mostly) and love it. Just don't understand …. oh…
<CraigBuchek>
So the instance is also a module… Nice trick.
<CraigBuchek>
Been wanting to write up a blog entry about the pattern.
<snusnu>
yeah, module is a "regular" class, and instances of it are modules
<CraigBuchek>
Not sure I like the include Xyz.new as much as something like include Virtus::AR.model though.
<mbj>
CraigBuchek: And instance methods defined on the regular class dont get mixed into a host class.
<mbj>
class MyMod < Module
<snusnu>
yeah, with lupo i went for more explicit "factories" like Lupo.enumerable and Lupo.collection
<mbj>
def something
<mbj>
end
<mbj>
end
<mbj>
class Foo
<mbj>
include MyMod.new
<mbj>
end
<mbj>
Foo#something will NOT exist.
<snusnu>
CraigBuchek: can you explain your thoughts behind "objection"? to me, it sounds like objecting to something, i.e. not agreeing with it? (i'm no native english speaker tho)
<CraigBuchek>
LOL. No, it was just a play on the word object.
skade has joined #rom-rb
<snusnu>
heh ok
jordanyee has joined #rom-rb
<snusnu>
thinking about "procto"
<snusnu>
lol
<CraigBuchek>
That's definitely why it popped into my head. I think I also liked that it ends in "ion", sounding like injection and similar words. I kinda also like the irony of the meaning not really applying.
<snusnu>
hehe, good points
<CraigBuchek>
But I can understand that the negative literal meaning could be a problem.
<snusnu>
i actually like procto, it's a "reverse" to_proc, kinda applies, but still sounds funny and meaningless :)
<CraigBuchek>
Sounds too much like proctologist to me.
<mbj>
snusnu: callable ?
<CraigBuchek>
Which could be a little joke though.
<CraigBuchek>
Yeah, and there was the one that was basically include Virtus.model(options). And Virtus.model uses a builder to dynamically build the module. I love that.
<snusnu>
CraigBuchek, mbj: wdyt about include Procto.call(:print)
<snusnu>
include Procto.call(:print) # => needs #print
<snusnu>
include Procto.call # => needs #call
<CraigBuchek>
Hmm.
<mbj>
snusnu thats REALLY nice!
mbj has quit [Quit: leaving]
<CraigBuchek>
Should it be calls instead of call there?
<snusnu>
hm, that kinda implies many calls to be included
<CraigBuchek>
No, I was trying to imply that it calls "print".
<CraigBuchek>
Ie. that it will call print.
<snusnu>
yeah i thought so, to me that doesn't fit with the include tho
<snusnu>
i think of it: ok, i include a procto call
<CraigBuchek>
So the nice thing about Virtus.model (and I could easily be mistaking Virtus for something else here) is that "model" describes what I want the object to be. By saying include Virtus.model, I'm saying that this thing is a Virtus model. Just like when I say include Enumerable, I'm saying that this thing is enumerable.
<CraigBuchek>
I don't think there's a way to make English express that here though.
<CraigBuchek>
I did just think of something really evil though. What if you added self.method_missing to MethodObject, and let it be called with include MethodObject.whatever, and then it news up with whatever method name method_missing got. (I did say it was evil!)
<CraigBuchek>
Here's a thought (not necessrily a good one): include Callable.method(:print)