Class: Datamappify::Data::Mapper
- Inherits:
-
Object
- Object
- Datamappify::Data::Mapper
- Defined in:
- lib/datamappify/data/mapper.rb,
lib/datamappify/data/mapper/attribute.rb
Defined Under Namespace
Classes: Attribute
Instance Attribute Summary collapse
-
#custom_mapping ⇒ Hash
Attribute name to source mapping as specified in Repository::MappingDSL#map_attribute.
- #default_provider_name ⇒ String
- #default_source_class_name ⇒ String
- #entity_class ⇒ Class
Instance Method Summary collapse
- #all_attribute_names ⇒ Array<Symbol> private
- #attributes ⇒ Set<Attribute>
-
#classified_attributes ⇒ Hash<Set>
Attribute sets classified by the names of their data provider.
- #custom_attribute_names ⇒ Array<Symbol> private
- #custom_attributes ⇒ Array<Attribute> private
- #default_attribute_names ⇒ Array<Symbol> private
- #default_attributes ⇒ Array<Attribute> private
- #default_provider ⇒ Module
- #default_source_class ⇒ Class
- #default_source_for(attribute) ⇒ String private
-
#initialize ⇒ Mapper
constructor
A new instance of Mapper.
- #map_custom_attribute(name, options) ⇒ Attribute private
- #provider(provider_name) ⇒ Module
Constructor Details
#initialize ⇒ Mapper
Returns a new instance of Mapper.
20 21 22 23 24 25 |
# File 'lib/datamappify/data/mapper.rb', line 20 def initialize @custom_mapping = {} @custom_attribute_names = [] @default_provider_name = Datamappify.defaults.default_provider end |
Instance Attribute Details
#custom_mapping ⇒ Hash
Returns attribute name to source mapping as specified in Repository::MappingDSL#map_attribute.
18 19 20 |
# File 'lib/datamappify/data/mapper.rb', line 18 def custom_mapping @custom_mapping end |
#default_provider_name ⇒ String
11 12 13 |
# File 'lib/datamappify/data/mapper.rb', line 11 def default_provider_name @default_provider_name end |
#default_source_class_name ⇒ String
45 46 47 |
# File 'lib/datamappify/data/mapper.rb', line 45 def default_source_class_name @default_source_class_name ||= entity_class.name end |
#entity_class ⇒ Class
8 9 10 |
# File 'lib/datamappify/data/mapper.rb', line 8 def entity_class @entity_class end |
Instance Method Details
#all_attribute_names ⇒ Array<Symbol> (private)
63 64 65 |
# File 'lib/datamappify/data/mapper.rb', line 63 def all_attribute_names entity_class.attribute_set.entries.collect(&:name) end |
#attributes ⇒ Set<Attribute>
50 51 52 |
# File 'lib/datamappify/data/mapper.rb', line 50 def attributes @attributes ||= Set.new(default_attributes + custom_attributes) end |
#classified_attributes ⇒ Hash<Set>
Returns attribute sets classified by the names of their data provider.
56 57 58 |
# File 'lib/datamappify/data/mapper.rb', line 56 def classified_attributes @classified_attributes ||= Set.new(attributes).classify(&:provider_name) end |
#custom_attribute_names ⇒ Array<Symbol> (private)
73 74 75 76 77 78 |
# File 'lib/datamappify/data/mapper.rb', line 73 def custom_attribute_names # make sure custom attributes are always processed custom_attributes @custom_attribute_names end |
#custom_attributes ⇒ Array<Attribute> (private)
93 94 95 96 97 |
# File 'lib/datamappify/data/mapper.rb', line 93 def custom_attributes @custom_attributes ||= custom_mapping.collect do |attribute, | map_custom_attribute(attribute, ) end end |
#default_attribute_names ⇒ Array<Symbol> (private)
68 69 70 |
# File 'lib/datamappify/data/mapper.rb', line 68 def default_attribute_names all_attribute_names - custom_attribute_names end |
#default_attributes ⇒ Array<Attribute> (private)
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/datamappify/data/mapper.rb', line 81 def default_attributes @default_attributes ||= default_attribute_names.collect do |attribute| Attribute.new( attribute, :to => default_source_for(attribute), :provider => default_provider_name, :primary_source_class => default_source_class ) end end |
#default_provider ⇒ Module
28 29 30 |
# File 'lib/datamappify/data/mapper.rb', line 28 def default_provider @default_provider ||= Provider.const_get(default_provider_name) end |
#default_source_class ⇒ Class
40 41 42 |
# File 'lib/datamappify/data/mapper.rb', line 40 def default_source_class @default_source_class ||= default_provider.find_or_build_record_class(default_source_class_name) end |
#default_source_for(attribute) ⇒ String (private)
115 116 117 |
# File 'lib/datamappify/data/mapper.rb', line 115 def default_source_for(attribute) "#{default_source_class_name}##{attribute}" end |
#map_custom_attribute(name, options) ⇒ Attribute (private)
102 103 104 105 106 107 108 109 |
# File 'lib/datamappify/data/mapper.rb', line 102 def map_custom_attribute(name, ) @custom_attribute_names << name .reverse_merge!(:provider => default_provider_name) .merge!(:primary_source_class => default_source_class) Attribute.new(name, ) end |
#provider(provider_name) ⇒ Module
35 36 37 |
# File 'lib/datamappify/data/mapper.rb', line 35 def provider(provider_name) Provider.const_get(provider_name) end |