Module: Queryable::ClassMethods

Defined in:
lib/queryable.rb

Overview

Internal: Contains the Queryable class methods.

Instance Method Summary collapse

Instance Method Details

#delegate(*methods) ⇒ Object

Public: Delegates the specified methods to the internal query.



40
41
42
43
# File 'lib/queryable.rb', line 40

def delegate(*methods)
  to = methods.last.is_a?(Hash) && methods.pop[:to] || :queryable
  def_delegators(to == :class ? 'self.class' : to, *methods)
end

#scope(name, proc = nil, &block) ⇒ Object

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.



69
70
71
72
73
74
# File 'lib/queryable.rb', line 69

def scope(name, proc=nil, &block)
  define_method(name) do |*args|
    @queryable = queryable.instance_exec *args, &(proc || block)
    self
  end
end