Sha256: 5cfc9adc6d81eeb78d1b15f4a87d6e8d289ddc5e28d5f5f48acdd2b61c50083b

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

require 'supa/commands/attribute'
require 'supa/commands/virtual'
require 'supa/commands/object'
require 'supa/commands/namespace'
require 'supa/commands/collection'
require 'supa/commands/append'

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

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

      define_method command do |name, getter = nil, options = {}, &block|
        klass.new(representer: representer,
                  context: context,
                  tree: tree,
                  name: name,
                  getter: getter,
                  options: options,
                  &block).represent
      end
    end

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

    def to_hash
      tree.to_hash
    end

    def to_json
      to_hash.to_json
    end

    private

    attr_reader :representer, :context, :tree
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
supa-0.2.2 lib/supa/builder.rb
supa-0.2.1 lib/supa/builder.rb