Class: SimplyCouch::Model::DatabaseInstance
- Inherits:
-
Object
- Object
- SimplyCouch::Model::DatabaseInstance
- Defined in:
- lib/simply_couch/model/database.rb
Overview
Renamed from CompatibilityNote — this is the actual Database class (separate from the Database module above to avoid naming conflict)
Instance Attribute Summary collapse
-
#couchrest_database ⇒ Object
readonly
Returns the value of attribute couchrest_database.
Instance Method Summary collapse
- #bulk_load(ids) ⇒ Object
- #delete_document(document) ⇒ Object
- #destroy_document(document, run_callbacks = true) ⇒ Object
- #first(spec) ⇒ Object
-
#initialize(couchrest_database) ⇒ DatabaseInstance
constructor
A new instance of DatabaseInstance.
- #load_document(id) ⇒ Object
- #save_document(document, validate = true) ⇒ Object
- #save_document!(document) ⇒ Object
- #view(spec) ⇒ Object
Constructor Details
#initialize(couchrest_database) ⇒ DatabaseInstance
Returns a new instance of DatabaseInstance.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/simply_couch/model/database.rb', line 76 def initialize(couchrest_database) if couchrest_database.is_a?(String) # URL string — create CouchRest database @couchrest_database = CouchRest.database(couchrest_database) elsif couchrest_database.nil? @couchrest_database = CouchRest.database('http://127.0.0.1:5984') else @couchrest_database = couchrest_database end end |
Instance Attribute Details
#couchrest_database ⇒ Object (readonly)
Returns the value of attribute couchrest_database.
74 75 76 |
# File 'lib/simply_couch/model/database.rb', line 74 def couchrest_database @couchrest_database end |
Instance Method Details
#bulk_load(ids) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/simply_couch/model/database.rb', line 148 def bulk_load(ids) response = couchrest_database.bulk_load ids docs = response['rows'].map{|row| row["doc"]}.compact docs.each{|doc| doc.database = self if doc.respond_to?(:database=) } docs end |
#delete_document(document) ⇒ Object
155 156 157 |
# File 'lib/simply_couch/model/database.rb', line 155 def delete_document(document) couchrest_database.delete_doc document.to_hash end |
#destroy_document(document, run_callbacks = true) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/simply_couch/model/database.rb', line 134 def destroy_document(document, run_callbacks = true) if run_callbacks document.run_callbacks :destroy do document._deleted = true couchrest_database.delete_doc document.to_hash end else document._deleted = true couchrest_database.delete_doc document.to_hash end document._id = nil document._rev = nil end |
#first(spec) ⇒ Object
106 107 108 109 |
# File 'lib/simply_couch/model/database.rb', line 106 def first(spec) spec.view_parameters = spec.view_parameters.merge({:limit => 1}) view(spec).first end |
#load_document(id) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/simply_couch/model/database.rb', line 127 def load_document(id) raise "Can't load a document without an id (got nil)" if id.nil? instance = couchrest_database.get(id) instance.database = self if instance.respond_to?(:database=) instance end |
#save_document(document, validate = true) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/simply_couch/model/database.rb', line 111 def save_document(document, validate = true) begin if document.new? create_document(document, validate) else update_document(document, validate) end rescue CouchRest::Conflict raise SimplyCouch::Conflict.new end end |
#save_document!(document) ⇒ Object
123 124 125 |
# File 'lib/simply_couch/model/database.rb', line 123 def save_document!(document) save_document(document) || raise("Validations failed: #{document.errors.}") end |
#view(spec) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/simply_couch/model/database.rb', line 87 def view(spec) results = View::ViewQuery.new( couchrest_database, spec.design_document, {spec.view_name => { :map => spec.map_function, :reduce => spec.reduce_function }}, (spec.list_name ? {spec.list_name => spec.list_function} : nil), spec.lib, spec.language ).query_view!(spec.view_parameters) processed_results = spec.process_results results processed_results.each do |document| document.database = self if document.respond_to?(:database=) end if processed_results.respond_to?(:each) processed_results end |