Module: Babl::Operators::Call::DSL

Included in:
Template
Defined in:
lib/babl/operators/call.rb

Instance Method Summary collapse

Instance Method Details

#call(*args, &block) ⇒ Object

Interpret whatever is passed to this method as BABL template. It is idempotent.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/babl/operators/call.rb', line 10

def call(*args, &block)
    if block
        raise Errors::InvalidTemplate, 'call() expects no argument when a block is given' unless args.empty?

        # The 'block' is wrapped by #selfify such that there is no implicit closure referencing the current
        # template. Ideally, once a template has been compiled, all intermediate template objects should be
        # garbage collectable.
        return with(unscoped, &Utils::Proc.selfify(block))
    end

    raise Errors::InvalidTemplate, 'call() expects exactly 1 argument (unless block)' unless args.size == 1

    arg = args.first

    case arg
    when Template then self.class.new(builder.wrap { |bound| arg.builder.bind(bound) })
    when Utils::DslProxy then call(arg.itself)
    when ::Symbol then nav(arg)
    when ::Proc then call(&arg)
    when ::Hash then object(arg)
    when ::Array then array(*arg)
    when ::String, ::Numeric, ::NilClass, ::TrueClass, ::FalseClass then static(arg)
    else raise Errors::InvalidTemplate, "call() received invalid argument: #{arg}"
    end
end