Module: WingedCouch::Models::Persistence

Included in:
WingedCouch::Model
Defined in:
lib/winged_couch/models/persistence.rb

Overview

Main module for storing records in CouchDB

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object


14
15
16
# File 'lib/winged_couch/models/persistence.rb', line 14

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#deleteObject

Deletes record from the database


72
73
74
75
76
# File 'lib/winged_couch/models/persistence.rb', line 72

def delete
  run_hooks(:before, :delete)
  native_document.delete
  run_hooks(:after, :delete)
end

#persisted?Boolean

Returns true if object is persisted

Returns:

  • (Boolean)

93
94
95
# File 'lib/winged_couch/models/persistence.rb', line 93

def persisted?
  !!(_id && _rev)
end

#savetrue, false

Saves object to database or updates it if it was stored before

Examples:

profile = Profile.new(name: "Name")
profile.save
# => true

profile = Profile.new
profile.save
# => false
profile.errors
# => ["name can't be blank"]

Returns:

  • (true, false)

    result of string

See Also:

  • #errors

59
60
61
62
63
64
65
66
67
68
# File 'lib/winged_couch/models/persistence.rb', line 59

def save
  native_document._id ||= SecureRandom.hex
  if self.class.bulk?
    self.class.bulk_records << self
    :bulk
  else
    run_hooks(:before, :save)
    native_document.save && run_hooks(:after, :save)
  end
end

#update(data) ⇒ Object

Updates record with passed data

Examples:

profile.update(name: "New Name")

Parameters:

  • data (Hash)

    in format { attribute name => new value }


85
86
87
88
89
# File 'lib/winged_couch/models/persistence.rb', line 85

def update(data)
  run_hooks(:before, :update)
  native_document.update(data)
  run_hooks(:after, :update)
end