Module CouchObject::Persistable
In: lib/couch_object/persistable.rb
lib/couch_object/persistable/has_many_relations_array.rb
lib/couch_object/persistable/meta_classes.rb
lib/couch_object/persistable/overloaded_methods.rb
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

Methods

Classes and Modules

Module CouchObject::Persistable::ClassMethods
Class CouchObject::Persistable::HasManyRelation

External Aliases

clone -> couch_object_origianl_clone
  When saved, clones and dupes are stored as separate documents from the object it originated from

Returns:

  • A new instance of itself where the ID and revision number is set to nil
  • the new instance shares the same storage location as the object it originates from.

Attributes

created_at  [R]  Accessors for instance variables specific to the persistable CouchObject
id  [R]  Accessors for instance variables specific to the persistable CouchObject
revision  [R]  Accessors for instance variables specific to the persistable CouchObject
updated_at  [R]  Accessors for instance variables specific to the persistable CouchObject

Public Class methods

Adds timestamps to the class.

Example:

  class Vacation
    include CouchObject::Persistable
    add_timestamp_for :on_create, :on_update
  end

  my_vacation = Vacation.new
  my_vacation.save(db_address)
  my_vacation.created_at => Somedate
  my_vacation.updated_at => Somedate

Defines a belongs_to relation which then again needs a corresponding has_many relation in the class the relation is made with (see the documentation of has_many above)

Takes:

  • a symbol indicating the name of the association. The association name can be freely chosen.
  • a symbol that indicates the name the corresponding has_many relationship in the owner class

Example:

  belongs_to :fruit_basket, :as => :fruits

  Requires a has_many relation from the other class that looks
        something like this:

    has_many :fruits

Raises:

  • BelongsToAssociationError if the association name,

or the :as parameter is left blank.

Timestamps are false by default

Sets the location of the database to use by default

Example:

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

  apple_tree = AppleTree.new
  apple_tree.save # saves automatically to the predefined
                  # database location

Smart save can also be used on a per-case basis if it is sometimes needed and sometimes not.

Example:

user_without_smart_save_1 = User.get("foo") User.smart_save user_with_smart_save = User.get("bar") User.deactivate_smart_save user_without_smart_save_2 = User.get("bong")

Defines a has_many relation which then again needs a corresponding belongs_to relation in the classes the relation is made with (see the documentation of belongs_to below)

Takes:

  • a symbol indicating the name of the association. The association name can be freely chosen.

Example:

  has_many :fruits

  Requires a belongs_to relation from the other part. F.ex:

    belongs_to: :fruit_basket, :as => :fruits

Raises:

  • HasManyAssociationError if the association name is left blank.

has_one relations are added as a layer to the has_many There is created a has_many relation ship but getters and setters for the has_one relationship on top of that that interact with the has_many relationship.

Location methods are added both as instance methods and as class level methods. The class level methods are needed when loading new objects from the database and the instance methods are used throughout the class

Smart save (defaults to false), if activated, keeps a snapshot of the objects initial state and evaluates if the class needs to be saved to the database by comparing it to the snapshot when a save is requested.

Please notice: Only activate this feature in cases where it is needed. It might slow down the performance of your app if you activate it for classes that you need many instances of and that you won‘t call the save method on after having loaded them from the database. Please also bare in mind that the class instance will store an extra copy of its contents which will lead to quite a big memory overhead for classes that store a lot of data!

Public Instance methods

<= and >= Uses the results from the methods ==, < and > to determine the result

Takes:

  • other_object to compare size with

Raises:

  • CantCompareSize when trying to compare two object that have not been saved or saved but do not have timestamps and that are not equal

Returns:

  • true or false

Equality is checked for using the following rules:

  • if the object types do not match the result is false
  • if the object_id of the class sent as an argument is the same as the object_id of self, equal returns true.
  • if the two objects have a different class type it fails as a result of the object_id test above which can‘t be true
  • fails if the object_id or revision numbers are different
  • it fails if one or both of the objects are new and they don‘t share the object_id, because in that case they will be stored as separate documents in the database
  • is true if all instance_variables are the same

