Sha256: d8ef6be1d5d07dcc0eeb39a799a51538bdae9dda7eca6ab631773482ea57a4ba

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module ROM

  # @api private
  class Header
    include Enumerable
    include Equalizer.new(:attributes)

    attr_reader :attributes

    class Attribute
      include Equalizer.new(:name, :key, :type)

      attr_reader :name, :key, :meta

      class Embedded < Attribute
        include Equalizer.new(:name, :type, :model, :header)

        def model
          meta[:model]
        end

        def header
          meta.fetch(:header)
        end

        def mapping
          [name, header.mapping]
        end

        def embedded?
          true
        end

      end

      def self.[](type)
        if type == Array || type == Hash
          Embedded
        else
          self
        end
      end

      def self.coerce(input)
        if input.kind_of?(self)
          input
        else
          name = input[0]
          meta = (input[1] || {}).dup

          meta[:type] ||= Object

          if meta.key?(:header)
            meta[:header] = Header.coerce(meta[:header])
          end

          self[meta[:type]].new(name, meta)
        end
      end

      def initialize(name, meta = {})
        @name = name
        @meta = meta
        @key = meta.fetch(:from) { name }
      end

      def type
        meta.fetch(:type)
      end

      def aliased?
        key != name
      end

      def embedded?
        false
      end

      def mapping
        [key, name]
      end
    end

    def self.coerce(input)
      if input.kind_of?(self)
        input
      else
        attributes = input.each_with_object({}) { |pair, h|
          h[pair.first] = Attribute.coerce(pair)
        }

        new(attributes)
      end
    end

    def initialize(attributes)
      @attributes = attributes
    end

    def each(&block)
      return to_enum unless block
      attributes.values.each(&block)
    end

    def keys
      attributes.keys
    end

    def mapping
      Hash[map(&:mapping)]
    end

    def values
      attributes.values
    end

    def [](name)
      attributes.fetch(name)
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rom-0.4.1 lib/rom/header.rb
rom-0.4.0 lib/rom/header.rb
rom-0.3.1 lib/rom/header.rb
rom-0.3.0 lib/rom/header.rb