module Babl::Operators::Object::DSL

Public Instance Methods

object(*args) click to toggle source

Create a JSON object node with static structure

# File lib/babl/operators/object.rb, line 11
def object(*args)
    kwargs = ::Hash === args.last ? args.pop : Utils::Hash::EMPTY

    (args.map(&:to_sym) + kwargs.keys.map(&:to_sym)).group_by(&:itself).each_value do |keys|
        raise Errors::InvalidTemplate, "Duplicate key in object(): #{keys.first}" if keys.size > 1
    end

    templates = args
        .map { |name| [name.to_sym, unscoped.nav(name)] }.to_h
        .merge(kwargs)
        .map { |k, v| [k, unscoped.reset_continue.call(v)] }

    construct_terminal { |ctx|
        Nodes::Object.new(templates.map { |key, template|
            [
                key.to_sym,
                template.builder.precompile(
                    Nodes::TerminalValue.instance,
                    ctx.merge(key: key)
                )
            ]
        }.to_h)
    }
end