Sha256: 6bc9d97d0820fe7392a0c5b791180264ea607130abb563a76503ba7cd97bedce

Contents?: true

Size: 931 Bytes

Versions: 1

Compression:

Stored size: 931 Bytes

Contents

module Supa
  class Builder
    COMMANDS_WITH_DEFAULT_INTERFACE = %w(attribute virtual object namespace collection append).freeze

    def initialize(subject, representer:, tree:)
      @subject = subject
      @representer = representer
      @tree = tree
    end

    COMMANDS_WITH_DEFAULT_INTERFACE.each do |command|
      klass = Supa::Commands.const_get(command.capitalize)

      define_method(command) do |name, **options, &block|
        klass.new(@subject,
          representer: @representer,
          tree: @tree,
          name: name,
          options: options,
          &block).represent
      end
    end

    def attributes(*names, **options)
      Supa::Commands::Attributes.new(
        @subject, representer: @representer, tree: @tree, name: names, options: options
      ).represent
    end

    def to_hash
      @tree.to_hash
    end

    def to_json
      Oj.dump(to_hash, mode: :compat)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
supa-0.5.0 lib/supa/builder.rb