Sha256: 9d9a213ec150df92c7d900cb3934ec722653927791bd490900a7b9494e513d7e

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

# Checks whether one or more rule holds and is capable of executing results.
module Restfulie
  module Client
    module Mikyung
      # 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' }], "")
      class WhenCondition
        def initialize(description, rule, params)
          @description = description
          @results = []
          @extra = []
          @rule = rule
          @params = params
        end
      
        # will execute the first attached result
        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
      
        # checks whether this step should execute for a specific resource
        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
      
        # adds an extra condition to this step
        def and(condition)
          @extra << condition
        end
        
        # adds an extra condition to this step
        def but(condition)
          @extra << condition
        end
        
        # adds an extra result to this step
        def results_on(result)
          @results << result
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
restfulie-nosqlite-1.0.4 lib/restfulie/client/mikyung/when_condition.rb
restfulie-1.1.1 lib/restfulie/client/mikyung/when_condition.rb
restfulie-1.1.0 lib/restfulie/client/mikyung/when_condition.rb
restfulie-nosqlite-1.0.3 lib/restfulie/client/mikyung/when_condition.rb
restfulie-1.0.3 lib/restfulie/client/mikyung/when_condition.rb
restfulie-1.0.0 lib/restfulie/client/mikyung/when_condition.rb
restfulie-1.0.0.beta5 lib/restfulie/client/mikyung/when_condition.rb
restfulie-1.0.0.beta4 lib/restfulie/client/mikyung/when_condition.rb
restfulie-1.0.0.beta1 lib/restfulie/client/mikyung/when_condition.rb
restfulie-0.1.0.beta1 lib/restfulie/client/mikyung/when_condition.rb
restfulie-0.9.3 lib/restfulie/client/mikyung/when_condition.rb
restfulie-0.9.1 lib/restfulie/client/mikyung/when_condition.rb
restfulie-0.8.1 lib/restfulie/client/mikyung/when_condition.rb