Module: MapByMethod::InstanceMethods
- Defined in:
- lib/map_by_method.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/map_by_method.rb', line 11
def method_missing(method, *args, &block)
method_missing_before_map_by_method(method, *args, &block)
rescue NoMethodError
error = $!
begin
if (match = method.to_s.match(self.class.map_by_method_regex))
iterator, callmethod = match[1], match[3]
callmethods = callmethod.split('_and_')
result = callmethods.collect do |callmethod|
self.send(iterator) {|item| item.send callmethod}
end
return callmethods.length == 1 ?
result.first :
result.transpose
end
method_name = method.to_s
@@support_singularize ||= method_name.methods.include? "singularize"
return self.map {|item| item.send method_name.singularize.to_sym} if @@support_singularize
rescue NoMethodError
nil
end
raise error
end
|
Instance Method Details
#method_missing_before_map_by_method ⇒ Object
9
|
# File 'lib/map_by_method.rb', line 9
alias_method :method_missing_before_map_by_method, :method_missing
|
#respond_to?(method) ⇒ Boolean
36
37
38
39
|
# File 'lib/map_by_method.rb', line 36
def respond_to?(method)
return true if respond_to_before_map_by_method?(method)
method.to_s.match(self.class.map_by_method_regex)
end
|
#respond_to_before_map_by_method? ⇒ Object
35
|
# File 'lib/map_by_method.rb', line 35
alias_method :"respond_to_before_map_by_method?", :"respond_to?"
|