Sha256: e334bb34c669e53d353a9637cbef8830be5e487ddb6b051c446cc78ca6e115a7

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8

module ROM
  class Schema
    class Definition

      # Builder object for Axiom relation
      #
      # @private
      class Relation
        include Equalizer.new(:header, :keys)

        attr_reader :registry

        # @api private
        def initialize(registry, &block)
          @registry = registry
          @header = []
          @keys   = []
          @wrappings = []
          @groupings = []
          instance_eval(&block)
        end

        # @api private
        def call(name)
          relation = Axiom::Relation::Base.new(name, build_header)

          if @wrappings.any?
            @wrappings.each { |wrapping| relation = relation.wrap(wrapping) }
          end

          if @groupings.any?
            @groupings.each { |grouping| relation = relation.group(grouping) }
          end

          renames = @header.each_with_object({}) { |ary, mapping|
            mapping[ary.first] = ary.last[:rename] if ary.last[:rename]
          }

          relation.rename(renames).optimize
        end

        # @api private
        def attribute(name, type, options = {})
          @header << [name, type, options]
        end

        # @api private
        def wrap(wrapping)
          @wrappings << wrapping
        end

        # @api private
        def group(grouping)
          @groupings << grouping
        end

        # @api private
        def key(*attribute_names)
          @keys.concat(attribute_names)
        end

        private

        # @api private
        def method_missing(*args)
          registry[args.first] || super
        end

        # @api private
        def build_header
          Axiom::Relation::Header.coerce(@header, keys: @keys)
        end

      end # Relation

    end # Definition
  end # Schema
end # ROM

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.2.0 lib/rom/schema/definition/relation.rb