Marnen Laibow-Koser wrote:
I like the idea behind Detective, but if it can't cope with dynamically
defined methods, then it won't be terribly useful in practice.
method_missing trickery and dynamic metaprogramming are fairly common in
Ruby.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
I've released a new version which supports method_missing like methods.
Here's an example
Detective is now also working with method_missing …
Detective.view_source('ActiveRecord::Base#find_by_id')
Result
def method_missing(method_id, *args, &block)
method_name = method_id.to_s
if self.class.private_method_defined?(method_name)
raise NoMethodError.new("Attempt to call private method",
method_name, args)
end
# If we haven't generated any methods yet, generate them, then
# see if we've created the method we're looking for.
if !self.class.generated_methods?
self.class.define_attribute_methods
if self.class.generated_methods.include?(method_name)
return self.send(method_id, *args, &block)
end
end
if self.class.primary_key.to_s == method_name
id
elsif md = self.class.match_attribute_method?(method_name)
attribute_name, method_type = md.pre_match, md.to_s
if @attributes.include?(attribute_name)
__send__("attribute#{method_type}", attribute_name, *args,
&block)
else
super
end
elsif @attributes.include?(method_name)
read_attribute(method_name)
else
super
end
end
···
--
Posted via http://www.ruby-forum.com/\.