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 (protected)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/map_by_method.rb', line 27

def method_missing(method, *args, &block)
  if !respond_to_before_map_by_method?(method) and
      (matches = method.to_s.match(MAP_BY_METHOD_FORMAT))
    iterator, callmethod = matches[1..2]
    # map by multiple stuff
    result = callmethod.split('_and_').collect do |method|
      # self.send(iterator) { |item| item.send method }
      self.send(iterator) { |item| item.send(method, *args, &block) }
    end
    
    return result.length > 1 ? result.transpose : result.first
  else
    method_missing_before_map_by_method(method, *args, &block)
  end
end

Instance Method Details

#respond_to?(method, include_priv = false) ⇒ Boolean

Support for include_priv due to ActiveRecord’s AssociationProxy expecting it; Support degrades to normal single argument respond_to?

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/map_by_method.rb', line 9

def respond_to?(method, include_priv = false)
  respond_to = (include_priv ?
    respond_to_before_map_by_method?(method, include_priv) :
    respond_to_before_map_by_method?(method))
  unless respond_to
    matches = method.to_s.match(MAP_BY_METHOD_FORMAT)
    if (respond_to = !!(matches)) && self.size > 0
      iterator, callmethod = matches[1..2]
      respond_to = self.first.respond_to?(callmethod)
    end
  end
  respond_to
end

#respond_to_before_map_by_method?Object



5
# File 'lib/map_by_method.rb', line 5

alias_method :respond_to_before_map_by_method?, :respond_to?