Class: Datamappify::Data::Mapper::Attribute
- Inherits:
-
Object
- Object
- Datamappify::Data::Mapper::Attribute
- Defined in:
- lib/datamappify/data/mapper/attribute.rb
Overview
Represents an entity attribute and its associated data source
Instance Attribute Summary collapse
-
#key ⇒ Symbol
readonly
Same as name, but in symbol.
- #name ⇒ String readonly
- #options ⇒ Hash readonly
- #primary_source_class ⇒ Class readonly
- #provider_name ⇒ String readonly
- #source_attribute_name ⇒ String readonly
- #source_class_name ⇒ String readonly
- #value ⇒ any
Instance Method Summary collapse
-
#external_attribute? ⇒ Boolean
External attribute is from a different data provider than the primary data provider.
-
#initialize(name, source, options) ⇒ Attribute
constructor
A new instance of Attribute.
-
#parse_source(source) ⇒ Array<String>
private
An array with provider name, source class name and source attribute name.
-
#primary_attribute? ⇒ Boolean
Primary attribute is from the same data provider and the same source class.
- #primary_key? ⇒ Boolean
- #primary_provider_name ⇒ String
-
#primary_reference_key ⇒ Symbol
Foreign key of the primary record, useful for joins.
- #reverse_mapped? ⇒ Boolean
-
#secondary_attribute? ⇒ Boolean
Secondary attribute is from the same data provider but a different source class.
- #source_attribute_key ⇒ Symbol
- #source_class ⇒ Class
- #source_key ⇒ Symbol
- #source_name ⇒ String
- #source_table ⇒ Symbol
Constructor Details
#initialize(name, source, options) ⇒ Attribute
Returns a new instance of Attribute.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 40 def initialize(name, source, ) @key = name @name = name.to_s @options = @primary_source_class = [:primary_source_class] @provider_name, @source_class_name, @source_attribute_name = parse_source(source) if secondary_attribute? if reverse_mapped? Record.build_reversed_association(self, primary_source_class) else Record.build_association(self, primary_source_class) end end end |
Instance Attribute Details
#key ⇒ Symbol (readonly)
Same as name, but in symbol
9 10 11 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 9 def key @key end |
#name ⇒ String (readonly)
12 13 14 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 12 def name @name end |
#options ⇒ Hash (readonly)
27 28 29 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 27 def @options end |
#primary_source_class ⇒ Class (readonly)
24 25 26 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 24 def primary_source_class @primary_source_class end |
#provider_name ⇒ String (readonly)
15 16 17 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 15 def provider_name @provider_name end |
#source_attribute_name ⇒ String (readonly)
21 22 23 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 21 def source_attribute_name @source_attribute_name end |
#source_class_name ⇒ String (readonly)
18 19 20 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 18 def source_class_name @source_class_name end |
#value ⇒ any
30 31 32 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 30 def value @value end |
Instance Method Details
#external_attribute? ⇒ Boolean
External attribute is from a different data provider than the primary data provider
140 141 142 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 140 def external_attribute? provider_name != primary_provider_name end |
#parse_source(source) ⇒ Array<String> (private)
Returns an array with provider name, source class name and source attribute name.
153 154 155 156 157 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 153 def parse_source(source) provider_name, source_class_and_attribute = source.split('::', 2) [provider_name, *source_class_and_attribute.split('#')] end |
#primary_attribute? ⇒ Boolean
Primary attribute is from the same data provider and the same source class
126 127 128 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 126 def primary_attribute? provider_name == primary_provider_name && primary_source_class == source_class end |
#primary_key? ⇒ Boolean
103 104 105 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 103 def primary_key? source_attribute_name == 'id' end |
#primary_provider_name ⇒ String
108 109 110 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 108 def primary_provider_name @primary_provider_name ||= primary_source_class.parent.to_s.demodulize end |
#primary_reference_key ⇒ Symbol
Foreign key of the primary record, useful for joins
119 120 121 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 119 def primary_reference_key @primary_reference_key ||= :"#{primary_source_class.to_s.demodulize.underscore}_id" end |
#reverse_mapped? ⇒ Boolean
145 146 147 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 145 def reverse_mapped? !!@options[:via] end |
#secondary_attribute? ⇒ Boolean
Secondary attribute is from the same data provider but a different source class
133 134 135 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 133 def secondary_attribute? provider_name == primary_provider_name && primary_source_class != source_class end |
#source_attribute_key ⇒ Symbol
89 90 91 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 89 def source_attribute_key @source_attribute_key ||= source_attribute_name.to_sym end |
#source_class ⇒ Class
62 63 64 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 62 def source_class @source_class ||= Record.find_or_build(provider_name, source_class_name) end |
#source_key ⇒ Symbol
80 81 82 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 80 def source_key @source_key ||= source_name.to_sym end |
#source_name ⇒ String
71 72 73 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 71 def source_name @source_name ||= source_class_name.demodulize.underscore end |
#source_table ⇒ Symbol
98 99 100 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 98 def source_table @source_table ||= source_class_name.pluralize.underscore.to_sym end |