Module: MethodMap

Defined in:
lib/method_map.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/method_map.rb', line 21

def method_missing(method, *args)
  if m = match_method(method)
    begin
      return @mapped.send *(m + args)
    rescue IndexError; end
  end
  super
end

Instance Method Details

#dirty_map!(mapped = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/method_map.rb', line 3

def dirty_map!(mapped = nil)
  @mapped = mapped || self
  map_method(:changes)
  map_method(:dirty?)
  map_method(:changed?)
  map_method(:clean_up!)
  map_method(/^([\w_]+)_changed\?$/, :changed?, true)
  map_method(/^([\w_]+)_change$/, :change, true)
  map_method(/^([\w_]+)_was$/, :was, true)
  map_method(/^([\w_]+)=$/, :[]=)
  map_method(/^([\w_]+)$/, :[], true)
end

#map_method(pattern, method_or_proc = nil, args = nil) ⇒ Object



16
17
18
19
# File 'lib/method_map.rb', line 16

def map_method(pattern, method_or_proc = nil, args = nil)
  regex = pattern.is_a?(Regexp) ? pattern : Regexp.new("^#{Regexp.escape(pattern.to_s)}$")
  method_map[regex] = {:method_or_proc => (method_or_proc || pattern), :args => args}
end