Module: Queryable::ClassMethods
- Defined in:
- lib/queryable.rb
Overview
Internal: Contains the Queryable class methods.
Instance Method Summary collapse
-
#delegate(*methods) ⇒ Object
Public: Delegates the specified methods to the internal query.
-
#delegate_and_chain(*methods) ⇒ Object
Public: Delegates the specified methods to the internal query, assigns the return value, and returns self.
-
#extract_delegation_target(args) ⇒ Object
Internal: Extracts the :to option of the arguments, uses the internal query object as the target if no option is provided.
-
#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.
Instance Method Details
#delegate(*methods) ⇒ Object
Public: Delegates the specified methods to the internal query.
40 41 42 |
# File 'lib/queryable.rb', line 40 def delegate(*methods) def_delegators extract_delegation_target(methods), *methods end |
#delegate_and_chain(*methods) ⇒ Object
Public: Delegates the specified methods to the internal query, assigns the return value, and returns self.
46 47 48 49 |
# 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) ⇒ Object
Internal: Extracts the :to option of the arguments, uses the internal query object as the target if no option is provided.
53 54 55 56 |
# 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) ⇒ 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.
82 83 84 85 86 87 |
# 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 |