Class: JPath::Parser::Formula
- Inherits:
-
Object
- Object
- JPath::Parser::Formula
- Defined in:
- lib/jpath/parser/formula.rb
Instance Attribute Summary collapse
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #boolean? ⇒ Boolean
- #first ⇒ Object
-
#initialize(type, *parts) ⇒ Formula
constructor
A new instance of Formula.
- #requires_boolean? ⇒ Boolean
- #second ⇒ Object
- #to_predicate ⇒ Object
- #to_s ⇒ Object
- #true?(*args) ⇒ Boolean
Constructor Details
#initialize(type, *parts) ⇒ Formula
Returns a new instance of Formula.
59 60 61 62 |
# File 'lib/jpath/parser/formula.rb', line 59 def initialize(type, *parts) @type = type @parts = parts.flatten end |
Instance Attribute Details
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
55 56 57 |
# File 'lib/jpath/parser/formula.rb', line 55 def parts @parts end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
57 58 59 |
# File 'lib/jpath/parser/formula.rb', line 57 def type @type end |
Instance Method Details
#boolean? ⇒ Boolean
118 119 120 |
# File 'lib/jpath/parser/formula.rb', line 118 def boolean? %w(> < = and or).include?(type) end |
#first ⇒ Object
106 107 108 |
# File 'lib/jpath/parser/formula.rb', line 106 def first parts[0] end |
#requires_boolean? ⇒ Boolean
114 115 116 |
# File 'lib/jpath/parser/formula.rb', line 114 def requires_boolean? %w(and or).include?(type) end |
#second ⇒ Object
110 111 112 |
# File 'lib/jpath/parser/formula.rb', line 110 def second parts[1] end |
#to_predicate ⇒ Object
122 123 124 |
# File 'lib/jpath/parser/formula.rb', line 122 def to_predicate Predicate.new(self) end |
#to_s ⇒ Object
126 127 128 |
# File 'lib/jpath/parser/formula.rb', line 126 def to_s "%s%s%s" % [first, type, second] end |
#true?(*args) ⇒ Boolean
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/jpath/parser/formula.rb', line 64 def true?(*args) f = first.value(*args) s = second.value(*args) if requires_boolean? unless f === true or f === false return false end unless s === true or s === false return false end else unless f.is_a?(Numeric) return false end unless s.is_a?(Numeric) return false end end case type when "+" f + s when "-" f - s when "*" f * s when "/" f / s when ">" f > s when "<" f < s when "=" f == s when "and" f && s when "or" f || s else raise "Invalid formula type: %s" % type end end |