# File lib/datamappify/data/mapper/attribute.rb, line 123 def primary_attribute? provider_name == primary_provider_name && primary_source_class == source_class end
class Datamappify::Data::Mapper::Attribute
Represents an entity attribute and its associated data source
Attributes
Same as name, but in symbol
@return [Symbol]
@return [String]
@return [Hash]
@return [Class]
@return [String]
@return [String]
@return [String]
@return [any]
Public Class Methods
@param name [Symbol]
name of the attribute
@param options [Hash]
# File lib/datamappify/data/mapper/attribute.rb, line 36 def initialize(name, options) @key = name @name = name.to_s @options = options @provider_name = options[:provider].to_s @primary_source_class = options[:primary_source_class] @source_class_name, @source_attribute_name = parse_source(options[:to]) 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
Public Instance Methods
External attribute is from a different data provider than the primary data provider
@return [Boolean]
# File lib/datamappify/data/mapper/attribute.rb, line 137 def external_attribute? provider_name != primary_provider_name end
Primary attribute is from the same data provider and the same source class
@return [Boolean]
@return [Boolean]
# File lib/datamappify/data/mapper/attribute.rb, line 100 def primary_key? source_attribute_name == 'id' end
@return [String]
# File lib/datamappify/data/mapper/attribute.rb, line 105 def primary_provider_name @primary_provider_name ||= primary_source_class.parent.to_s.demodulize end
Foreign key of the primary record, useful for joins
@example
:user_id
@return [Symbol]
# File lib/datamappify/data/mapper/attribute.rb, line 116 def primary_reference_key @primary_reference_key ||= :"#{primary_source_class.to_s.demodulize.underscore}_id" end
@return [Boolean]
# File lib/datamappify/data/mapper/attribute.rb, line 142 def reverse_mapped? !!@options[:via] end
Secondary attribute is from the same data provider but a different source class
@return [Boolean]
# File lib/datamappify/data/mapper/attribute.rb, line 130 def secondary_attribute? provider_name == primary_provider_name && primary_source_class != source_class end
@example
:title
@return [Symbol]
# File lib/datamappify/data/mapper/attribute.rb, line 86 def source_attribute_key @source_attribute_key ||= source_attribute_name.to_sym end
@example
Namespaced::UserComment
@return [Class]
# File lib/datamappify/data/mapper/attribute.rb, line 59 def source_class @source_class ||= Record.find_or_build(provider_name, source_class_name) end
@example
:user_comment
@return [Symbol]
# File lib/datamappify/data/mapper/attribute.rb, line 77 def source_key @source_key ||= source_name.to_sym end
@example
"user_comment"
@return [String]
# File lib/datamappify/data/mapper/attribute.rb, line 68 def source_name @source_name ||= source_class_name.demodulize.underscore end
@example
:user_comments
@return [Symbol]
# File lib/datamappify/data/mapper/attribute.rb, line 95 def source_table @source_table ||= source_class_name.pluralize.underscore.to_sym end
Private Instance Methods
@return [Array<String>]
an array with provider name, source class name and source attribute name
# File lib/datamappify/data/mapper/attribute.rb, line 150 def parse_source(source) source.split('#') end