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, options) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • name (Symbol)

    name of the attribute

  • options (Hash)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

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

#optionsHash (readonly)

Returns:

  • (Hash)


27
28
29
# File 'lib/datamappify/data/mapper/attribute.rb', line 27

def options
  @options
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)


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

Returns:

  • (Boolean)


137
138
139
# File 'lib/datamappify/data/mapper/attribute.rb', line 137

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



150
151
152
# File 'lib/datamappify/data/mapper/attribute.rb', line 150

def parse_source(source)
  source.split('#')
end

#primary_attribute?Boolean

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

Returns:

  • (Boolean)


123
124
125
# 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?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/datamappify/data/mapper/attribute.rb', line 100

def primary_key?
  source_attribute_name == 'id'
end

#primary_provider_nameString

Returns:

  • (String)


105
106
107
# 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_keySymbol

Foreign key of the primary record, useful for joins

Examples:


:user_id

Returns:

  • (Symbol)


116
117
118
# 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?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/datamappify/data/mapper/attribute.rb', line 142

def reverse_mapped?
  !!@options[:via]
end

#secondary_attribute?Boolean

Secondary attribute is from the same data provider but a different source class

Returns:

  • (Boolean)


130
131
132
# 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_keySymbol

Examples:


:title

Returns:

  • (Symbol)


86
87
88
# File 'lib/datamappify/data/mapper/attribute.rb', line 86

def source_attribute_key
  @source_attribute_key ||= source_attribute_name.to_sym
end

#source_classClass

Examples:


Namespaced::UserComment

Returns:

  • (Class)


59
60
61
# 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_keySymbol

Examples:


:user_comment

Returns:

  • (Symbol)


77
78
79
# File 'lib/datamappify/data/mapper/attribute.rb', line 77

def source_key
  @source_key ||= source_name.to_sym
end

#source_nameString

Examples:


"user_comment"

Returns:

  • (String)


68
69
70
# File 'lib/datamappify/data/mapper/attribute.rb', line 68

def source_name
  @source_name ||= source_class_name.demodulize.underscore
end

#source_tableSymbol

Examples:


:user_comments

Returns:

  • (Symbol)


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

def source_table
  @source_table ||= source_class_name.pluralize.underscore.to_sym
end