Class: JPath::Parser::Formula

Inherits:
Object
  • Object
show all
Defined in:
lib/jpath/parser/formula.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#partsObject (readonly)

Returns the value of attribute parts.



55
56
57
# File 'lib/jpath/parser/formula.rb', line 55

def parts
  @parts
end

#typeObject (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

Returns:

  • (Boolean)


118
119
120
# File 'lib/jpath/parser/formula.rb', line 118

def boolean?
  %w(> < = and or).include?(type)
end

#firstObject



106
107
108
# File 'lib/jpath/parser/formula.rb', line 106

def first
  parts[0]
end

#requires_boolean?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/jpath/parser/formula.rb', line 114

def requires_boolean?
  %w(and or).include?(type)
end

#secondObject



110
111
112
# File 'lib/jpath/parser/formula.rb', line 110

def second
  parts[1]
end

#to_predicateObject



122
123
124
# File 'lib/jpath/parser/formula.rb', line 122

def to_predicate
  Predicate.new(self)
end

#to_sObject



126
127
128
# File 'lib/jpath/parser/formula.rb', line 126

def to_s
  "%s%s%s" % [first, type, second]
end

#true?(*args) ⇒ Boolean

Returns:

  • (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