module Queryable::ClassMethods

Internal: Contains the Queryable class methods.

Public Instance Methods

delegate(*methods) click to toggle source

Public: Delegates the specified methods to the internal query.

# File lib/queryable.rb, line 40
def delegate(*methods)
  def_delegators extract_delegation_target(methods), *methods
end
delegate_and_chain(*methods) click to toggle source

Public: Delegates the specified methods to the internal query, assigns the return value, and returns self.

# File lib/queryable.rb, line 46
def delegate_and_chain(*methods)
  to = extract_delegation_target(methods)
  class_eval methods.map { |name| Queryable.chained_method(name, to) }.join
end
extract_delegation_target(args) click to toggle source

Internal: Extracts the :to option of the arguments, uses the internal query object as the target if no option is provided.

# File lib/queryable.rb, line 53
def extract_delegation_target(args)
  to = args.last.is_a?(Hash) && args.pop[:to] || :queryable
  to == :class ? 'self.class' : to
end
scope(name, proc=nil, &block) click to toggle source

Public: Defines a new method that executes the passed proc or block in the context of the internal query object, and returns self.

name - Name of the scope to define for this Queryable.

proc - An optional proc or lambda to be executed in the context of the

the current query.

block - An optional block to be executed in the context of the current

query.

Yields the arguments given to the scope when invoked, generally none.

Examples

scope :active, ->{ where(status: 'active') }

scope(:recent) { desc(:created_at) }

scope :of_brand do |brand|
  where(_type: "#{brand}ExtremelyFastRacingCar")
end

Returns nothing.

# File lib/queryable.rb, line 82
def scope(name, proc=nil, &block)
  define_method(name) do |*args|
    @queryable = queryable.instance_exec *args, &(proc || block)
    self
  end
end