Class: DataMaps::Mapping
- Inherits:
-
Object
- Object
- DataMaps::Mapping
- 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
Instance Attribute Summary collapse
- #mapping ⇒ Object readonly
- #mapping_hash ⇒ Object readonly
Instance Method Summary collapse
-
#compile ⇒ Object
Compile the mapping statements, this will lazily called on first use.
-
#each_statement ⇒ Enumerator|self
Allow iterations over all statements.
-
#execute(data) ⇒ Hash
Execute mapping on the given data.
-
#get_statement_for(destination) ⇒ Object
Getter to get the statement for a destination_field.
-
#initialize(mapping_hash = {}) ⇒ Mapping
constructor
Initializer for the Mapping class.
-
#valid? ⇒ Boolean
Validate the mapping statements, this is a compiling without save the compiled mapping.
- #validate ⇒ Object
Methods included from Dsl::Mapping
Methods included from Concerns::Configurable
Constructor Details
#initialize(mapping_hash = {}) ⇒ Mapping
Initializer for the Mapping class
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
#mapping ⇒ Object (readonly)
11 12 13 |
# File 'lib/data_maps/mapping.rb', line 11 def mapping @mapping end |
#mapping_hash ⇒ Object (readonly)
11 12 13 |
# File 'lib/data_maps/mapping.rb', line 11 def mapping_hash @mapping_hash end |
Instance Method Details
#compile ⇒ Object
Compile the mapping statements, this will lazily called on first use
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_statement ⇒ Enumerator|self
Allow iterations over all statements
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
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
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
39 40 41 |
# File 'lib/data_maps/mapping.rb', line 39 def valid? true if validate rescue false end |
#validate ⇒ Object
43 44 45 |
# File 'lib/data_maps/mapping.rb', line 43 def validate mapping_hash.each &method(:_create_statement) end |