Class: DataMaps::Condition
- Inherits:
-
Object
- Object
- DataMaps::Condition
- Defined in:
- lib/data_maps/condition.rb
Overview
A condition
Instance Attribute Summary collapse
-
#thens ⇒ Array
readonly
The current value of thens.
-
#whens ⇒ Array
readonly
The current value of whens.
Class Method Summary collapse
-
.create_from_map(mapping) ⇒ Array
Helper method to create conditions from a mapping.
Instance Method Summary collapse
-
#can_break? ⇒ Bool
Helper method to indicate if this condition can break execution.
-
#check(data) ⇒ mixed
Check all whens on data.
-
#execute(data) ⇒ mixed
Execute this condition with given data.
-
#initialize(whens, thens) ⇒ Condition
constructor
Initializer for a Condition.
-
#result(data) ⇒ mixed
Apply the thens on data.
Constructor Details
#initialize(whens, thens) ⇒ Condition
Initializer for a Condition
29 30 31 32 33 34 35 |
# 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 |
Instance Attribute Details
#thens ⇒ Array (readonly)
Returns the current value of thens.
7 8 9 |
# File 'lib/data_maps/condition.rb', line 7 def thens @thens end |
#whens ⇒ Array (readonly)
Returns the current value of whens.
7 8 9 |
# File 'lib/data_maps/condition.rb', line 7 def whens @whens end |
Class Method Details
.create_from_map(mapping) ⇒ Array
Helper method to create conditions from a mapping
14 15 16 17 18 19 20 21 22 23 |
# 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 |
Instance Method Details
#can_break? ⇒ Bool
Helper method to indicate if this condition can break execution
40 41 42 |
# File 'lib/data_maps/condition.rb', line 40 def can_break? thens.any?{ |t| t.is_a?(DataMaps::Then::Filter) } end |
#check(data) ⇒ mixed
Check all whens on data
64 65 66 |
# File 'lib/data_maps/condition.rb', line 64 def check(data) whens.all? { |w| w.execute data } end |
#execute(data) ⇒ mixed
Execute this condition with given data
48 49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ mixed
Apply the thens on data
72 73 74 75 76 77 78 |
# File 'lib/data_maps/condition.rb', line 72 def result(data) thens.each do |t| data = t.execute(data) end data end |