Class: Hotdog::Expression::FuncallNode
- Inherits:
-
ExpressionNode
- Object
- ExpressionNode
- Hotdog::Expression::FuncallNode
- Defined in:
- lib/hotdog/expression/semantics.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#function ⇒ Object
readonly
Returns the value of attribute function.
Instance Method Summary collapse
- #dump(options = {}) ⇒ Object
- #evaluate(environment, options = {}) ⇒ Object
-
#initialize(function, args) ⇒ FuncallNode
constructor
A new instance of FuncallNode.
- #optimize(options = {}) ⇒ Object
Constructor Details
#initialize(function, args) ⇒ FuncallNode
Returns a new instance of FuncallNode.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/hotdog/expression/semantics.rb', line 415 def initialize(function, args) # FIXME: smart argument handling (e.g. arity & type checking) case function.to_s when "HEAD", "head" @function = :HEAD when "GROUP_BY", "group_by" @function = :GROUP_BY when "LIMIT", "limit" @function = :HEAD when "ORDER_BY", "order_by" @function = :ORDER_BY when "REVERSE", "reverse" @function = :REVERSE when "SAMPLE", "sample" @function = :HEAD args[0] = FuncallNode.new("SHUFFLE", [args[0]]) when "SHUFFLE", "shuffle" @function = :SHUFFLE when "SLICE", "slice" @function = :SLICE when "SORT", "sort" @function = :ORDER_BY when "TAIL", "tail" @function = :TAIL else raise(SyntaxError.new("unknown function call: #{function}")) end @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
413 414 415 |
# File 'lib/hotdog/expression/semantics.rb', line 413 def args @args end |
#function ⇒ Object (readonly)
Returns the value of attribute function.
413 414 415 |
# File 'lib/hotdog/expression/semantics.rb', line 413 def function @function end |
Instance Method Details
#dump(options = {}) ⇒ Object
445 446 447 448 449 450 451 452 453 454 |
# File 'lib/hotdog/expression/semantics.rb', line 445 def dump(={}) args = @args.map { |arg| if ExpressionNode === arg arg.dump() else arg end } {funcall: @function.to_s, args: args} end |
#evaluate(environment, options = {}) ⇒ Object
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/hotdog/expression/semantics.rb', line 490 def evaluate(environment, ={}) case function when :HEAD args[0].evaluate(environment, ).take(args[1] || 1) when :GROUP_BY intermediate = args[0].evaluate(environment, ) q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \ "WHERE tags.name = ? AND hosts_tags.host_id IN (%s) " \ "GROUP BY tags.value;" % intermediate.map { "?" }.join(", ") QueryExpressionNode.new(q, [args[1]] + intermediate, fallback: nil).evaluate(environment, ) when :ORDER_BY intermediate = args[0].evaluate(environment, ) if args[1] q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \ "WHERE tags.name = ? AND hosts_tags.host_id IN (%s) " \ "ORDER BY tags.value;" % intermediate.map { "?" }.join(", ") QueryExpressionNode.new(q, [args[1]] + intermediate, fallback: nil).evaluate(environment, ) else q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \ "INNER JOIN tags ON hosts_tags.tag_id = tags.id " \ "WHERE hosts_tags.host_id IN (%s) " \ "ORDER BY hosts_tags.host_id;" % intermediate.map { "?" }.join(", ") QueryExpressionNode.new(q, intermediate, fallback: nil).evaluate(environment, ) end when :REVERSE args[0].evaluate(environment, ).reverse() when :SHUFFLE args[0].evaluate(environment, ).shuffle() when :SLICE args[0].evaluate(environment, ).slice(args[1], args[2] || 1) when :TAIL args[0].evaluate(environment, ).last(args[1] || 1) else [] end end |
#optimize(options = {}) ⇒ Object
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/hotdog/expression/semantics.rb', line 456 def optimize(={}) case function when :HEAD @args[0] = @args[0].optimize() when :GROUP_BY @args[0] = @args[0].optimize() if TagExpressionNode === args[1] # workaround for expressions like `ORDER_BY((environment:development),role)` @args[1] = @args[1].tag_name else @args[1] = @args[1] end when :ORDER_BY @args[0] = @args[0].optimize() if @args[1] if TagExpressionNode === @args[1] # workaround for expressions like `ORDER_BY((environment:development),role)` @args[1] = @args[1].tag_name else @args[1] = @args[1] end end when :REVERSE @args[0] = @args[0].optimize() when :SHUFFLE @args[0] = @args[0].optimize() when :SLICE @args[0] = @args[0].optimize() when :TAIL @args[0] = @args[0].optimize() end self end |