Module: CouchObject::Persistable::ClassMethods

Defined in:
lib/couch_object/persistable.rb

Instance Method Summary collapse

Instance Method Details

#get_by_id(id, db_uri = self.location) ⇒ Object Also known as: get

Loads a document from the database

Aliases:

  • get

Takes:

  • id: the ID of the document that should be loaded

  • db_uri: the uri to the database. Is optional if the database has been defined on class level:

    class SomeClass
      include CouchObject::Persistable
      database 'http://localhost:5984'
    end
    

Returns:

  • a fully initialized class of type self

Raises:

  • CouchObject::Errors::NoDatabaseLocationSet if db_uri is blank AND has not been set on class level

  • CouchObject::Errors::DocumentNotFound if the document doesn’t exist or has been deleted



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/couch_object/persistable.rb', line 90

def get_by_id(id, db_uri = self.location)
  # Raises an error if the location variable hasn't been set
  raise CouchObject::Errors::NoDatabaseLocationSet unless db_uri
  
  db = CouchObject::Database.open(db_uri)
  response = JSON.parse(db.get(id).body)
  
  if response["error"]
    case response["reason"]
    when "deleted"
      raise CouchObject::Errors::DocumentNotFound, "The document has been deleted"
    else  
      raise CouchObject::Errors::DocumentNotFound, "The document could not be found"
    end
  end
          
  # creates a new object and initialize all its sub objects
  new_object = couch_load_object(response)
  
  # set the storage location it was loaded from so it can be saved 
  # back directly without having to supply the db_uri again
  new_object.instance_variable_set("@location", db_uri)

  # return the new couch object
  new_object
end

#get_from_view(view, params = {:key => nil, :update => true, :db_uri => self.location}) ⇒ Object

Loads all document from a given view from the database

Takes:

  • view (string): the name of the view to call

  • params

    (hash): a hash of URL query arguments supported

    by couchDB. If omitted it defaults to not use a key and not update the view. Additionally the db_uri can be set as a parameter if it hasn’t been defined at class level.

    Example:

    AppleTree.get_from_view("foo_view",
      { :db_uri => "http://localhost:5984/mydb",
        :update => false, :key => "bar"}) => Array
    

Returns:

  • a array of initialized classes (if the view includes the documents full content)

Raises:

  • CouchObject::Errors::NoDatabaseLocationSet if db_uri is blank AND has not been set on class level

  • CouchObject::Errors::MissingView if the view doesn’t exist



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/couch_object/persistable.rb', line 168

def get_from_view( view, params = {:key => nil,
                                   :update => true,
                                   :db_uri => self.location})

  # Raise an error if the location variable hasn't been set
  db_uri = params[:db_uri] || self.location
  raise CouchObject::Errors::NoDatabaseLocationSet unless db_uri

  db = CouchObject::Database.open(db_uri)

  params.delete(:db_uri)

  #Create a querystring with the parameters passed inn
  querystring = "?"
  params.each_pair do |key, value|
    querystring += \
      "#{key}=#{Utils.encode_querystring_parameter(value)}&"
  end
  querystring = querystring[0...-1]

  view_with_parameters = view + querystring

  objects_to_return = []

  response = JSON.parse(db.get(view_with_parameters).body)
  
  raise CouchObject::Errors::MissingView, \
    "The view '#{view}' doesn't exist on the server" \
    if response["error"] == "not_found"

  raise CouchObject::Errors::CouchDBError, \
    "CouchDB returned and error and described the problem as #{response['reason']}. \n" + \
    "There might be something wrong with one of your views, or it might be missing!" \
    if response["error"]
  
  response["rows"].each do |params_for_object|
    objects_to_return << couch_load_object(params_for_object["value"])
  end
  
  objects_to_return

end

#get_with_smart_save(id, db_uri = self.location) ⇒ Object

Takes, returns and raises the same things as get_by_id

Creates a new object that is forced into smart save mode although the class it is stemming from might not have smart saving enabled.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/couch_object/persistable.rb', line 129

def get_with_smart_save(id, db_uri = self.location)
  
  new_object = self.get_by_id(id, db_uri)

  # Force it into smart save mode
  new_object.couch_force_smart_save
  
  # Initialize the original state.
  new_object.couch_set_initial_state
  
  new_object
end