Class: Datamappify::Data::Mapper

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

Defined Under Namespace

Classes: Attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMapper

Returns a new instance of Mapper.



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

def initialize
  @custom_mapping         = {}
  @custom_attribute_names = []
end

Instance Attribute Details

#custom_mappingHash

Returns attribute name to source mapping as specified in Repository::MappingDSL#map_attribute.

Returns:



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

def custom_mapping
  @custom_mapping
end

#default_provider_nameString

Returns:

  • (String)


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

def default_provider_name
  @default_provider_name
end

#entity_classClass

Returns:

  • (Class)


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

def entity_class
  @entity_class
end

Instance Method Details

#all_attribute_namesArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


51
52
53
# File 'lib/datamappify/data/mapper.rb', line 51

def all_attribute_names
  entity_class.attribute_set.entries.collect(&:name)
end

#attributesSet<Attribute>

Returns:



38
39
40
# File 'lib/datamappify/data/mapper.rb', line 38

def attributes
  @attributes ||= Set.new(default_attributes + custom_attributes)
end

#classified_attributesHash<Set>

Returns attribute sets classified by the names of their data provider.

Returns:

  • (Hash<Set>)

    attribute sets classified by the names of their data provider



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

def classified_attributes
  @classified_attributes ||= Set.new(attributes).classify(&:provider_name)
end

#custom_attribute_namesArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


61
62
63
64
65
66
# File 'lib/datamappify/data/mapper.rb', line 61

def custom_attribute_names
  # make sure custom attributes are always processed
  custom_attributes

  @custom_attribute_names
end

#custom_attributesArray<Attribute> (private)

Returns:



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

def custom_attributes
  @custom_attributes ||= custom_mapping.collect do |attribute, source|
    map_attribute(attribute, source)
  end
end

#default_attribute_namesArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


56
57
58
# File 'lib/datamappify/data/mapper.rb', line 56

def default_attribute_names
  all_attribute_names - custom_attribute_names
end

#default_attributesArray<Attribute> (private)

Returns:



69
70
71
72
73
# File 'lib/datamappify/data/mapper.rb', line 69

def default_attributes
  @default_attributes ||= default_attribute_names.collect do |attribute|
    Attribute.new(attribute, default_source_for(attribute))
  end
end

#default_providerModule

Returns:

  • (Module)


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

def default_provider
  @default_provider ||= Provider.const_get(default_provider_name)
end

#default_source_classClass

Returns:

  • (Class)


33
34
35
# File 'lib/datamappify/data/mapper.rb', line 33

def default_source_class
  @default_source_class ||= default_provider.find_or_build_record_class(entity_class.name)
end

#default_source_for(attribute) ⇒ String (private)

Parameters:

  • attribute (Symbol)

    name of the attribute

Returns:

  • (String)


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

def default_source_for(attribute)
  "#{default_provider_name}::#{entity_class.name}##{attribute}"
end

#map_attribute(name, source) ⇒ Attribute (private)

Parameters:

  • name (Symbol)

    name of the attribute

  • source (String)

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

Returns:



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

def map_attribute(name, source)
  @custom_attribute_names << name

  Attribute.new(name, source)
end

#provider(provider_name) ⇒ Module

Returns:

  • (Module)


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

def provider(provider_name)
  Provider.const_get(provider_name)
end