module Babl::Operators::Call::DSL

Public Instance Methods

call(*args, &block) click to toggle source

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

# 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 ::Proc then call(&arg)
    when ::Hash then object(arg)
    when ::Array then array(*arg)
    when ::String, ::Numeric, ::NilClass, ::TrueClass, ::FalseClass, ::Symbol then static(arg)
    else raise Errors::InvalidTemplate, "call() received invalid argument: #{arg}"
    end
end