Class: Humanoid::Collection
- Includes:
- Humanoid::Collections::Mimic
- Defined in:
- lib/humanoid/collection.rb
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#directed(options = {}) ⇒ Object
Determines where to send the next read query.
-
#find(selector = {}, options = {}) ⇒ Object
Find documents from the database given a selector and options.
-
#find_one(selector = {}, options = {}) ⇒ Object
Find the first document from the database given a selector and options.
-
#initialize(klass, name) ⇒ Collection
constructor
Initialize a new Humanoid::Collection, setting up the master, slave, and name attributes.
-
#map_reduce(map, reduce, options = {}) ⇒ Object
(also: #mapreduce)
Perform a map/reduce on the documents.
-
#master ⇒ Object
Return the object responsible for writes to the database.
-
#slaves ⇒ Object
Return the object responsible for reading documents from the database.
Methods included from Humanoid::Collections::Mimic
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
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
11 12 13 |
# File 'lib/humanoid/collection.rb', line 11 def counter @counter end |
#name ⇒ Object (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( = {}) enslave = .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 = {}, = {}) cursor = Humanoid::Cursor.new(@klass, self, directed().find(selector, )) 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 = {}, = {}) directed().find_one(selector, ) 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, = {}) directed().map_reduce(map, reduce, ) end |
#master ⇒ Object
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 |
#slaves ⇒ Object
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 |