Sha256: f1c8c49cb5366d9f02a4c74265bff3e28688304a1726f688e2274684713b204a

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# encoding: utf-8

require 'rom/support/axiom/adapter'

module ROM

  # A repository with a given +name+ and +adapter+
  #
  # @api private
  class Repository
    include Concord.new(:name, :adapter, :relations)

    # Build a repository with a given +name+ and +uri+
    #
    # @param [Symbol] name
    #   the repository's name
    #
    # @param [Addressable::URI] uri
    #   the uri for initializing the adapter
    #
    # @return [Repository]
    #
    # @api private
    def self.build(name, uri, relations = {})
      new(name, Axiom::Adapter.build(uri), relations)
    end

    # Return the relation identified by +name+
    #
    # @example
    #
    #   repo = Repository.coerce(:test, 'in_memory://test')
    #   repo.register(:foo, [[:id, String], [:foo, String]])
    #   repo[:foo]
    #
    #   # => <Axiom::Relation header=Axiom::Header ...>
    #
    # @param [Symbol] name
    #   the name of the relation
    #
    # @return [Axiom::Relation]
    #
    # @raise [KeyError]
    #
    # @api public
    def [](name)
      relations.fetch(name)
    end

    # Register a relation with this repository
    #
    # @param [Axiom::Relation::Base] relation
    #
    # @return [Object] relation gateway
    #
    # @api public
    def []=(name, relation)
      relation =
        if adapter.respond_to?(:gateway)
          adapter.gateway(relation)
        else
          adapter[name] = relation
          adapter[name]
        end

      relations[name] = relation
    end

  end # Repository

end # ROM

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.2.0 lib/rom/repository.rb