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
# File 'lib/datamappify/data/mapper.rb', line 20

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:



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)


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

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


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

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

#attributesSet<Attribute>

Returns:



48
49
50
# File 'lib/datamappify/data/mapper.rb', line 48

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



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

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

#custom_attribute_namesArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


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

def custom_attribute_names
  # make sure custom attributes are always processed
  custom_attributes

  @custom_attribute_names
end

#custom_attributesArray<Attribute> (private)

Returns:



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

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

#default_attribute_namesArray<Symbol> (private)

Returns:

  • (Array<Symbol>)


66
67
68
# File 'lib/datamappify/data/mapper.rb', line 66

def default_attribute_names
  all_attribute_names - custom_attribute_names
end

#default_attributesArray<Attribute> (private)

Returns:



79
80
81
82
83
# File 'lib/datamappify/data/mapper.rb', line 79

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

#default_providerModule

Returns:

  • (Module)


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

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

#default_source_classClass

Returns:

  • (Class)


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

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)


107
108
109
# File 'lib/datamappify/data/mapper.rb', line 107

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

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

Parameters:

  • name (Symbol)

    name of the attribute

  • source (String)

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

  • options (Hash)

Returns:



95
96
97
98
99
100
101
# File 'lib/datamappify/data/mapper.rb', line 95

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

  options.merge!(:primary_source_class => default_source_class)

  Attribute.new(name, source, options)
end

#provider(provider_name) ⇒ Module

Parameters:

  • provider_name (String)

Returns:

  • (Module)


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

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