Class: JsonLogic::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/json_logic/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil) ⇒ Rule

Returns a new instance of Rule.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/json_logic/rule.rb', line 29

def initialize(name, parent = nil)
  @name = name
  @result = false
  @parent = parent
  @reasons = []
  @deep_level = 0
  return if @parent.nil?

  @deep_level = @parent.deep_level + 1
  @parent.reasons << self
end

Instance Attribute Details

#deep_levelObject

Returns the value of attribute deep_level.



27
28
29
# File 'lib/json_logic/rule.rb', line 27

def deep_level
  @deep_level
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/json_logic/rule.rb', line 27

def name
  @name
end

#parentObject

Returns the value of attribute parent.



27
28
29
# File 'lib/json_logic/rule.rb', line 27

def parent
  @parent
end

#reasonsObject

Returns the value of attribute reasons.



27
28
29
# File 'lib/json_logic/rule.rb', line 27

def reasons
  @reasons
end

#resultObject

Returns the value of attribute result.



27
28
29
# File 'lib/json_logic/rule.rb', line 27

def result
  @result
end

Instance Method Details

#add_data_point(var_name, operator, rules, data, result) ⇒ Object



41
42
43
44
45
# File 'lib/json_logic/rule.rb', line 41

def add_data_point(var_name, operator, rules, data, result)
  @result = result
  current_data = data.is_a?(Hash) ? data[var_name] : data
  @reasons << DataPoint.new(var_name, operator, rules[operator], current_data, result)
end

#reportObject



47
48
49
50
51
52
# File 'lib/json_logic/rule.rb', line 47

def report
  report = "LOGIC: '#{name}', RESULT = #{result}\n"
  report + reasons.map do |rule|
    "#{Array.new(deep_level + 1, "\t").join} #{rule.report}"
  end.join("\n")
end