Takes:

  • other_object which is any object one wishes to compare self to

Returns:

  • true or false

Goal: The goal is to return true for the object which has the newest representation in the database.

Requires:

  • that both objects have timestamps

Returns:

  • false if they are equal objects.
  • true if self has been saved while the other_object has not.
  • false if self hasn‘t been saved but the other_object has
  • true if both have been saved and self has been saved more recently.
  • false if both have been saved but the other object more recently.

Takes:

  • other_object to compare size with

Raises:

  • CantCompareSize when trying to compare two object that have not been saved or saved but do not have timestamps

Returns:

  • true or false

This method is called from the method that assigns a belongs_to relation to inform the master object of the relation (has_many relations)

Forces the instance object into smart save mode

Stores the initial value of the instance to a variable for later reference by the +unsaved_changes?+ method

Returns the name of the relation in itself matching one of the relations in the other object

Example:

  other_object has defined the relationships:
    belongs_to :house, :as => :houses
    belongs_to :humanity

  self has the relation
    has_many :houses

  :houses is returned

Any instance should be able to delete itself

Takes:

  • db_uri if not set in the location variable

Returns:

  • true on success
  • false on failure

Note:

  • it also deletes all has_many relations from the database
  • it removes itself from object it belongs to

Raises:

do_load_belongs_to_relation()
do_load_has_one_relation()
do_load_has_one_relations()

If you need to access the belongs_to variable without loading the relation if it hasn‘t already been loaded, you can call the instance method do_not_load_belongs_to_relations. To reactivate loading so the relation is loaded the next time it is needed, call the instance method do_load_belongs_to_relations.

do_not_load_has_many_relation()

Sometimes you might want to add an object to a has_many relation without interacting with the other relations at all. In cases like that, when loading all the relations would just cause unnecessary traffic to the database, you can tell the object not to load it has_many relations using this method

do_not_load_has_one_relation()
do_not_load_has_one_relations()

This method is called from the method that assigns a belongs_to relation to inform the previous master object that the relation ship has ended (has_one relations)

Breaks relations if existing

Takes:

  • undesired_object as a reference to the object the relationship should be broken with
  • which_is_stored_as (string) which is what the relation is stored as.

Default values for has_many, belongs_to and belongs_to_as

Returns:

  • true if the object hasn‘t been saved
  • false if the object has previously been stored or is loaded from the document store

Saves the object to the db_uri supplied, or if not set, to the location the object has previously been saved to.

Takes:

Raises:

Returns:

  • Hash with the id and revision: {:id => "1234", :revision => "ABC123"}

Sub methods might raise:

This method is called from the method that assigns a belongs_to relation to inform the master object of the relation (has_one relations)

Sets the location variable manually

Takes:

set_storage_location=(db_uri)

Alias for set_location=

storage_location()

Alias for location

serializes this object into JSON

Returns:

  • The values of the class in json format

    Example {"class":"Bike","attributes":{"wheels":2}}

Classes WITH smart_save activated: Any instance should be able to know if it has unsaved changes or not. When an instance is loaded from the DB it creates a snapshot of what its variables contain. Based on a comparison between the snapshot and the contents of the instance this method returns true or false.

Classes WITHOUT smart_save activated: Will always return true regardless of what state it is in

A new object will always return true

Returns:

  • true: if it has changes that haven‘t been saved to the database
  • fase: if the nothing has changed since it was loaded from the database.

Smart savign

Protected Instance methods

Saves the class as a new document in the database

Returns:

Raises:

Loads the belongs_to relation if the class has a previously save relation

Updates a document in the database based on the id and revision

Returns:

Raises:

Performs callbacks before and after these events:

[Validate]