class Datamappify::Data::Mapper::Attribute

Represents an entity attribute and its associated data source

Attributes

key[R]

Same as name, but in symbol

@return [Symbol]

name[R]

@return [String]

options[R]

@return [Hash]

primary_source_class[R]

@return [Class]

provider_name[R]

@return [String]

source_attribute_name[R]

@return [String]

source_class_name[R]

@return [String]

value[RW]

@return [any]

Public Class Methods

new(name, options) click to toggle source

@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?() click to toggle source

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?() click to toggle source

Primary attribute is from the same data provider and the same source class

@return [Boolean]

# File lib/datamappify/data/mapper/attribute.rb, line 123
def primary_attribute?
  provider_name == primary_provider_name && primary_source_class == source_class
end
primary_key?() click to toggle source

@return [Boolean]

# File lib/datamappify/data/mapper/attribute.rb, line 100
def primary_key?
  source_attribute_name == 'id'
end
primary_provider_name() click to toggle source

@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
primary_reference_key() click to toggle source

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
reverse_mapped?() click to toggle source

@return [Boolean]

# File lib/datamappify/data/mapper/attribute.rb, line 142
def reverse_mapped?
  !!@options[:via]
end
secondary_attribute?() click to toggle source

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
source_attribute_key() click to toggle source

@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
source_class() click to toggle source

@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
source_key() click to toggle source

@example

:user_comment

@return [Symbol]

# File lib/datamappify/data/mapper/attribute.rb, line 77
def source_key
  @source_key ||= source_name.to_sym
end
source_name() click to toggle source

@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
source_table() click to toggle source

@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

parse_source(source) click to toggle source

@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