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
- #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, primary_source_class) ⇒ 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_key? ⇒ Boolean
- #primary_provider_name ⇒ String
-
#primary_reference_key ⇒ Symbol
Foreign key of the primary record, useful for joins.
- #source_attribute_key ⇒ Symbol
- #source_class ⇒ Class
- #source_key ⇒ Symbol
- #source_name ⇒ String
- #source_table ⇒ Symbol
Constructor Details
#initialize(name, source, primary_source_class) ⇒ Attribute
Returns a new instance of Attribute.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 37 def initialize(name, source, primary_source_class) @key = name @name = name.to_s @primary_source_class = primary_source_class @provider_name, @source_class_name, @source_attribute_name = parse_source(source) unless primary_attribute? || external_attribute? Record.build_association(self, primary_source_class) 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 |
#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
27 28 29 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 27 def value @value end |
Instance Method Details
#external_attribute? ⇒ Boolean
External attribute is from a different data provider than the primary data provider
107 108 109 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 107 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.
131 132 133 134 135 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 131 def parse_source(source) provider_name, source_class_and_attribute = source.split('::') [provider_name, *source_class_and_attribute.split('#')] end |
#primary_attribute? ⇒ Boolean
100 101 102 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 100 def primary_attribute? provider_name == primary_provider_name && primary_source_class == source_class end |
#primary_key? ⇒ Boolean
95 96 97 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 95 def primary_key? source_attribute_name == 'id' end |
#primary_provider_name ⇒ String
112 113 114 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 112 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
123 124 125 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 123 def primary_reference_key @primary_reference_key ||= :"#{primary_source_class.to_s.demodulize.underscore}_id" end |
#source_attribute_key ⇒ Symbol
81 82 83 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 81 def source_attribute_key @source_attribute_key ||= source_attribute_name.to_sym end |
#source_class ⇒ Class
54 55 56 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 54 def source_class @source_class ||= Record.find_or_build(provider_name, source_class_name) end |
#source_key ⇒ Symbol
72 73 74 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 72 def source_key @source_key ||= source_name.to_sym end |
#source_name ⇒ String
63 64 65 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 63 def source_name @source_name ||= source_class_name.underscore end |
#source_table ⇒ Symbol
90 91 92 |
# File 'lib/datamappify/data/mapper/attribute.rb', line 90 def source_table @source_table ||= source_class_name.pluralize.underscore.to_sym end |