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/revisions.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/deep_dirty_attributes.rb,
lib/couch_potato/persistence/deep_tracked_property.rb,
lib/couch_potato/persistence/active_model_compliance.rb

Defined Under Namespace

Modules: ActiveModelCompliance, Callbacks, DeepDirtyAttributes, DirtyAttributes, Json, Properties, PropertyMethods, Revisions Classes: DeepTrackedProperty, SimpleProperty, TypeCaster

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/couch_potato/persistence.rb', line 22

def self.included(base) #:nodoc:
  base.send :include, Properties, Callbacks, Json, CouchPotato::View::CustomViews,
    CouchPotato::View::Lists
  base.send :include, DirtyAttributes, Attachments
  base.send :include, MagicTimestamps, ActiveModelCompliance,
    ForbiddenAttributesProtection, Revisions
  base.send :include, Validation
  base.class_eval do
    attr_accessor :_id, :_rev, :_deleted, :database
    alias_method :id, :_id
    alias_method :id=, :_id=

    def self.inherited(child)
      super
      CouchPotato.models << child
    end
  end

  CouchPotato.models << base
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



131
132
133
# File 'lib/couch_potato/persistence.rb', line 131

def ==(other) #:nodoc:
  super || (self.class == other.class && self._id.present? && self._id == other._id)
end

#[](attribute) ⇒ Object



110
111
112
# File 'lib/couch_potato/persistence.rb', line 110

def [](attribute)
  send(attribute)
end

#[]=(attribute, value) ⇒ Object



106
107
108
# File 'lib/couch_potato/persistence.rb', line 106

def []=(attribute, value)
  send "#{attribute}=", value
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}


99
100
101
102
103
104
# File 'lib/couch_potato/persistence.rb', line 99

def attributes
  self.class.properties.inject(ActiveSupport::HashWithIndifferentAccess.new) 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


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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/couch_potato/persistence.rb', line 135

def eql?(other)
  self == other
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/couch_potato/persistence.rb', line 114

def has_key?(key)
  attributes.has_key?(key)
end

#hashObject



139
140
141
# File 'lib/couch_potato/persistence.rb', line 139

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'

Yields:

  • (_self)

Yield Parameters:



61
62
63
64
65
66
67
68
# File 'lib/couch_potato/persistence.rb', line 61

def initialize(attributes = {})
  if attributes
    @skip_dirty_tracking = true
    self.attributes = attributes
    @skip_dirty_tracking = false
  end
  yield self if block_given?
end

#inspectObject



148
149
150
151
# File 'lib/couch_potato/persistence.rb', line 148

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/couch_potato/persistence.rb', line 119

def new?
  _rev.nil?
end

#reloadObject

Returns a reloaded instance. Does not touch the original instance.



144
145
146
# File 'lib/couch_potato/persistence.rb', line 144

def reload
  database.load id
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



127
128
129
# File 'lib/couch_potato/persistence.rb', line 127

def to_param
  _id
end