I was looking through the code of attachment_fu and came across the
following block pulled from attachment_fu:60
with_options :foreign_key => ‘parent_id’ do |m|
m.has_many :thumbnails, :dependent => :destroy, :class_name =>
options[:thumbnail_class].to_s
m.belongs_to :parent, :class_name => base_class.to_s
end
I’ve never seen with_options called without being tied to a map, e.g.
map.with_options, so I opened up the with_options code to see what was
going on and with my newbie Ruby knowledge can’t seem to figure this
one out.
Here’s the object extension in ActiveSupport that gets called
def with_options(options)
yield ActiveSupport::OptionMerger.new(self, options)
end
That seems straight forward but the OptionMerger boggles me. Here’s
the full module
module ActiveSupport
class OptionMerger #:nodoc:
instance_methods.each do |method|
undef_method(method) if method !~ /^(__|instance_eval|class)/
end
def initialize(context, options)
@context, @options = context, options
end
private
def method_missing(method, *arguments, &block)
merge_argument_options! arguments
@context.send(method, *arguments, &block)
end
def merge_argument_options!(arguments)
arguments << if arguments.last.respond_to? :to_hash
@options.merge(arguments.pop)
else
@options.dup
end
end
end
end
I take it that the object is initialized and then the instance methods
are iterated over undeffing methods that don’t match the give regex.
Why? I don’t know. The instance_methods method would also just apply
to the OptionMerger class methods right? I also don’t see how the
private methods are called. Any help to further expand my ruby/
ActiveSupport knowledge is much appreciated.
Eric