Sha256: 612baf917fe49f1d40566406b063399d9fe121736c01275bf68029a6c300adfb

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

require 'rom/support/deprecations'
require 'rom/support/options'

require 'rom/repository/mapper_builder'
require 'rom/repository/loading_proxy'

module ROM
  class Repository
    # Abstract repository class to inherit from
    #
    # TODO: rename this to Repository once deprecated Repository from rom core is gone
    #
    # @api public
    include Options

    option :mapper_builder, reader: true, default: proc { MapperBuilder.new }

    # Define which relations your repository is going to use
    #
    # @example
    #   class MyRepo < ROM::Repository::Base
    #     relations :users, :tasks
    #   end
    #
    #   my_repo = MyRepo.new(rom_env)
    #
    #   my_repo.users
    #   my_repo.tasks
    #
    # @return [Array<Symbol>]
    #
    # @api public
    def self.relations(*names)
      if names.any?
        attr_reader(*names)
        @relations = names
      else
        @relations
      end
    end

    # @api private
    def initialize(env, options = {})
      super
      self.class.relations.each do |name|
        proxy = LoadingProxy.new(
          env.relation(name), name: name, mapper_builder: mapper_builder
        )
        instance_variable_set("@#{name}", proxy)
      end
    end

    class Base < Repository
      def self.inherited(klass)
        super
        Deprecations.announce(self, 'inherit from Repository instead')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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