persistable.rb

Path: lib/couch_object/persistable.rb
Last Update: Wed Apr 30 00:28:14 +0200 2008
Enumerable Document StandardError HasManyAssociationError MissingView CantCompareSize DocumentNotFound BelongsToAssociationError HasOneAssociationError CouchDBError MapProcessError DatabaseSaveFailed NoDatabaseLocationSet Array HasManyRelation Response Database Server View Errors VERSION ClassMethods Persistable Utils CouchObject dot/f_3.png

Persistable

Introduction

The persistable mixin let‘s you save and load your classes to and from CoachDB using the instance method save and the class level method get_by_id. Documentation and examples can be found in the README file. The specs also contain lots of examples.

TODOs and known issues:

  • TODO: create a way to solve saving conflicts. Should also be possible to chose if only conflicts for certain variables should be handled.
  • TODO: If real world usage shows that it is needed then create a reload method that reloads the content from the db. Might be useful if some other process has changed the content of the document in which case the local copy can‘t be stored to the DB. Should local changes be overwritten? How should differences between the version on the server and the local version be handeled?
  • ISSUE: When creating a new document the server does not return other values than the new documents ID and REVISION number. The local representation of the created_at and updated_at attributes are therefore set right before the call to the server is issued. Their values are therefore not guaranteed to be completely in sync with the values stored on the server!
  • ISSUE: When loading multiple objects from a view using the get_from_view mehtod that normally would have been in a belongs_to or has_many relation, their relations aren‘t set.
  • ISSUE: Given the following scenario: B is in a has_many/has_one relationship to A and both have been saved to the database. B is removed from the relationship and A is then saved. Saving A wont save the changed version of B where the relationship has been ended. Therefore: the next time A is loaded from the database B will be loaded as a relative!

    Possible sollutions: 1) keep a list of objects that need to be saved in A so that B gets

        saved when A is saved althouh the relationship has ended.
    

    2) save B as it is removed from the relationship with A.

    I belive 1) to be the better solution as it doesn‘t prematurely save B in cases where you don‘t want the changes you do to be saved to the database. MAKE SURE: that the object that is added doesn‘t have the same id and revision number ass the object that is replaced. That happens when a class loads its belongs_to relations! In cases like that the old class should NOT be tracked and saved later!

Required files

persistable/has_many_relations_array   persistable/meta_classes   persistable/overloaded_methods  

[Validate]