Class: Crocoduck::Store
- Inherits:
-
Object
- Object
- Crocoduck::Store
- Includes:
- Logging
- Defined in:
- lib/crocoduck/store.rb
Class Attribute Summary collapse
-
.id_field ⇒ Object
Returns the value of attribute id_field.
-
.server_cluster ⇒ Object
Returns the value of attribute server_cluster.
-
.server_collection ⇒ Object
Returns the value of attribute server_collection.
-
.server_db ⇒ Object
Returns the value of attribute server_db.
Instance Attribute Summary collapse
-
#collection ⇒ Object
writeonly
Sets the attribute collection.
-
#database ⇒ Object
writeonly
Sets the attribute database.
-
#store ⇒ Object
writeonly
Sets the attribute store.
Instance Method Summary collapse
- #close ⇒ Object
-
#get(id) ⇒ Object
Returns a single document given its ID.
-
#insert(document) ⇒ Object
Inserts a brand new document into the database.
-
#remove(criteria = nil) ⇒ Object
Use this method to remove documents from your datastore.
-
#setup? ⇒ Boolean
A nice method to determine if there is enough information to potentially connect to the backing database.
-
#update(entry_id, field, value) ⇒ Object
A simple convienance method to update a single document in your datastore.
Class Attribute Details
.id_field ⇒ Object
Returns the value of attribute id_field.
21 22 23 |
# File 'lib/crocoduck/store.rb', line 21 def id_field @id_field end |
.server_cluster ⇒ Object
Returns the value of attribute server_cluster.
21 22 23 |
# File 'lib/crocoduck/store.rb', line 21 def server_cluster @server_cluster end |
.server_collection ⇒ Object
Returns the value of attribute server_collection.
21 22 23 |
# File 'lib/crocoduck/store.rb', line 21 def server_collection @server_collection end |
.server_db ⇒ Object
Returns the value of attribute server_db.
21 22 23 |
# File 'lib/crocoduck/store.rb', line 21 def server_db @server_db end |
Instance Attribute Details
#collection=(value) ⇒ Object
Sets the attribute collection
24 25 26 |
# File 'lib/crocoduck/store.rb', line 24 def collection=(value) @collection = value end |
#database=(value) ⇒ Object
Sets the attribute database
24 25 26 |
# File 'lib/crocoduck/store.rb', line 24 def database=(value) @database = value end |
#store=(value) ⇒ Object
Sets the attribute store
24 25 26 |
# File 'lib/crocoduck/store.rb', line 24 def store=(value) @store = value end |
Instance Method Details
#close ⇒ Object
34 35 36 |
# File 'lib/crocoduck/store.rb', line 34 def close store.close end |
#get(id) ⇒ Object
Returns a single document given its ID
48 49 50 51 52 |
# File 'lib/crocoduck/store.rb', line 48 def get(id) collection.find_one({ Crocoduck::Store.id_field => id.to_i }) end |
#insert(document) ⇒ Object
Inserts a brand new document into the database
64 65 66 |
# File 'lib/crocoduck/store.rb', line 64 def insert(document) collection.insert document end |
#remove(criteria = nil) ⇒ Object
Use this method to remove documents from your datastore. Cares has been taken to prevent accidental database destruction. Only pass {} to this method if you are 100% sure you want to clear the database.
58 59 60 61 |
# File 'lib/crocoduck/store.rb', line 58 def remove(criteria=nil) return if criteria.nil? collection.remove criteria end |
#setup? ⇒ Boolean
A nice method to determine if there is enough information to potentially connect to the backing database.
28 29 30 31 32 |
# File 'lib/crocoduck/store.rb', line 28 def setup? Crocoduck::Store.server_cluster && Crocoduck::Store.server_db && Crocoduck::Store.server_collection end |
#update(entry_id, field, value) ⇒ Object
A simple convienance method to update a single document in your datastore.
40 41 42 43 44 45 |
# File 'lib/crocoduck/store.rb', line 40 def update(entry_id, field, value) collection.update({ Crocoduck::Store.id_field => entry_id}, {'$set' => { field => value} }, :safe => true) end |