Sha256: fe5c1b090fadba98c58ca2db5cc324f3c29ad68a292f46f57df0082125e40a74

Contents?: true

Size: 1.89 KB

Versions: 14

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Esse
  class Index
    module ClassMethods
      attr_writer :repo_hash

      def repo_hash
        @repo_hash ||= {}
      end

      def repo(name = nil)
        if name.nil? && repo_hash.size == 1
          name = repo_hash.keys.first
        elsif name.nil? && repo_hash.size > 1
          raise ArgumentError, "You can only call `repo' with a name when there is only one type defined."
        end
        name ||= DEFAULT_REPO_NAME

        repo_hash.fetch(name.to_s)
      rescue KeyError
        raise ArgumentError, <<~MSG
          No repo named "#{name}" found. Use the `repository' method to define one:

            repository :#{name} do
              # collection ...
              # document ...
            end
        MSG
      end

      def repo?(name = nil)
        return repo_hash.size > 0 if name.nil?

        repo_hash.key?(name.to_s)
      end

      def repository(repo_name, *_args, **kwargs, &block)
        repo_class = Class.new(Esse::Repository)
        kwargs[:const] = true unless kwargs.key?(:const) # TODO Change this to false to avoid collisions with application classes
        kwargs[:lazy_evaluate] ||= false

        if kwargs[:const]
          const_set(Hstring.new(repo_name).camelize.demodulize.to_s, repo_class)
        end

        index = self

        repo_class.send(:define_singleton_method, :index) { index }
        repo_class.send(:define_singleton_method, :repo_name) { repo_name.to_s }

        plugins.each do |mod|
          next unless mod.const_defined?(:RepositoryClassMethods, false)

          repository_plugin_extend(repo_class, mod::RepositoryClassMethods)
        end

        if kwargs[:lazy_evaluate]

        elsif block
          repo_class.class_eval(&block)
        end

        self.repo_hash = repo_hash.merge(repo_class.repo_name => repo_class)
        repo_class
      end
    end

    extend ClassMethods
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
esse-0.4.0.rc4 lib/esse/index/type.rb
esse-0.4.0.rc3 lib/esse/index/type.rb
esse-0.4.0.rc2 lib/esse/index/type.rb
esse-0.4.0.rc1 lib/esse/index/type.rb
esse-0.3.5 lib/esse/index/type.rb
esse-0.3.4 lib/esse/index/type.rb
esse-0.3.3 lib/esse/index/type.rb
esse-0.3.2 lib/esse/index/type.rb
esse-0.3.1 lib/esse/index/type.rb
esse-0.3.0 lib/esse/index/type.rb
esse-0.2.6 lib/esse/index/type.rb
esse-0.2.5 lib/esse/index/type.rb
esse-0.2.4 lib/esse/index/type.rb
esse-0.2.3 lib/esse/index/type.rb