Class: Piglet::Relation::BlockContext

Inherits:
Object
  • Object
show all
Defined in:
lib/piglet/relation/block_context.rb

Instance Method Summary collapse

Constructor Details

#initialize(relation, interpreter) ⇒ BlockContext

Returns a new instance of BlockContext.



6
7
8
# File 'lib/piglet/relation/block_context.rb', line 6

def initialize(relation, interpreter)
  @relation, @interpreter = relation, interpreter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/piglet/relation/block_context.rb', line 30

def method_missing(name, *args)
  if args.size == 0
    @relation.method_missing(name, *args)
  elsif @interpreter.respond_to?(name)
    @interpreter.send(name, *args)
  else
    super
  end
end

Instance Method Details

#[](n) ⇒ Object



26
27
28
# File 'lib/piglet/relation/block_context.rb', line 26

def [](n)
  @relation.field("\$#{n}")
end

#literal(obj) ⇒ Object

Support for literals in FOREACH … GENERATE blocks.

x.foreach { |r| [literal("hello").as(:hello)] } # => FOREACH x GENERATE 'hello' AS hello


13
14
15
# File 'lib/piglet/relation/block_context.rb', line 13

def literal(obj)
  Field::Literal.new(obj)
end

#test(test, if_true, if_false) ⇒ Object

Support for binary conditions, a.k.a. the ternary operator.

x.test(x.a > x.b, x.a, x.b) # => (a > b ? a : b)

Should only be used in the block given to #filter and #foreach



22
23
24
# File 'lib/piglet/relation/block_context.rb', line 22

def test(test, if_true, if_false)
  Field::BinaryConditional.new(test, if_true, if_false)
end