Class: DataMaps::Mapping

Inherits:
Object
  • Object
show all
Includes:
Concerns::Configurable, Dsl::Mapping
Defined in:
lib/data_maps/mapping.rb

Overview

The mapping class which defines a mapping

attr_reader [Hash] mapping the compiled mapping attr_reader [Hash] mapping_hash the mapping description

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Mapping

#field

Methods included from Concerns::Configurable

#configure

Constructor Details

#initialize(mapping_hash = {}) ⇒ Mapping

Initializer for the Mapping class

Parameters:

  • mapping_hash (Hash) (defaults to: {})

Raises:

  • (ArgumentError)

    when the given mapping_hash is not a Hash

Since:

  • 0.0.1



18
19
20
21
22
23
# File 'lib/data_maps/mapping.rb', line 18

def initialize(mapping_hash = {})
  raise ArgumentError.new('The mapping_hash must be a Hash') unless mapping_hash.is_a? Hash

  @mapping = {}
  @mapping_hash = mapping_hash.with_indifferent_access
end

Instance Attribute Details

#mappingObject (readonly)

Since:

  • 0.0.1



11
12
13
# File 'lib/data_maps/mapping.rb', line 11

def mapping
  @mapping
end

#mapping_hashObject (readonly)

Since:

  • 0.0.1



11
12
13
# File 'lib/data_maps/mapping.rb', line 11

def mapping_hash
  @mapping_hash
end

Instance Method Details

#compileObject

Compile the mapping statements, this will lazily called on first use

Since:

  • 0.0.1



27
28
29
30
31
32
33
34
35
36
# File 'lib/data_maps/mapping.rb', line 27

def compile
  unless @_compiled
    # iterate over the mapping_hash and initialize mapping for each key
    mapping_hash.each do |destination, map|
      @mapping[destination] = _create_statement(destination, map)
    end

    @_compiled = true
  end
end

#each_statementEnumerator|self

Allow iterations over all statements

Parameters:

  • &block (Block)

    the block to execute for each map statement

Returns:

  • (Enumerator|self)

    return the enumerator if no block given or self

Since:

  • 0.0.1



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/data_maps/mapping.rb', line 63

def each_statement
  compile

  return enum_for(:each_statement) unless block_given? # return Enumerator

  mapping.each do |destination, statement|
    yield destination, statement
  end

  self
end

#execute(data) ⇒ Hash

Execute mapping on the given data

Parameters:

  • data (Hash)

Returns:

  • (Hash)

    the mapped data

Since:

  • 0.0.1



79
80
81
82
83
84
85
86
87
# File 'lib/data_maps/mapping.rb', line 79

def execute(data)
  result = {}
  each_statement do |destination, statement|
    key, value = statement.execute(data)
    result[key] = value unless value.is_a? DataMaps::FilteredValue
  end

  result
end

#get_statement_for(destination) ⇒ Object

Getter to get the statement for a destination_field

Parameters:

  • destination (String|Symbol)

    the field name to receive statement for

Raises:

  • (KeyError)

    when the destination_field isn’t present in map

Since:

  • 0.0.1



51
52
53
54
55
56
57
# File 'lib/data_maps/mapping.rb', line 51

def get_statement_for(destination)
  compile

  raise KeyError.new("The map has no statement for field: #{destination}") unless mapping.has_key?(destination)

  mapping[destination]
end

#valid?Boolean

Validate the mapping statements, this is a compiling without save the compiled mapping

Returns:

  • (Boolean)

Since:

  • 0.0.1



39
40
41
# File 'lib/data_maps/mapping.rb', line 39

def valid?
  true if validate rescue false
end

#validateObject

Since:

  • 0.0.1



43
44
45
# File 'lib/data_maps/mapping.rb', line 43

def validate
  mapping_hash.each &method(:_create_statement)
end