Module: Forcast::Model::RuleEngine::RuleEngine

Extended by:
ActiveSupport::Concern
Included in:
Rule
Defined in:
lib/forcast/models/rule_engine/rule_engine.rb

Constant Summary collapse

RULE_VALUES_MAP =
{
  string: :value_s,
  integer: :value_i,
  decimal: :value_d,
  boolean: :value_b,
  datetime: :value_t
}.freeze
VALID_RULE_COMPARATORS_MAP =
{
  string: %w[eql? ==],
  integer: %w[eql? > >= < <= ==],
  decimal: %w[eql? > >= < <= ==],
  boolean: %w[eql? ==],
  datetime: %w[eql? > >= < <= ==]
}.freeze

Instance Method Summary collapse

Instance Method Details

#validate_logic(object) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/forcast/models/rule_engine/rule_engine.rb', line 106

def validate_logic(object)
  attr_comparable = self.attr_comparable
  case rule_type
  when 'logic'
    value_attr_comparable = object.send(attr_comparable.to_s)
    value_type = Rule.rule_value(rule_class)
    value_comparable = send(value_type)
    puts "Attr Value: #{value_attr_comparable}"
    puts "Rule Value: #{value_comparable}"
    validation = Rule.validate_logic_rule(rule, value_attr_comparable, value_comparable)
  when 'change'
    value_attr_comparable = object.send(attr_comparable.to_s)
    last_value_attr_comparable = self.last_value_attr_comparable
    validation = !Rule.validate_logic_rule('eql?', value_attr_comparable, last_value_attr_comparable)
    update_columns(last_value_attr_comparable: value_attr_comparable)
  when 'logic_comparable'
    rules_to = Rule.where(id: rule.split(',').map(&:to_i))
    boolean_array = []
    rules_to.each do |rule_to|
      boolean_array.push(validate_logic(object,rule_to))
    end
    validation = Rule.logical_calculation(boolean_array, combine_type)
  else
    validation = false
  end
  validation
end