Class: Datamappify::Data::Mapper::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/datamappify/data/mapper/attribute.rb

Overview

Represents an entity attribute and its associated data source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source, primary_source_class) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • name (Symbol)

    name of the attribute

  • source (String)

    data provider, class and attribute, e.g. “ActiveRecord::User#surname”

  • primary_source_class (Class)


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

#keySymbol (readonly)

Same as name, but in symbol

Returns:

  • (Symbol)


9
10
11
# File 'lib/datamappify/data/mapper/attribute.rb', line 9

def key
  @key
end

#nameString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/datamappify/data/mapper/attribute.rb', line 12

def name
  @name
end

#primary_source_classClass (readonly)

Returns:

  • (Class)


24
25
26
# File 'lib/datamappify/data/mapper/attribute.rb', line 24

def primary_source_class
  @primary_source_class
end

#provider_nameString (readonly)

Returns:

  • (String)


15
16
17
# File 'lib/datamappify/data/mapper/attribute.rb', line 15

def provider_name
  @provider_name
end

#source_attribute_nameString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/datamappify/data/mapper/attribute.rb', line 21

def source_attribute_name
  @source_attribute_name
end

#source_class_nameString (readonly)

Returns:

  • (String)


18
19
20
# File 'lib/datamappify/data/mapper/attribute.rb', line 18

def source_class_name
  @source_class_name
end

#valueany

Returns:

  • (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

Returns:

  • (Boolean)


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.

Returns:

  • (Array<String>)

    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

Returns:

  • (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

Returns:

  • (Boolean)


95
96
97
# File 'lib/datamappify/data/mapper/attribute.rb', line 95

def primary_key?
  source_attribute_name == 'id'
end

#primary_provider_nameString

Returns:

  • (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_keySymbol

Foreign key of the primary record, useful for joins

Examples:


:user_id

Returns:

  • (Symbol)


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_keySymbol

Examples:


:title

Returns:

  • (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_classClass

Examples:


UserComment

Returns:

  • (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_keySymbol

Examples:


:user_comment

Returns:

  • (Symbol)


72
73
74
# File 'lib/datamappify/data/mapper/attribute.rb', line 72

def source_key
  @source_key ||= source_name.to_sym
end

#source_nameString

Examples:


"user_comment"

Returns:

  • (String)


63
64
65
# File 'lib/datamappify/data/mapper/attribute.rb', line 63

def source_name
  @source_name ||= source_class_name.underscore
end

#source_tableSymbol

Examples:


:user_comments

Returns:

  • (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