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.



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

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

  @default_provider_name  = Datamappify.defaults.default_provider
end

Instance Attribute Details

#custom_mappingHash

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

Returns:



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

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

#default_source_class_nameString

Returns:

  • (String)


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

def default_source_class_name
  @default_source_class_name ||= entity_class.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>)


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

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

#attributesSet<Attribute>

Returns:



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

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



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

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

#custom_attribute_namesArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


73
74
75
76
77
78
# File 'lib/datamappify/data/mapper.rb', line 73

def custom_attribute_names
  # make sure custom attributes are always processed
  custom_attributes

  @custom_attribute_names
end

#custom_attributesArray<Attribute> (private)

Returns:



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

def custom_attributes
  @custom_attributes ||= custom_mapping.collect do |attribute, options|
    map_custom_attribute(attribute, options)
  end
end

#default_attribute_namesArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


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

def default_attribute_names
  all_attribute_names - custom_attribute_names
end

#default_attributesArray<Attribute> (private)

Returns:



81
82
83
84
85
86
87
88
89
90
# File 'lib/datamappify/data/mapper.rb', line 81

def default_attributes
  @default_attributes ||= default_attribute_names.collect do |attribute|
    Attribute.new(
      attribute,
      :to                   => default_source_for(attribute),
      :provider             => default_provider_name,
      :primary_source_class => default_source_class
    )
  end
end

#default_providerModule

Returns:

  • (Module)


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

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

#default_source_classClass

Returns:

  • (Class)


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

def default_source_class
  @default_source_class ||= default_provider.find_or_build_record_class(default_source_class_name)
end

#default_source_for(attribute) ⇒ String (private)

Parameters:

  • attribute (Symbol)

    name of the attribute

Returns:

  • (String)


115
116
117
# File 'lib/datamappify/data/mapper.rb', line 115

def default_source_for(attribute)
  "#{default_source_class_name}##{attribute}"
end

#map_custom_attribute(name, options) ⇒ Attribute (private)

Parameters:

  • name (Symbol)

    name of the attribute

  • options (Hash)

Returns:



102
103
104
105
106
107
108
109
# File 'lib/datamappify/data/mapper.rb', line 102

def map_custom_attribute(name, options)
  @custom_attribute_names << name

  options.reverse_merge!(:provider => default_provider_name)
  options.merge!(:primary_source_class => default_source_class)

  Attribute.new(name, options)
end

#provider(provider_name) ⇒ Module

Parameters:

  • provider_name (String)

Returns:

  • (Module)


35
36
37
# File 'lib/datamappify/data/mapper.rb', line 35

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