Class: ExprEvaluator
- Defined in:
- lib/json_expr/expr_evaluator.rb
Constant Summary collapse
- NUMERIC_REGEX =
/\A[-+]?[0-9]*\.?[0-9]+\Z/
Instance Attribute Summary collapse
-
#operators ⇒ Object
Returns the value of attribute operators.
-
#vars ⇒ Object
Returns the value of attribute vars.
Instance Method Summary collapse
- #boolean_convert(x) ⇒ Object
- #compare(lhs, rhs) ⇒ Object
- #evaluate(expr) ⇒ Object
- #extract_var(path) ⇒ Object
-
#initialize(operators, vars) ⇒ ExprEvaluator
constructor
A new instance of ExprEvaluator.
- #number_convert(x) ⇒ Object
- #string_convert(x) ⇒ Object
Constructor Details
#initialize(operators, vars) ⇒ ExprEvaluator
Returns a new instance of ExprEvaluator.
14 15 16 17 |
# File 'lib/json_expr/expr_evaluator.rb', line 14 def initialize(operators, vars) @operators = operators @vars = vars end |
Instance Attribute Details
#operators ⇒ Object
Returns the value of attribute operators.
10 11 12 |
# File 'lib/json_expr/expr_evaluator.rb', line 10 def operators @operators end |
#vars ⇒ Object
Returns the value of attribute vars.
11 12 13 |
# File 'lib/json_expr/expr_evaluator.rb', line 11 def vars @vars end |
Instance Method Details
#boolean_convert(x) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/json_expr/expr_evaluator.rb', line 32 def boolean_convert(x) if x.is_a?(TrueClass) || x.is_a?(FalseClass) return x elsif x.is_a?(Numeric) || !(x.to_s =~ NUMERIC_REGEX).nil? return !x.to_f.zero? elsif x.is_a?(String) return x != "false" && x != "0" && x != "" end !x.nil? end |
#compare(lhs, rhs) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/json_expr/expr_evaluator.rb', line 89 def compare(lhs, rhs) if lhs.nil? return rhs.nil? ? 0 : nil elsif rhs.nil? return nil end if lhs.is_a?(Numeric) rvalue = number_convert(rhs) return lhs.to_f.to_s.casecmp(rvalue.to_s) unless rvalue.nil? elsif lhs.is_a?(String) rvalue = string_convert(rhs) return lhs.compare_to(rvalue) unless rvalue.nil? elsif lhs.is_a?(TrueClass) || lhs.is_a?(FalseClass) rvalue = boolean_convert(rhs) return lhs.to_s.casecmp(rvalue.to_s) unless rvalue.nil? elsif lhs.class == rhs.class && lhs === rhs return 0 end nil end |
#evaluate(expr) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/json_expr/expr_evaluator.rb', line 19 def evaluate(expr) if expr.is_a? Array return @operators[:and].evaluate(self, expr) elsif expr.is_a? Hash expr.transform_keys(&:to_sym).each do |key, value| if @operators[key] return @operators[key].evaluate(self, value) end end end nil end |
#extract_var(path) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/json_expr/expr_evaluator.rb', line 66 def extract_var(path) frags = path.split("/") target = !vars.nil? ? vars : {} frags.each do |frag| list = target value = nil if target.is_a?(Array) value = list[frag.to_i] elsif target.is_a?(Hash) value = list[frag].nil? ? list[frag.to_sym] : list[frag] end unless value.nil? target = value next end return nil end target end |
#number_convert(x) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/json_expr/expr_evaluator.rb', line 44 def number_convert(x) return if x.nil? || x.to_s.empty? if x.is_a?(Numeric) || !(x.to_s =~ NUMERIC_REGEX).nil? return x.to_f elsif x.is_a?(TrueClass) || x.is_a?(FalseClass) return x ? 1.0 : 0.0 end nil end |
#string_convert(x) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/json_expr/expr_evaluator.rb', line 55 def string_convert(x) if x.is_a?(String) return x elsif x.is_a?(TrueClass) || x.is_a?(FalseClass) return x.to_s elsif x.is_a?(Numeric) || !(x.to_s =~ NUMERIC_REGEX).nil? return x == x.to_i ? x.to_i.to_s : x.to_s end nil end |