Class: Humanoid::Collection

Inherits:
Object show all
Includes:
Humanoid::Collections::Mimic
Defined in:
lib/humanoid/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Humanoid::Collections::Mimic

included

Constructor Details

#initialize(klass, name) ⇒ Collection

Initialize a new Humanoid::Collection, setting up the master, slave, and name attributes. Masters will be used for writes, slaves for reads.

Example:

Humanoid::Collection.new(masters, slaves, "test")



76
77
78
# File 'lib/humanoid/collection.rb', line 76

def initialize(klass, name)
  @klass, @name = klass, name
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



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

def counter
  @counter
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#directed(options = {}) ⇒ Object

Determines where to send the next read query. If the slaves are not defined then send to master. If the read counter is under the configured maximum then return the master. In any other case return the slaves.

Example:

collection.directed

Return:

Either a Master or Slaves collection.



32
33
34
35
# File 'lib/humanoid/collection.rb', line 32

def directed(options = {})
  enslave = options.delete(:enslave) || @klass.enslaved?
  enslave ? master_or_slaves : master
end

#find(selector = {}, options = {}) ⇒ Object

Find documents from the database given a selector and options.

Options:

selector: A Hash selector that is the query. options: The options to pass to the db.

Example:

collection.find({ :test => "value" })



47
48
49
50
51
52
53
54
# File 'lib/humanoid/collection.rb', line 47

def find(selector = {}, options = {})
  cursor = Humanoid::Cursor.new(@klass, self, directed(options).find(selector, options))
  if block_given?
    yield cursor; cursor.close
  else
    cursor
  end
end

#find_one(selector = {}, options = {}) ⇒ Object

Find the first document from the database given a selector and options.

Options:

selector: A Hash selector that is the query. options: The options to pass to the db.

Example:

collection.find_one({ :test => "value" })



66
67
68
# File 'lib/humanoid/collection.rb', line 66

def find_one(selector = {}, options = {})
  directed(options).find_one(selector, options)
end

#map_reduce(map, reduce, options = {}) ⇒ Object Also known as: mapreduce

Perform a map/reduce on the documents.

Options:

map: The map javascript funcdtion. reduce: The reduce javascript function.



86
87
88
# File 'lib/humanoid/collection.rb', line 86

def map_reduce(map, reduce, options = {})
  directed(options).map_reduce(map, reduce, options)
end

#masterObject

Return the object responsible for writes to the database. This will always return a collection associated with the Master DB.

Example:

collection.writer



98
99
100
# File 'lib/humanoid/collection.rb', line 98

def master
  @master ||= Collections::Master.new(Humanoid.master, @name)
end

#slavesObject

Return the object responsible for reading documents from the database. This is usually the slave databases, but in their absence the master will handle the task.

Example:

collection.reader



109
110
111
# File 'lib/humanoid/collection.rb', line 109

def slaves
  @slaves ||= Collections::Slaves.new(Humanoid.slaves, @name)
end