Class: DataMaps::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/data_maps/statement.rb

Overview

A mapping statement

Since:

  • 0.0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, conditions, converter) ⇒ Statement

The statement initializer

Parameters:

  • from (String)
  • to (String)
  • conditions (Array)
  • converter (Array)

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



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

#conditionsArray (readonly)

a array of conditions

Returns:

  • (Array)

    the current value of conditions

Since:

  • 0.0.1



9
10
11
# File 'lib/data_maps/statement.rb', line 9

def conditions
  @conditions
end

#converterArray (readonly)

a array of converter

Returns:

  • (Array)

    the current value of converter

Since:

  • 0.0.1



9
10
11
# File 'lib/data_maps/statement.rb', line 9

def converter
  @converter
end

#fromString (readonly)

the source data

Returns:

  • (String)

    the current value of from

Since:

  • 0.0.1



9
10
11
# File 'lib/data_maps/statement.rb', line 9

def from
  @from
end

#toString (readonly)

the destination data

Returns:

  • (String)

    the current value of to

Since:

  • 0.0.1



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

Parameters:

  • mapping (Hash)

Returns:

Since:

  • 0.0.1



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

Parameters:

  • data (mixed)

Returns:

  • (Array)

    key and value of the result

Since:

  • 0.0.1



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

Parameters:

  • data (mixed)

Returns:

  • (mixed)

    mutated data

Since:

  • 0.0.1



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

Parameters:

  • data (mixed)

Returns:

  • (mixed)

    mutated data

Since:

  • 0.0.1



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