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
-
#attribute_names ⇒ Array<String>
Return the list of all mapped attributes.
-
#mapped_name?(mappings, name) ⇒ Boolean
Is the name given part of the attribute mappings?.
-
#method_missing(name, *args, &block) ⇒ Object
Lazily define reader and writer methods for all mappings available to the mapper, and extend
self
with it. -
#respond_to_missing?(name, include_private = false) ⇒ Boolean
Look for methods that might be dynamically defined and define them for lookup.
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 |
# 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) super else mappings = all_mappings if mapped_name?(mappings, name) define_attribute_methods(mappings) send(name, *args, &block) else super end end end |
Instance Method Details
#attribute_names ⇒ Array<String>
Return the list of all mapped attributes
64 65 66 |
# File 'lib/flat_map/open_mapper/attribute_methods.rb', line 64 def attribute_names all_mappings.map { |mapping| mapping.full_name } end |
#mapped_name?(mappings, name) ⇒ Boolean
Is the name given part of the attribute mappings?
51 52 53 54 55 56 57 58 59 |
# File 'lib/flat_map/open_mapper/attribute_methods.rb', line 51 def mapped_name?(mappings, name) valid_names = mappings.map do |mapping| full_name = mapping.full_name [full_name, "#{full_name}=".to_sym] end valid_names.flatten! valid_names.include?(name) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
Look for methods that might be dynamically defined and define them for lookup.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/flat_map/open_mapper/attribute_methods.rb', line 33 def respond_to_missing?(name, include_private = false) # Added magically by Ruby 1.9.3 if name == :to_ary || name == :empty? false else unless @attribute_methods_defined define_attribute_methods(all_mappings) end mapped_name?(all_mappings, name) end end |