Customizable Mixin

P

Phrogz

Summary:
I'm writing a module that needs a specific property/method to be
supplied by the user. How do you handle this?

Details:
For my module to be effective, it needs the user to implement either an
instance property (or method that returns a value)that 'describes' the
instance. This should _not_ be unique for each instance, so #object_id
is out.

It would be effective to simply say "Hey, if you include this module in
your class, you need to supply @foo or else it'll break". But for some
reason that seems wrong to me.

The best way I've come up with is the following. Do you have a better
suggestion?

module Foo
def self.included( klass )
klass.class_eval{
def self.foo_name=( block )
@name_block = block
end
def self.foo_name
@name_block
end
}
end

def foo_name
self.class.foo_name.call( self )
end

def same_as?( other_foo )
self.foo_name == other_foo.foo_name
end
end

class Bar
include Foo
self.foo_name = lambda{ |bar| bar.name }

attr_reader :name
def initialize( name )
@name = name
end
end

b1a = Bar.new( "Fred" )
b1b = Bar.new( "Fred" )
b2 = Bar.new( "Jim" )

p b1a == b1b #=> false
p b1a.same_as?( b1b ) #=> true
p b1a.same_as?( b2 ) #=> false
 
D

Daniel Brockman

Phrogz said:
I'm writing a module that needs a specific property/method to be
supplied by the user. How do you handle this?

The core module Enumerable needs the method `each' to be supplied by
the user, but doesn't try to get fancy about it. If you supply the
method, fine; if not, you get an error saying `each' is undefined.

Why do you need to do something more complicated than this?
 
D

Devin Mullins

Phrogz said:
It would be effective to simply say "Hey, if you include this module in
your class, you need to supply @foo or else it'll break". But for some
reason that seems wrong to me.
Why? Enumerable requires you to supply #each.

Another possibility:

module Foo
def foo_name=(foo_name)
@foo_name = foo_name
end
end

class Bar
include Foo
def initialize(name)
self.foo_name = name
end
end

Your way seems like a lot of (confusing, to me) overhead which not much
benefit, pragmatic or idealogical. But I could be missing something.

Devin
 
D

Devin Mullins

Or, for that Railsy flair:

module Foo
end

class Object
def extend_foo(name)
extend Foo
@foo_name = name
end
end

class Bar
def initialize(name)
extend_foo name
end
end

Devin
 
G

Gavin Kistner

The core module Enumerable needs the method `each' to be supplied by
the user, but doesn't try to get fancy about it. If you supply the
method, fine; if not, you get an error saying `each' is undefined.

Why? Enumerable requires you to supply #each.

Er...uhm. Good point. Thanks for the head bonk. KISS it is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top