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/type_caster.rb,
lib/couch_potato/persistence/simple_property.rb,
lib/couch_potato/persistence/dirty_attributes.rb,
lib/couch_potato/persistence/active_model_compliance.rb
Defined Under Namespace
Modules: ActiveModelCompliance, Callbacks, DirtyAttributes, Json, Properties, PropertyMethods Classes: SimpleProperty, TypeCaster
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
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.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(attributes = {}) {|_self| ... } ⇒ Object
initialize a new instance of the model optionally passing it a hash of attributes.
- #inspect ⇒ Object
-
#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
:nodoc:
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/couch_potato/persistence.rb', line 20 def self.included(base) #:nodoc: base.send :include, Properties, Callbacks, Json, CouchPotato::View::CustomViews, CouchPotato::View::Lists base.send :include, DirtyAttributes, GhostAttributes, Attachments base.send :include, MagicTimestamps, ActiveModelCompliance base.send :include, Validation base.class_eval do attr_accessor :_id, :_rev, :_deleted, :database alias_method :id, :_id alias_method :id=, :_id= end end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
107 108 109 |
# File 'lib/couch_potato/persistence.rb', line 107 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}
87 88 89 90 91 92 |
# File 'lib/couch_potato/persistence.rb', line 87 def attributes self.class.properties.inject({}) do |res, property| property.value(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
71 72 73 74 75 |
# File 'lib/couch_potato/persistence.rb', line 71 def attributes=(hash) hash.each do |attribute, value| self.send "#{attribute}=", value end end |
#eql?(other) ⇒ Boolean
111 112 113 |
# File 'lib/couch_potato/persistence.rb', line 111 def eql?(other) self == other end |
#hash ⇒ Object
115 116 117 |
# File 'lib/couch_potato/persistence.rb', line 115 def hash _id.hash * (_id.hash.to_s.size ** 10) + _rev.hash end |
#initialize(attributes = {}) {|_self| ... } ⇒ 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. the new model will be yielded to an optionally given block.
example:
class Book
include CouchPotato::Persistence
property :title
end
book = Book.new :title => 'Time to Relax'
OR
book = Book.new do |b|
b.title = 'Time to Relax'
end
book.title # => 'Time to Relax'
49 50 51 52 53 54 55 56 |
# File 'lib/couch_potato/persistence.rb', line 49 def initialize(attributes = {}) if attributes attributes.each do |name, value| self.send("#{name}=", value) end end yield self if block_given? end |
#inspect ⇒ Object
119 120 121 122 |
# File 'lib/couch_potato/persistence.rb', line 119 def inspect attributes_as_string = attributes.map {|attribute, value| "#{attribute}: #{value.inspect}"}.join(", ") %Q{#<#{self.class} _id: "#{_id}", _rev: "#{_rev}", #{attributes_as_string}>} end |
#new? ⇒ Boolean Also known as: new_record?
returns true if a model hasn’t been saved yet, false otherwise
95 96 97 |
# File 'lib/couch_potato/persistence.rb', line 95 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
103 104 105 |
# File 'lib/couch_potato/persistence.rb', line 103 def to_param _id end |