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) ⇒ 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”



32
33
34
35
36
37
# File 'lib/datamappify/data/mapper/attribute.rb', line 32

def initialize(name, source)
  @key  = name
  @name = name.to_s

  @provider_name, @source_class_name, @source_attribute_name = parse_source(source)
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

#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)


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

def value
  @value
end

Instance Method Details

#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



53
54
55
56
57
# File 'lib/datamappify/data/mapper/attribute.rb', line 53

def parse_source(source)
  provider_name, source_class_and_attribute = source.split('::')

  [provider_name, *source_class_and_attribute.split('#')]
end

#primary_key?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/datamappify/data/mapper/attribute.rb', line 45

def primary_key?
  source_attribute_name == 'id'
end

#source_classClass

Returns:

  • (Class)


40
41
42
# File 'lib/datamappify/data/mapper/attribute.rb', line 40

def source_class
  @source_class ||= Record.find_or_build(provider_name, source_class_name)
end