Class: DataMaps::Condition

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

Overview

A condition

Since:

  • 0.0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(whens, thens) ⇒ Condition

Initializer for a Condition

Parameters:

  • whens (Array)

    an array of when’s

  • thens (Array)

    an array of then’s

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



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

#thensArray (readonly)

Returns the current value of thens.

Returns:

  • (Array)

    the current value of thens

Since:

  • 0.0.1



7
8
9
# File 'lib/data_maps/condition.rb', line 7

def thens
  @thens
end

#whensArray (readonly)

Returns the current value of whens.

Returns:

  • (Array)

    the current value of whens

Since:

  • 0.0.1



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

Parameters:

  • mapping (Array)

Returns:

  • (Array)

    of Condition

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



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

Returns:

  • (Bool)

Since:

  • 0.0.1



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

Parameters:

  • data (mixed)

    The given data

Returns:

  • (mixed)

    data The original or modified data for the next step

Since:

  • 0.0.1



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

Parameters:

  • data (mixed)

    The given data

Returns:

  • (mixed)

    data The original or modified data for the next step

Since:

  • 0.0.1



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

Parameters:

  • data (mixed)

    The given data

Returns:

  • (mixed)

    data The original or modified data for the next step

Since:

  • 0.0.1



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