Module: Queryable

Defined in:
lib/queryable.rb,
lib/queryable/mongoid.rb,
lib/queryable/chainable.rb,
lib/queryable/active_record.rb,
lib/queryable/default_query.rb,
lib/queryable/default_scope.rb

Overview

Public: Allows to define default scopes in query objects, and inherit them in query object subclasses.

Defined Under Namespace

Modules: ActiveRecord, Chainable, ClassMethods, DefaultQuery, DefaultScope, Mongoid

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.chained_method(name, accessor) ⇒ Object

Internal: Generates a method that delegates the call to the an internal object, assigns the return value, and returns self.

Returns a String with the code of the method.



94
95
96
97
98
99
100
101
# File 'lib/queryable.rb', line 94

def self.chained_method(name, accessor)
  <<-CHAIN
    def #{name}(*args, &block)
      @queryable = #{accessor}.__send__(:#{name}, *args, &block)
      self
    end
  CHAIN
end

.default_delegated_methodsObject

Internal: Default methods to be delegated to the internal query.

Returns an Array with the name of the methods to delegate.



106
107
108
# File 'lib/queryable.rb', line 106

def self.default_delegated_methods
  Array.instance_methods - Object.instance_methods + [:all, :==, :as_json, :decorate]
end

.included(base) ⇒ Object

Internal: Adds class methods, a query accessor, and method delegation.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/queryable.rb', line 16

def self.included(base)
  base.extend Forwardable
  base.extend ClassMethods
  base.class_eval do
    # Public: Gets/Sets the internal query.
    attr_accessor :queryable
    alias_method :query, :queryable

    # Internal: Delegates Array and Criteria methods to the internal query.
    delegate *Queryable.default_delegated_methods
  end
end

Instance Method Details

#initialize(query) ⇒ Object

Public: Initialize a Queryable with a query.

query - The internal query to build upon.



32
33
34
# File 'lib/queryable.rb', line 32

def initialize(query)
  @queryable = query.all
end