Module: FlatMap::OpenMapper::AttributeMethods

Included in:
FlatMap::OpenMapper
Defined in:
lib/flat_map/open_mapper/attribute_methods.rb

Overview

This module allows mappers to return and assign values via method calls which names correspond to names of mappings defined within the mapper.

This methods are defined within anonymous module that will extend mapper on first usage of this methods.

NOTE: :to_ary method is called internally by Ruby 1.9.3 when we call something like [mapper].flatten. And we DO want default behavior for handling this missing method.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Lazily define reader and writer methods for all mappings available to the mapper, and extend self with it.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/flat_map/open_mapper/attribute_methods.rb', line 14

def method_missing(name, *args, &block)
  if name == :to_ary ||
      @attribute_methods_defined ||
      self.class.protected_instance_methods.include?(name)
    return super
  end

  mappings    = all_mappings
  valid_names = mappings.map do |mapping|
    full_name = mapping.full_name
    [full_name, "#{full_name}=".to_sym]
  end
  valid_names.flatten!

  return super unless valid_names.include?(name)

  extend attribute_methods(mappings)
  @attribute_methods_defined = true
  send(name, *args, &block)
end