Module: Humanoid::Finders

Defined in:
lib/humanoid/finders.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#all(*args) ⇒ Object

Find Documents given the conditions.

Options:

args: A Hash with a conditions key and other options

Person.all(:conditions => { :attribute => "value" })



11
12
13
# File 'lib/humanoid/finders.rb', line 11

def all(*args)
  find(:all, *args)
end

#count(*args) ⇒ Object

Returns a count of matching records in the database based on the provided arguments.

Person.count(:first, :conditions => { :attribute => "value" })



19
20
21
# File 'lib/humanoid/finders.rb', line 19

def count(*args)
  Criteria.translate(self, *args).count
end

#criteriaObject

Helper to initialize a new Criteria object for this class.

Example:

Person.criteria



28
29
30
# File 'lib/humanoid/finders.rb', line 28

def criteria
  Criteria.new(self)
end

#find(*args) ⇒ Object

Find a Document in several different ways.

If a String is provided, it will be assumed that it is a representation of a Mongo::ObjectID and will attempt to find a single Document based on that id. If a Symbol and Hash is provided then it will attempt to find either a single Document or multiples based on the conditions provided and the first parameter.

Person.find(:first, :conditions => { :attribute => "value" })

Person.find(:all, :conditions => { :attribute => "value" })

Person.find(Mongo::ObjectID.new.to_s)



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/humanoid/finders.rb', line 45

def find(*args)
  raise Errors::InvalidOptions.new("Calling Document#find with nil is invalid") if args[0].nil?
  type = args.delete_at(0) if args[0].is_a?(Symbol)
  criteria = Criteria.translate(self, *args)
  case type
  when :first then return criteria.one
  when :last then return criteria.last
  else
    return criteria
  end
end

#find_or_create_by(attrs = {}) ⇒ Object

Find the first Document given the conditions, or creates a new document with the conditions that were supplied

Options:

args: A Hash of attributes

Person.find_or_create_by(:attribute => "value")



65
66
67
# File 'lib/humanoid/finders.rb', line 65

def find_or_create_by(attrs = {})
  find_or(:create, attrs)
end

#find_or_initialize_by(attrs = {}) ⇒ Object

Find the first Document given the conditions, or instantiates a new document with the conditions that were supplied

Options:

args: A Hash of attributes

Person.find_or_initialize_by(:attribute => "value")



77
78
79
# File 'lib/humanoid/finders.rb', line 77

def find_or_initialize_by(attrs = {})
  find_or(:new, attrs)
end

#first(*args) ⇒ Object

Find the first Document given the conditions.

Options:

args: A Hash with a conditions key and other options

Person.first(:conditions => { :attribute => "value" })



88
89
90
# File 'lib/humanoid/finders.rb', line 88

def first(*args)
  find(:first, *args)
end

#last(*args) ⇒ Object

Find the last Document given the conditions.

Options:

args: A Hash with a conditions key and other options

Person.last(:conditions => { :attribute => "value" })



99
100
101
# File 'lib/humanoid/finders.rb', line 99

def last(*args)
  find(:last, *args)
end

#max(field) ⇒ Object

Convenience method for returning the max value of a field.

Options:

field: The field to use when calculating the max.

Example:

Person.max(:age)

Returns: Float max value.



114
115
116
# File 'lib/humanoid/finders.rb', line 114

def max(field)
  Criteria.new(self).max(field)
end

#min(field) ⇒ Object

Convenience method for returning the min value of a field.

Options:

field: The field to use when calculating the min.

Example:

Person.min(:age)

Returns: Float min value.



147
148
149
# File 'lib/humanoid/finders.rb', line 147

def min(field)
  Criteria.new(self).min(field)
end

#only(*args) ⇒ Object

Entry point for creating a new criteria from a Document. This will instantiate a new Criteria object with the supplied select criterion already added to it.

Options:

args: A list of field names to retrict the returned fields to.

Example:

Person.only(:field1, :field2, :field3)

Returns: Criteria



181
182
183
# File 'lib/humanoid/finders.rb', line 181

def only(*args)
  Criteria.new(self).only(*args)
end

#paginate(params = {}) ⇒ Object

Find all documents in paginated fashion given the supplied arguments. If no parameters are passed just default to offset 0 and limit 20.

Options:

params: A Hash of params to pass to the Criteria API.

Example:

Person.paginate(:conditions => { :field => "Test" }, :page => 1, :per_page => 20)

Returns paginated array of docs.



164
165
166
# File 'lib/humanoid/finders.rb', line 164

def paginate(params = {})
  Criteria.translate(self, params).paginate
end

#sum(field) ⇒ Object

Convenience method for returning the sum of a specified field for all documents in the database.

Options:

field: The field to use when calculating the sum.

Example:

Person.sum(:age)

Returns: Float of the sum.



197
198
199
# File 'lib/humanoid/finders.rb', line 197

def sum(field)
  Criteria.new(self).sum(field)
end

#where(selector = nil) ⇒ Object

Entry point for creating a new criteria from a Document. This will instantiate a new Criteria object with the supplied select criterion already added to it.

Options:

selector: A where criteria to initialize.

Example:

Person.where(:field1 => "Value")

Returns: Criteria



214
215
216
# File 'lib/humanoid/finders.rb', line 214

def where(selector = nil)
  Criteria.new(self).where(selector)
end