Class: DataMaps::Statement
- Inherits:
-
Object
- Object
- DataMaps::Statement
- Defined in:
- lib/data_maps/statement.rb
Overview
A mapping statement
Instance Attribute Summary collapse
-
#conditions ⇒ Array
readonly
a array of conditions.
-
#converter ⇒ Array
readonly
a array of converter.
-
#from ⇒ String
readonly
the source data.
-
#to ⇒ String
readonly
the destination data.
Class Method Summary collapse
-
.create_from_map(mapping) ⇒ Statement
Create statement from a mapping hash.
Instance Method Summary collapse
-
#execute(data) ⇒ Array
Execute the statement on the given data.
-
#execute_conditions(data) ⇒ mixed
Execute conditions.
-
#execute_converter(data) ⇒ mixed
Apply all converter to the given data.
-
#initialize(from, to, conditions, converter) ⇒ Statement
constructor
The statement initializer.
Constructor Details
#initialize(from, to, conditions, converter) ⇒ Statement
The statement initializer
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/data_maps/statement.rb', line 31 def initialize(from, to, conditions, converter) raise ArgumentError.new('Statement needs a source field') unless from.present? raise ArgumentError.new('Conditions must be an array of DataMaps::Condition') unless conditions.is_a?(Array) && conditions.all?{ |c| c.is_a?(DataMaps::Condition) } raise ArgumentError.new('Converter must be an array of DataMaps::Converter') unless DataMaps::Converter::Base.valid_collection?(converter) @from = from @to = to @conditions = conditions @converter = converter end |
Instance Attribute Details
#conditions ⇒ Array (readonly)
a array of conditions
9 10 11 |
# File 'lib/data_maps/statement.rb', line 9 def conditions @conditions end |
#converter ⇒ Array (readonly)
a array of converter
9 10 11 |
# File 'lib/data_maps/statement.rb', line 9 def converter @converter end |
#from ⇒ String (readonly)
the source data
9 10 11 |
# File 'lib/data_maps/statement.rb', line 9 def from @from end |
#to ⇒ String (readonly)
the destination data
9 10 11 |
# File 'lib/data_maps/statement.rb', line 9 def to @to end |
Class Method Details
.create_from_map(mapping) ⇒ Statement
Create statement from a mapping hash
16 17 18 19 20 21 22 23 |
# File 'lib/data_maps/statement.rb', line 16 def self.create_from_map(mapping) self.new( mapping[:from], mapping[:to], DataMaps::Condition.create_from_map(mapping[:conditions] || []), DataMaps::Converter.create_from_map(mapping[:converter] || []) ) end |
Instance Method Details
#execute(data) ⇒ Array
Execute the statement on the given data
46 47 48 49 50 51 52 53 |
# File 'lib/data_maps/statement.rb', line 46 def execute(data) data = _fetch_from_data(data) data = execute_conditions(data) data = execute_converter(data) [to, data] end |
#execute_conditions(data) ⇒ mixed
Execute conditions
59 60 61 62 63 |
# File 'lib/data_maps/statement.rb', line 59 def execute_conditions(data) conditions.reduce(data) do |data, condition| condition.execute(data) end end |
#execute_converter(data) ⇒ mixed
Apply all converter to the given data
69 70 71 72 73 |
# File 'lib/data_maps/statement.rb', line 69 def execute_converter(data) converter.reduce(data) do |data, converter| converter.execute(data) end end |