class DataMaps::Condition

A condition

@since 0.0.1 @attr_reader [Array] whens @attr_reader [Array] thens

Attributes

thens[R]
whens[R]

Public Class Methods

create_from_map(mapping) click to toggle source

Helper method to create conditions from a mapping

@param [Array] mapping @return [Array] of Condition

# File lib/data_maps/condition.rb, line 14
def self.create_from_map(mapping)
  raise ArgumentError.new('Conditions mapping has to be an array') unless mapping.is_a?(Array)

  mapping.map do |condition|
    self.new(
      DataMaps::When.factory_from_map(condition[:when]),
      DataMaps::Then.factory_from_map(condition[:then])
    )
  end
end
new(whens, thens) click to toggle source

Initializer for a Condition

@param [Array] whens an array of when’s @param [Array] thens an array of then’s

# File lib/data_maps/condition.rb, line 29
def initialize(whens, thens)
  raise ArgumentError.new('Whens must be an array of DataMaps::When') unless DataMaps::When::Base.valid_collection?(whens)
  raise ArgumentError.new('Thens must be an array of DataMaps::Then') unless DataMaps::Then::Base.valid_collection?(thens)

  @whens = whens
  @thens = thens
end

Public Instance Methods

can_break?() click to toggle source

Helper method to indicate if this condition can break execution

@return [Bool]

# File lib/data_maps/condition.rb, line 40
def can_break?
  thens.any?{ |t| t.is_a?(DataMaps::Then::Filter) }
end
check(data) click to toggle source

Check all whens on data

@param [mixed] data The given data @return [mixed] data The original or modified data for the next step

# File lib/data_maps/condition.rb, line 64
def check(data)
  whens.all? { |w| w.execute data }
end
execute(data) click to toggle source

Execute this condition with given data

@param [mixed] data The given data @return [mixed] data The original or modified data for the next step

# File lib/data_maps/condition.rb, line 48
def execute(data)
  if check(data)
    if can_break?
      DataMaps::FilteredValue.new(data)
    else
      result(data)
    end
  else
    data
  end
end
result(data) click to toggle source

Apply the thens on data

@param [mixed] data The given data @return [mixed] data The original or modified data for the next step

# File lib/data_maps/condition.rb, line 72
def result(data)
  thens.each do |t|
    data = t.execute(data)
  end

  data
end