Class: Noteikumi::RuleConditionValidator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/noteikumi/rule_condition_validator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This is a class used by Noteikumi::Rule#satisfies_run_condition? to create a clean room to evaluate the state conditions in and provides helpers to expose named conditions as methods for use by Noteikumi::Rule#run_when

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ RuleConditionValidator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new validator

Parameters:



12
13
14
# File 'lib/noteikumi/rule_condition_validator.rb', line 12

def initialize(rule)
  @__rule = rule
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provide method access to named based conditions

Returns:

  • (Boolean)

Raises:

  • (NoMethodError)

    for unknown conditions

See Also:

  • {__evaluate_condition}


77
78
79
80
81
82
83
# File 'lib/noteikumi/rule_condition_validator.rb', line 77

def method_missing(method, *args, &blk)
  if __known_condition?(method)
    __evaluate_condition(method, *args)
  else
    super
  end
end

Instance Method Details

#__condition(condition) ⇒ Proc?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves a named condition from the rule

Parameters:

  • condition (Symbol)

    the condition name

Returns:

  • (Proc, nil)


57
58
59
# File 'lib/noteikumi/rule_condition_validator.rb', line 57

def __condition(condition)
  @__rule.conditions[condition]
end

#__evaluate_condition(condition, *args) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluate a named condition

Parameters:

  • condition (Symbol)

    the condition name

  • args (Array<Object>)

    arguments to pass to the condition

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/noteikumi/rule_condition_validator.rb', line 66

def __evaluate_condition(condition, *args)
  result = !!__condition(condition).call(*args)
  @__rule.logger.debug("Condition %s returned %s on %s" % [condition, result.inspect, @__rule])
  result
end

#__known_condition?(condition) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if the rule has a condition by name

Parameters:

  • condition (Symbol)

    the condition name

Returns:

  • (Boolean)


49
50
51
# File 'lib/noteikumi/rule_condition_validator.rb', line 49

def __known_condition?(condition)
  @__rule.has_condition?(condition)
end

#__should_run?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the rules run condition

Returns:

  • (Boolean)


41
42
43
# File 'lib/noteikumi/rule_condition_validator.rb', line 41

def __should_run?
  instance_eval(&@__rule.run_condition)
end

#first_run?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if this is the first time the rule is being ran

Returns:

  • (Boolean)


19
20
21
# File 'lib/noteikumi/rule_condition_validator.rb', line 19

def first_run?
  @__rule.run_count == 0
end

#state_had_failures?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if the state had any past failures

Returns:

  • (Boolean)


26
27
28
# File 'lib/noteikumi/rule_condition_validator.rb', line 26

def state_had_failures?
  @__rule.state.had_failures?
end

#state_processed_by?(rule) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if a rule with a specific name acted on the state

Parameters:

  • rule (Symbol, Rule)

Returns:

  • (Boolean)


34
35
36
# File 'lib/noteikumi/rule_condition_validator.rb', line 34

def state_processed_by?(rule)
  @__rule.state.processed_by?(rule)
end