Sha256: b5ad4208b32742305c30f4c9beb63652d78b586eb22c0957ecd68f32a510e977

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module ROM
  # Globally accessible public interface exposed via ROM module
  #
  # @public
  module Global
    # Starts the setup process for schema, relations, mappers and commands
    #
    # @example
    #
    #   ROM.setup('sqlite::memory')
    #
    #   ROM.relation(:users) do
    #     # ...
    #   end
    #
    #   ROM.mappers do
    #     define(:users) do
    #       # ...
    #     end
    #   end
    #
    #   ROM.commands(:users) do
    #     define(:create) do
    #       # ...
    #     end
    #   end
    #
    #   ROM.finalize # builds the env
    #   ROM.env # returns the env registry
    #
    # @param [Hash] options repository URIs
    #
    # @return [Setup] boot object
    #
    # @api public
    def setup(*args, &block)
      config = Config.build(*args)

      adapters = config.each_with_object({}) do |(name, uri_or_opts), hash|
        uri, opts =
          if uri_or_opts.is_a?(Hash)
            uri_or_opts.values_at(:uri, :options)
          else
            [uri_or_opts, {}]
          end

        hash[name] = Adapter.setup(uri, opts)
      end

      repositories = adapters.each_with_object({}) do |(name, adapter), hash|
        hash[name] = Repository.new(adapter)
      end

      boot = Setup.new(repositories)

      if block
        boot.instance_exec(&block)
        boot.finalize
      else
        @boot = boot
      end
    end

    # @see ROM::Setup#schema
    #
    # @api public
    def schema(&block)
      boot.schema(&block)
    end

    # @see ROM::Setup#relation
    #
    # @api public
    def relation(*args, &block)
      boot.relation(*args, &block)
    end

    # @api public
    def commands(*args, &block)
      boot.commands(*args, &block)
    end

    # @api public
    def mappers(*args, &block)
      boot.mappers(*args, &block)
    end

    # @api public
    def env
      @env
    end

    # @api public
    def finalize
      @env = boot.finalize
      @boot = nil
      self
    end

    private

    # @api private
    def boot
      @boot
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.5.0 lib/rom/global.rb