class DataMaps::Condition
A condition
@since 0.0.1 @attr_reader [Array] whens @attr_reader [Array] thens
Attributes
Public Class Methods
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
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
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 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 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
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