Class: Restfulie::Client::Mikyung::WhenCondition

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulie/client/mikyung/when_condition.rb

Overview

Creates a conditional execution based on a description. Its rule is an array where its first element represents a rule name and second a lambda that returns true or false Its params are extra params that might be passed to the rule Example WhenCondition.new(“when running”, [“”, lambda { |resource| resource.human.state==‘running’ }], “”)

Instance Method Summary collapse

Constructor Details

#initialize(description, rule, params) ⇒ WhenCondition

Returns a new instance of WhenCondition.



11
12
13
14
15
16
17
# File 'lib/restfulie/client/mikyung/when_condition.rb', line 11

def initialize(description, rule, params)
  @description = description
  @results = []
  @extra = []
  @rule = rule
  @params = params
end

Instance Method Details

#and(condition) ⇒ Object

adds an extra condition to this step



41
42
43
# File 'lib/restfulie/client/mikyung/when_condition.rb', line 41

def and(condition)
  @extra << condition
end

#but(condition) ⇒ Object

adds an extra condition to this step



46
47
48
# File 'lib/restfulie/client/mikyung/when_condition.rb', line 46

def but(condition)
  @extra << condition
end

#execute(resource, goal, mikyung) ⇒ Object

will execute the first attached result



20
21
22
23
24
25
# File 'lib/restfulie/client/mikyung/when_condition.rb', line 20

def execute(resource, goal, mikyung)
  @results.each do |result|
    Restfulie::Common::Logger.logger.info("will '#{result.description}'")
    return result.execute(resource, goal, mikyung)
  end
end

#results_on(result) ⇒ Object

adds an extra result to this step



51
52
53
# File 'lib/restfulie/client/mikyung/when_condition.rb', line 51

def results_on(result)
  @results << result
end

#should_run_for(resource, goal) ⇒ Object

checks whether this step should execute for a specific resource



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restfulie/client/mikyung/when_condition.rb', line 28

def should_run_for(resource, goal)
  if @rule[1].arity == 2
    rule_accepts = @rule[1].call(resource, @params)
  else
    rule_accepts = @rule[1].call(resource)
  end
  return false unless rule_accepts
  !@extra.find do |condition|
    !condition.should_run_for(resource, goal)
  end
end