
Submitted by gmosx (Fri Apr 15 04:00:56 UTC 2005)
example:
class ListItem
include Orderable, :name => :positionend
module Orderable
def self.append_dynamic_features(base, options)
...
base.module_eval %{
attr_accessor :{options[:name]}
...
}
end
end
the method append_dynamic_features is called by the modified include method to append parametrized functionality.
An altenative name could be parametrized include.
alias_method :__include_without_options__, :include
def include(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
for mod in args
if mod.respond_to?(:append_dynamic_features)
mod.append_dynamic_features(self, options)
end
end
__include_without_options__(*args)
end
alias_method :dynamic_include, :include
end

| Comments | Current voting | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|


RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
def self.append_dynamic_features(base, options) ... base.module_eval %{ p "#{self}" ... } endWill not result in the self one would normally want.
If the implemenation is improved I am in favor.
T.
I think that should be:
p "\#{self}"
or
p "#{base}"
this way you would get your expected behaviour. IMHO, p "#{self}" gives the expected result.
George
Just pointing to the "traits" mechanism :
Traits give fine control to the including entity over what gets included from a mixin, so you get a similar kind of flexibility than with parameterized includes, but without having to write the mixin in a templatized way.
What do you think ? should I post a separate RCR for traits or is this one the place to discuss it ?
-- Damien
Fair points George. Certainly it is workable and thus useful the way you have implemented. But as an RCR, the implementation should be more integerated into Ruby. IN other words, there should be away to achieve this functionality without the string interpolation.
BTW I have a module method that I think would be better as a paramerterized module:
def key_attributes(*fields) code = "" code << "def ==(o) " << fields.map {|f| "self.#{f} == o.#{f}" }.join(" && ") << " end\n" code << "def eql?(o) " << fields.map {|f| "self.#{f}.eql?(o.#{f})" }.join(" && ") << " end\n" code << "def hash() " << fields.map {|f| "self.#{f}.hash" }.join(" ^ ") << " end\n" # puts code class_eval code fields endSo others agree that this is a good use case?
T.