Class: JsonLogic::Rule
- Inherits:
-
Object
- Object
- JsonLogic::Rule
- Defined in:
- lib/json_logic/rule.rb
Instance Attribute Summary collapse
-
#deep_level ⇒ Object
Returns the value of attribute deep_level.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#reasons ⇒ Object
Returns the value of attribute reasons.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
- #add_data_point(var_name, operator, rules, data, result) ⇒ Object
-
#initialize(name, parent = nil) ⇒ Rule
constructor
A new instance of Rule.
- #report ⇒ Object
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_level ⇒ Object
Returns the value of attribute deep_level.
27 28 29 |
# File 'lib/json_logic/rule.rb', line 27 def deep_level @deep_level end |
#name ⇒ Object
Returns the value of attribute name.
27 28 29 |
# File 'lib/json_logic/rule.rb', line 27 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
27 28 29 |
# File 'lib/json_logic/rule.rb', line 27 def parent @parent end |
#reasons ⇒ Object
Returns the value of attribute reasons.
27 28 29 |
# File 'lib/json_logic/rule.rb', line 27 def reasons @reasons end |
#result ⇒ Object
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 |
#report ⇒ Object
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 |