Class: RockingChair::Database
- Inherits:
-
Object
- Object
- RockingChair::Database
- Defined in:
- lib/rocking_chair/database.rb
Instance Attribute Summary collapse
-
#storage ⇒ Object
Returns the value of attribute storage.
Class Method Summary collapse
Instance Method Summary collapse
- #[](doc_id) ⇒ Object
- #[]=(doc_id, document, options = {}) ⇒ Object (also: #store)
- #all_documents(options = {}) ⇒ Object
- #bulk(documents) ⇒ Object
- #copy(original_id, new_id, rev = nil) ⇒ Object
- #delete(doc_id, rev) ⇒ Object
- #document_count ⇒ Object
- #exists?(doc_id) ⇒ Boolean
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #load(doc_id, options = {}) ⇒ Object
- #rev ⇒ Object
- #view(design_doc_name, view_name, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Database
Returns a new instance of Database.
8 9 10 |
# File 'lib/rocking_chair/database.rb', line 8 def initialize @storage = {} end |
Instance Attribute Details
#storage ⇒ Object
Returns the value of attribute storage.
6 7 8 |
# File 'lib/rocking_chair/database.rb', line 6 def storage @storage end |
Class Method Details
.uuid ⇒ Object
12 13 14 |
# File 'lib/rocking_chair/database.rb', line 12 def self.uuid UUIDTools::UUID.random_create().to_s.gsub('-', '') end |
.uuids(count) ⇒ Object
16 17 18 19 20 |
# File 'lib/rocking_chair/database.rb', line 16 def self.uuids(count) ids = [] count.to_i.times {ids << uuid} ids end |
Instance Method Details
#[](doc_id) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/rocking_chair/database.rb', line 30 def [](doc_id) if exists?(doc_id) return storage[doc_id] else RockingChair::Error.raise_404 end end |
#[]=(doc_id, document, options = {}) ⇒ Object Also known as: store
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rocking_chair/database.rb', line 62 def []=(doc_id, document, ={}) # options are ignored for now: batch, bulk begin document = normalize_payload(document) rescue Exception => e raise RockingChair::Error.new(500, 'InvalidJSON', "the document #{doc_id} is not a valid JSON or Hash object: #{e}") end if exists?(doc_id) update(doc_id, document) else insert(doc_id, document) end end |
#all_documents(options = {}) ⇒ Object
127 128 129 |
# File 'lib/rocking_chair/database.rb', line 127 def all_documents( = {}) View.run_all(self, ) end |
#bulk(documents) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rocking_chair/database.rb', line 103 def bulk(documents) response = [] documents = normalize_payload(documents) docs = documents[:docs] || documents['docs'] docs.each do |doc| begin if exists?(doc['_id']) && doc['_deleted'].to_s == 'true' self.delete(doc['_id'], doc['_rev']) state = {'id' => doc['_id'], 'rev' => doc['_rev']} else state = JSON.parse(self.store(doc['_id'], doc)) end response << {'id' => state['id'], 'rev' => state['rev']} rescue RockingChair::Error => e response << {'id' => doc['_id'], 'error' => e.error, 'reason' => e.reason} end end response.to_json end |
#copy(original_id, new_id, rev = nil) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rocking_chair/database.rb', line 92 def copy(original_id, new_id, rev=nil) original = JSON.parse(self[original_id], :create_additions => false) if rev original['_rev'] = rev else original.delete('_rev') end self.store(new_id, original) end |
#delete(doc_id, rev) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rocking_chair/database.rb', line 79 def delete(doc_id, rev) if exists?(doc_id) existing = self[doc_id] if matching_revision?(existing, rev) storage.delete(doc_id) else RockingChair::Error.raise_409 end else RockingChair::Error.raise_404 end end |
#document_count ⇒ Object
123 124 125 |
# File 'lib/rocking_chair/database.rb', line 123 def document_count storage.keys.size end |
#exists?(doc_id) ⇒ Boolean
26 27 28 |
# File 'lib/rocking_chair/database.rb', line 26 def exists?(doc_id) storage.has_key?(doc_id) end |
#load(doc_id, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rocking_chair/database.rb', line 38 def load(doc_id, = {}) = { 'rev' => nil, 'revs' => false }.update() .assert_valid_keys('rev', 'revs', 'revs_info') document = self[doc_id] if ['rev'] && ( JSON.parse(document, :create_additions => false)['_rev'] != ['rev']) RockingChair::Error.raise_404 end if ['revs'] && ['revs'] == 'true' json = JSON.parse(document, :create_additions => false) json['_revisions'] = {'start' => 1, 'ids' => [json['_rev']]} document = json.to_json end if ['revs_info'] && ['revs_info'] == 'true' json = JSON.parse(document, :create_additions => false) json['_revs_info'] = [{"rev" => json['_rev'], "status" => "disk"}] document = json.to_json end document end |
#rev ⇒ Object
22 23 24 |
# File 'lib/rocking_chair/database.rb', line 22 def rev self.class.uuid end |