Module: CouchTomato::Persistence::Base

Included in:
CouchTomato::Persistence, Nested
Defined in:
lib/couch_tomato/persistence/base.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

:nodoc:



56
57
58
# File 'lib/couch_tomato/persistence/base.rb', line 56

def ==(other) #:nodoc:
  other.class == self.class && self.to_json == other.to_json
end

#attributesObject

returns all of a model’s attributes that have been defined using the #property method as a Hash

example:

class Book
  include CouchTomato::Persistence
  property :title
  property :year
end
book = Book.new :year => 2009
book.attributes # => {:title => nil, :year => 2009}


49
50
51
52
53
54
# File 'lib/couch_tomato/persistence/base.rb', line 49

def attributes
  self.class.properties.inject({}) do |res, property|
    property.serialize(res, self)
    res
  end
end

#attributes=(hash) ⇒ Object

assign multiple attributes at once. the attributes have to be declared using the #property method

example:

class Book
  include CouchTomato::Persistence
  property :title
  property :year
end
book = Book.new
book.attributes = {:title => 'Time to Relax', :year => 2009}
book.title # => 'Time to Relax'
book.year # => 2009


33
34
35
36
37
# File 'lib/couch_tomato/persistence/base.rb', line 33

def attributes=(hash)
  hash.each do |attribute, value|
    self.send "#{attribute}=", value
  end
end

#initialize(attributes = {}) ⇒ Object

initialize a new instance of the model optionally passing it a hash of attributes. the attributes have to be declared using the #property method

example:

class Book
  include CouchTomato::Persistence
  property :title
end
book = Book.new :title => 'Time to Relax'
book.title # => 'Time to Relax'


14
15
16
17
18
# File 'lib/couch_tomato/persistence/base.rb', line 14

def initialize(attributes = {})
  attributes.each do |name, value|
    self.send("#{name}=", value)
  end if attributes
end