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/validation.rb,
lib/couch_potato/persistence/simple_property.rb,
lib/couch_potato/persistence/dirty_attributes.rb
Defined Under Namespace
Modules: Callbacks, DirtyAttributes, Json, Properties, Validation Classes: SimpleProperty
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#attributes ⇒ Object
returns all of a model’s attributes that have been defined using the #property method as a Hash.
-
#attributes=(hash) ⇒ Object
assign multiple attributes at once.
-
#initialize(attributes = {}) ⇒ Object
initialize a new instance of the model optionally passing it a hash of attributes.
-
#new? ⇒ Boolean
(also: #new_record?)
returns true if a model hasn’t been saved yet, false otherwise.
-
#to_param ⇒ Object
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.
Class Method Details
.included(base) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/couch_potato/persistence.rb', line 18 def self.included(base) base.send :include, Properties, Callbacks, Validation, Json, CouchPotato::View::CustomViews base.send :include, DirtyAttributes, GhostAttributes, Attachments base.send :include, MagicTimestamps base.class_eval do attr_accessor :_id, :_rev, :_deleted, :database alias_method :id, :_id end end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
93 94 95 |
# File 'lib/couch_potato/persistence.rb', line 93 def ==(other) #:nodoc: other.class == self.class && self.to_json == other.to_json end |
#attributes ⇒ Object
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}
73 74 75 76 77 78 |
# File 'lib/couch_potato/persistence.rb', line 73 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
57 58 59 60 61 |
# File 'lib/couch_potato/persistence.rb', line 57 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'
38 39 40 41 42 |
# File 'lib/couch_potato/persistence.rb', line 38 def initialize(attributes = {}) attributes.each do |name, value| self.send("#{name}=", value) end if attributes end |
#new? ⇒ Boolean Also known as: new_record?
returns true if a model hasn’t been saved yet, false otherwise
81 82 83 |
# File 'lib/couch_potato/persistence.rb', line 81 def new? _rev.nil? end |
#to_param ⇒ Object
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
89 90 91 |
# File 'lib/couch_potato/persistence.rb', line 89 def to_param _id end |