Class: Hotdog::Commands::Search::BinaryExpressionNode
- Inherits:
-
ExpressionNode
- Object
- ExpressionNode
- Hotdog::Commands::Search::BinaryExpressionNode
- Defined in:
- lib/hotdog/commands/search.rb
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#op ⇒ Object
readonly
Returns the value of attribute op.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #evaluate(environment, options = {}) ⇒ Object
-
#initialize(op, left, right) ⇒ BinaryExpressionNode
constructor
A new instance of BinaryExpressionNode.
- #optimize(options = {}) ⇒ Object
Constructor Details
#initialize(op, left, right) ⇒ BinaryExpressionNode
239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/hotdog/commands/search.rb', line 239 def initialize(op, left, right) case op || "or" when "&&", "&", /\Aand\z/i @op = :AND when "||", "|", /\Aor\z/i @op = :OR else raise(SyntaxError.new("unknown binary operator: #{op.inspect}")) end @left = left @right = right end |
Instance Attribute Details
#left ⇒ Object (readonly)
Returns the value of attribute left.
237 238 239 |
# File 'lib/hotdog/commands/search.rb', line 237 def left @left end |
#op ⇒ Object (readonly)
Returns the value of attribute op.
237 238 239 |
# File 'lib/hotdog/commands/search.rb', line 237 def op @op end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
237 238 239 |
# File 'lib/hotdog/commands/search.rb', line 237 def right @right end |
Instance Method Details
#==(other) ⇒ Object
289 290 291 |
# File 'lib/hotdog/commands/search.rb', line 289 def ==(other) self.class === other and @op == other.op and @left == other.left and @right == other.right end |
#evaluate(environment, options = {}) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/hotdog/commands/search.rb', line 252 def evaluate(environment, ={}) case @op when :AND left_values = @left.evaluate(environment) environment.logger.debug("lhs: #{left_values.length} value(s)") if left_values.empty? [] else right_values = @right.evaluate(environment) environment.logger.debug("rhs: #{right_values.length} value(s)") (left_values & right_values).tap do |values| environment.logger.debug("lhs AND rhs: #{values.length} value(s)") end end when :OR left_values = @left.evaluate(environment) environment.logger.debug("lhs: #{left_values.length} value(s)") right_values = @right.evaluate(environment) environment.logger.debug("rhs: #{right_values.length} value(s)") (left_values | right_values).uniq.tap do |values| environment.logger.debug("lhs OR rhs: #{values.length} value(s)") end else [] end end |
#optimize(options = {}) ⇒ Object
279 280 281 282 283 284 285 286 287 |
# File 'lib/hotdog/commands/search.rb', line 279 def optimize(={}) @left = @left.optimize() @right = @right.optimize() if @left == @right @left else self end end |