Sha256: 07cc91999f1a354c7c5c5fa4deab883b87039dc233527248a276d5c2436e90aa

Contents?: true

Size: 976 Bytes

Versions: 2

Compression:

Stored size: 976 Bytes

Contents

module Querifier
  module Queries
    module Base
      def initialize(params)
        @params = params
        check_params!
      end

      def collection
        @collection ||= self.class.entity_class.all
      end

      def params
        @params.fetch(Config.filter_param, {})
      end

      def self.included(klass)
        klass.extend(ClassMethods)
      end

      module ClassMethods
        def entity_class(value = nil)
          return entity_class = value unless value.nil?
          @@entity_class ||= Object.const_get self.name.split('::').last.split('Query').first
        end

        def entity_class=(value)
          @@entity_class = value
        end
      end

      private

      def check_params!
        return if @params.respond_to?(:keys) && @params.respond_to?(:has_key?)
        raise ArgumentError, "The params should extend of a hash, instead, they are a #{@params.class}" # rubocop:disable Metrics/LineLength
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
querifier-0.1.1 lib/querifier/queries/base.rb
querifier-0.1.0 lib/querifier/queries/base.rb