Sha256: 59b8806fa92447f4bee1bdcae1c7fa6d4bb51adcc06dfab004554fe7f3b7a986

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

module Railjet
  module Repository
    class Generic < Module
      class << self
        def [](dao = nil)
          new(dao)
        end

        attr_accessor :type
      end

      def initialize(dao = nil)
        @dao  = dao
        @type = self.class.type
      end

      def included(klass)
        define_dao_accessor(@type, @dao)
        define_type_accessor(klass, @type)
        define_initializer(klass)
        include_repository_methods(klass)
      end

      private

      def define_dao_accessor(type, dao)
        define_method type do
          @cached_dao_class ||= begin
            dao_klass = instance_variable_get("@#{type}") || instance_variable_set("@#{type}", (dao.constantize if dao.respond_to?(:constantize)))
            dao_klass.is_a?(String) ? dao_klass.safe_constantize : dao_klass
          end
        end
      end

      def define_type_accessor(klass, type)
        klass.define_singleton_method(:type) { type }
      end

      def define_initializer(klass)
        klass.class_eval do
          attr_reader :registry

          def initialize(registry, **kwargs)
            @registry = registry
            instance_variable_set("@#{self.class.type}", kwargs.fetch(self.class.type, nil))

            # Let's check if DAO was set through registry or set when including inner repo mixin
            unless send(self.class.type)
              raise ArgumentError, "Your repository #{self.class} need a DAO. It can be set with inner-repo mixin  or through registry with `#{self.class.type}:` option"
            end
          end
        end
      end
      
      def include_repository_methods(klass)
        if defined?(self.class::RepositoryMethods)
          klass.send :include, self.class::RepositoryMethods
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
railjet-4.0.0 lib/railjet/repository/generic.rb
railjet-3.5.0 lib/railjet/repository/generic.rb