Class: Humanoid::Criteria
- Includes:
- Enumerable, Humanoid::Criterion::Exclusion, Humanoid::Criterion::Inclusion, Humanoid::Criterion::Optional
- Defined in:
- lib/humanoid/criteria.rb
Overview
The Criteria
class is the core object needed in Humanoid to retrieve objects from the database. It is a DSL that essentially sets up the selector and options arguments that get passed on to a Mongo::Collection
in the Ruby driver. Each method on the Criteria
returns self to they can be chained in order to create a readable criterion to be executed against the database.
Example setup:
criteria = Criteria.new
criteria.only(:field).where(:field => "value").skip(20).limit(20)
criteria.execute
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#documents ⇒ Object
Returns the value of attribute documents.
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Class Method Summary collapse
-
.translate(*args) ⇒ Object
Translate the supplied arguments into a
Criteria
object.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Concatinate the criteria with another enumerable.
-
#-(other) ⇒ Object
Returns the difference between the criteria and another enumerable.
-
#==(other) ⇒ Object
Returns true if the supplied
Enumerable
orCriteria
is equal to the results of thisCriteria
or the criteria itself. -
#blank? ⇒ Boolean
(also: #empty?)
Returns true if the criteria is empty.
-
#context ⇒ Object
Return or create the context in which this criteria should be executed.
-
#each(&block) ⇒ Object
Iterate over each
Document
in the results. -
#fuse(criteria_conditions = {}) ⇒ Object
Merges the supplied argument hash into a single criteria.
-
#initialize(klass) ⇒ Criteria
constructor
Create the new
Criteria
object. -
#merge(other) ⇒ Object
Merges another object into this
Criteria
. -
#method_missing(name, *args) ⇒ Object
Used for chaining
Criteria
scopes together in the for of class methods on theDocument
the criteria is for. -
#scoped ⇒ Object
Returns the selector and options as a
Hash
that would be passed to a scope for use with named scopes.
Methods included from Humanoid::Criterion::Optional
#cache, #cached?, #enslave, #extras, #id, #limit, #offset, #order_by, #skip
Methods included from Humanoid::Criterion::Inclusion
Methods included from Humanoid::Criterion::Exclusion
Constructor Details
#initialize(klass) ⇒ Criteria
Create the new Criteria
object. This will initialize the selector and options hashes, as well as the type of criteria.
Options:
type: One of :all, :first:, or :last klass: The class to execute on.
136 137 138 |
# File 'lib/humanoid/criteria.rb', line 136 def initialize(klass) @selector, @options, @klass, @documents = {}, {}, klass, [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Used for chaining Criteria
scopes together in the for of class methods on the Document
the criteria is for.
Options:
name: The name of the class method on the Document
to chain. args: The arguments passed to the method.
Returns: Criteria
166 167 168 169 170 171 172 173 174 |
# File 'lib/humanoid/criteria.rb', line 166 def method_missing(name, *args) if @klass.respond_to?(name) new_scope = @klass.send(name) new_scope.merge(self) return new_scope else return entries.send(name, *args) end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
28 29 30 |
# File 'lib/humanoid/criteria.rb', line 28 def collection @collection end |
#documents ⇒ Object
Returns the value of attribute documents.
30 31 32 |
# File 'lib/humanoid/criteria.rb', line 30 def documents @documents end |
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
28 29 30 |
# File 'lib/humanoid/criteria.rb', line 28 def ids @ids end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
28 29 30 |
# File 'lib/humanoid/criteria.rb', line 28 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/humanoid/criteria.rb', line 28 def @options end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
28 29 30 |
# File 'lib/humanoid/criteria.rb', line 28 def selector @selector end |
Class Method Details
.translate(*args) ⇒ Object
Translate the supplied arguments into a Criteria
object.
If the passed in args is a single String
, then it will construct an id Criteria
from it.
If the passed in args are a type and a hash, then it will construct the Criteria
with the proper selector, options, and type.
Options:
args: either a String
or a Symbol
, +Hash combination.
Example:
Criteria.translate(Person, "4ab2bc4b8ad548971900005c")
Criteria.translate(Person, :conditions => { :field => "value"}, :limit => 20)
200 201 202 203 204 205 206 207 |
# File 'lib/humanoid/criteria.rb', line 200 def self.translate(*args) klass = args[0] params = args[1] || {} unless params.is_a?(Hash) return new(klass).id_criteria(params) end return new(klass).where(params.delete(:conditions) || {}).extras(params) end |
Instance Method Details
#+(other) ⇒ Object
Concatinate the criteria with another enumerable. If the other is a Criteria
then it needs to get the collection from it.
50 51 52 |
# File 'lib/humanoid/criteria.rb', line 50 def +(other) entries + comparable(other) end |
#-(other) ⇒ Object
Returns the difference between the criteria and another enumerable. If the other is a Criteria
then it needs to get the collection from it.
56 57 58 |
# File 'lib/humanoid/criteria.rb', line 56 def -(other) entries - comparable(other) end |
#==(other) ⇒ Object
Returns true if the supplied Enumerable
or Criteria
is equal to the results of this Criteria
or the criteria itself.
This will force a database load when called if an enumerable is passed.
Options:
other: The other Enumerable
or Criteria
to compare to.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/humanoid/criteria.rb', line 68 def ==(other) case other when Criteria self.selector == other.selector && self. == other. when Enumerable return (execute.entries == other) else return false end end |
#blank? ⇒ Boolean Also known as: empty?
Returns true if the criteria is empty.
Example:
criteria.blank?
84 85 86 |
# File 'lib/humanoid/criteria.rb', line 84 def blank? count < 1 end |
#context ⇒ Object
Return or create the context in which this criteria should be executed.
This will return an Enumerable context if the class is embedded, otherwise it will return a Mongo context for root classes.
94 95 96 |
# File 'lib/humanoid/criteria.rb', line 94 def context @context ||= Contexts.context_for(self) end |
#each(&block) ⇒ Object
Iterate over each Document
in the results. This can take an optional block to pass to each argument in the results.
Example:
criteria.each { |doc| p doc }
104 105 106 107 108 109 110 |
# File 'lib/humanoid/criteria.rb', line 104 def each(&block) return caching(&block) if cached? if block_given? execute.each { |doc| yield doc } end self end |
#fuse(criteria_conditions = {}) ⇒ Object
Merges the supplied argument hash into a single criteria
Options:
criteria_conditions: Hash of criteria keys, and parameter values
Example:
criteria.fuse(:where => { :field => "value"}, :limit => 20)
Returns self
123 124 125 126 127 |
# File 'lib/humanoid/criteria.rb', line 123 def fuse(criteria_conditions = {}) criteria_conditions.inject(self) do |criteria, (key, value)| criteria.send(key, value) end end |
#merge(other) ⇒ Object
Merges another object into this Criteria
. The other object may be a Criteria
or a Hash
. This is used to combine multiple scopes together, where a chained scope situation may be desired.
Options:
other: The Criteria
or Hash
to merge with.
Example:
criteria.merge({ :conditions => { :title => "Sir" } })
151 152 153 154 155 |
# File 'lib/humanoid/criteria.rb', line 151 def merge(other) @selector.update(other.selector) @options.update(other.) @documents = other.documents end |
#scoped ⇒ Object
Returns the selector and options as a Hash
that would be passed to a scope for use with named scopes.
180 181 182 |
# File 'lib/humanoid/criteria.rb', line 180 def scoped { :where => @selector }.merge(@options) end |