Module: CouchPotato::Persistence

Defined in:
lib/couch_potato/persistence.rb,
lib/couch_potato/persistence/json.rb,
lib/couch_potato/persistence/callbacks.rb,
lib/couch_potato/persistence/properties.rb,
lib/couch_potato/persistence/simple_property.rb,
lib/couch_potato/persistence/dirty_attributes.rb,
lib/couch_potato/persistence/belongs_to_property.rb

Defined Under Namespace

Modules: Callbacks, DirtyAttributes, Json, Properties Classes: BelongsToProperty, SimpleProperty

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/couch_potato/persistence.rb', line 15

def self.included(base)
  base.send :include, Properties, Callbacks, Validatable, Json, CouchPotato::View::CustomViews
  base.send :include, DirtyAttributes
  base.send :include, MagicTimestamps
  base.class_eval do
    attr_accessor :_id, :_rev, :_attachments, :_deleted, :database
    alias_method :id, :_id
  end
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



89
90
91
# File 'lib/couch_potato/persistence.rb', line 89

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 CouchPotato::Persistence
  property :title
  property :year
end
book = Book.new :year => 2009
book.attributes # => {:title => nil, :year => 2009}


70
71
72
73
74
75
# File 'lib/couch_potato/persistence.rb', line 70

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 CouchPotato::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


54
55
56
57
58
# File 'lib/couch_potato/persistence.rb', line 54

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 CouchPotato::Persistence
  property :title
end
book = Book.new :title => 'Time to Relax'
book.title # => 'Time to Relax'


35
36
37
38
39
# File 'lib/couch_potato/persistence.rb', line 35

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

#new?Boolean

returns true if a model hasn’t been saved yet, false otherwise

Returns:

  • (Boolean)


78
79
80
# File 'lib/couch_potato/persistence.rb', line 78

def new?
  _rev.nil?
end

#to_paramObject

returns the document id this is used by rails to construct URLs can be overridden to for example use slugs for URLs instead if ids



85
86
87
# File 'lib/couch_potato/persistence.rb', line 85

def to_param
  _id
end