Class: Humanoid::Contexts::Mongo

Inherits:
Object
  • Object
show all
Includes:
Ids, Paging
Defined in:
lib/humanoid/contexts/mongo.rb

Constant Summary collapse

AGGREGATE_REDUCE =
"function(obj, prev) { prev.count++; }"
GROUP_REDUCE =
"function(obj, prev) { prev.group.push(obj); }"
MAX_REDUCE =
"function(obj, prev) { if (prev.max == 'start') { prev.max = obj.[field]; } " +
"if (prev.max < obj.[field]) { prev.max = obj.[field]; } }"
MIN_REDUCE =
"function(obj, prev) { if (prev.min == 'start') { prev.min = obj.[field]; } " +
"if (prev.min > obj.[field]) { prev.min = obj.[field]; } }"
SUM_REDUCE =
"function(obj, prev) { if (prev.sum == 'start') { prev.sum = 0; } prev.sum += obj.[field]; }"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ids

#id_criteria

Methods included from Paging

#page, #paginate, #per_page

Constructor Details

#initialize(criteria) ⇒ Mongo

Create the new mongo context. This will execute the queries given the selector and options against the database.

Example:

Humanoid::Contexts::Mongo.new(criteria)



96
97
98
99
100
101
# File 'lib/humanoid/contexts/mongo.rb', line 96

def initialize(criteria)
  @criteria = criteria
  if criteria.klass.hereditary
    criteria.in(:_type => criteria.klass._types)
  end
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



6
7
8
# File 'lib/humanoid/contexts/mongo.rb', line 6

def criteria
  @criteria
end

Instance Method Details

#aggregateObject

Aggregate the context. This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with counts.

Example:

context.aggregate

Returns:

A Hash with field values as keys, counts as values



23
24
25
# File 'lib/humanoid/contexts/mongo.rb', line 23

def aggregate
  klass.collection.group(options[:fields], selector, { :count => 0 }, AGGREGATE_REDUCE, true)
end

#countObject

Get the count of matching documents in the database for the context.

Example:

context.count

Returns:

An Integer count of documents.



36
37
38
# File 'lib/humanoid/contexts/mongo.rb', line 36

def count
  @count ||= klass.collection.find(selector, process_options).count
end

#execute(paginating = false) ⇒ Object

Execute the context. This will take the selector and options and pass them on to the Ruby driver’s find() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned new documents of the type of class provided will be instantiated.

Example:

mongo.execute

Returns:

An enumerable Cursor.



52
53
54
55
56
57
58
59
60
# File 'lib/humanoid/contexts/mongo.rb', line 52

def execute(paginating = false)
  cursor = klass.collection.find(selector, process_options)
  if cursor
    @count = cursor.count if paginating
    cursor
  else
    []
  end
end

#groupObject

Groups the context. This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with objects.

Example:

context.group

Returns:

A Hash with field values as keys, arrays of documents as values.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/humanoid/contexts/mongo.rb', line 75

def group
  klass.collection.group(
    options[:fields],
    selector,
    { :group => [] },
    GROUP_REDUCE,
    true
  ).collect do |docs|
    docs["group"] = docs["group"].collect do |attrs|
      Humanoid::Factory.build(klass, attrs)
    end
    docs
  end
end

#grouped(start, field, reduce) ⇒ Object

Common functionality for grouping operations. Currently used by min, max and sum. Will gsub the field name in the supplied reduce function.



200
201
202
203
204
205
206
207
208
209
# File 'lib/humanoid/contexts/mongo.rb', line 200

def grouped(start, field, reduce)
  collection = klass.collection.group(
    nil,
    selector,
    { start => "start" },
    reduce.gsub("[field]", field),
    true
  )
  collection.empty? ? nil : collection.first[start.to_s]
end

#lastObject

Return the last result for the Context. Essentially does a find_one on the collection with the sorting reversed. If no sorting parameters have been provided it will default to ids.

Example:

context.last

Returns:

The last document in the collection.



114
115
116
117
118
119
120
121
# File 'lib/humanoid/contexts/mongo.rb', line 114

def last
  opts = process_options
  sorting = opts[:sort]
  sorting = [[:_id, :asc]] unless sorting
  opts[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] }
  attributes = klass.collection.find_one(selector, opts)
  attributes ? Humanoid::Factory.build(klass, attributes) : nil
end

#max(field) ⇒ Object

Return the max value for a field.

This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.

Example:

context.max(:age)

Returns:

A numeric max value.



139
140
141
# File 'lib/humanoid/contexts/mongo.rb', line 139

def max(field)
  grouped(:max, field.to_s, MAX_REDUCE)
end

#min(field) ⇒ Object

Return the min value for a field.

This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.

Example:

context.min(:age)

Returns:

A numeric minimum value.



159
160
161
# File 'lib/humanoid/contexts/mongo.rb', line 159

def min(field)
  grouped(:min, field.to_s, MIN_REDUCE)
end

#oneObject Also known as: first

Return the first result for the Context.

Example:

context.one

Return:

The first document in the collection.



172
173
174
175
# File 'lib/humanoid/contexts/mongo.rb', line 172

def one
  attributes = klass.collection.find_one(selector, process_options)
  attributes ? Humanoid::Factory.build(klass, attributes) : nil
end

#process_optionsObject

Filters the field list. If no fields have been supplied, then it will be empty. If fields have been defined then _type will be included as well.



213
214
215
216
217
218
219
220
# File 'lib/humanoid/contexts/mongo.rb', line 213

def process_options
  fields = options[:fields]
  if fields && fields.size > 0 && !fields.include?(:_type)
    fields << :_type
    options[:fields] = fields
  end
  options.dup
end

#sum(field) ⇒ Object

Sum the context.

This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.

Example:

context.sum(:age)

Returns:

A numeric value that is the sum.



194
195
196
# File 'lib/humanoid/contexts/mongo.rb', line 194

def sum(field)
  grouped(:sum, field.to_s, SUM_REDUCE)
end