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

Defined Under Namespace

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)

:nodoc:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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, GhostAttributes, Attachments
  base.send :include, MagicTimestamps, ActiveModelCompliance, ForbiddenAttributesProtection
  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

- (Object) ==(other)

:nodoc:



129
130
131
# File 'lib/couch_potato/persistence.rb', line 129

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

- (Object) [](attribute)



108
109
110
# File 'lib/couch_potato/persistence.rb', line 108

def [](attribute)
  send(attribute)
end

- (Object) []=(attribute, value)



104
105
106
# File 'lib/couch_potato/persistence.rb', line 104

def []=(attribute, value)
  send "#{attribute}=", value
end

- (Object) attributes

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}


97
98
99
100
101
102
# File 'lib/couch_potato/persistence.rb', line 97

def attributes
  self.class.properties.inject(ActiveSupport::HashWithIndifferentAccess.new) do |res, property|
    property.value(res, self)
    res
  end
end

- (Object) attributes=(hash)

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


81
82
83
84
85
# File 'lib/couch_potato/persistence.rb', line 81

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

- (Boolean) eql?(other)



133
134
135
# File 'lib/couch_potato/persistence.rb', line 133

def eql?(other)
  self == other
end

- (Boolean) has_key?(key)



112
113
114
# File 'lib/couch_potato/persistence.rb', line 112

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

- (Object) hash



137
138
139
# File 'lib/couch_potato/persistence.rb', line 137

def hash
  _id.hash * (_id.hash.to_s.size ** 10) + _rev.hash
end

- (Object) initialize(attributes = {}) {|_self| ... }

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:



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

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

- (Object) inspect



146
147
148
149
# File 'lib/couch_potato/persistence.rb', line 146

def inspect
  attributes_as_string = attributes.map {|attribute, value| "#{attribute}: #{value.inspect}"}.join(", ")
  %Q{#<#{self.class} _id: "#{_id}", _rev: "#{_rev}", #{attributes_as_string}>}
end

- (Boolean) new? Also known as: new_record?

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



117
118
119
# File 'lib/couch_potato/persistence.rb', line 117

def new?
  _rev.nil?
end

- (Object) reload

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



142
143
144
# File 'lib/couch_potato/persistence.rb', line 142

def reload
  database.load id
end

- (Object) to_param

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



125
126
127
# File 'lib/couch_potato/persistence.rb', line 125

def to_param
  _id
end