Class: CouchDB::Document

Inherits:
JSONObject show all
Defined in:
lib/couchdb/document.rb

Instance Attribute Summary collapse

Attributes inherited from JSONObject

#errors

Instance Method Summary collapse

Methods inherited from JSONObject

#[]=, dynamic_structure!, dynamic_structure?, fixed_structure!, fixed_structure?, #inspect, lookup, #merge, #merge!, properties, property, #replace, #store, #update, #valid?, #validate!

Constructor Details

#initialize(db, attributes = nil) ⇒ Document

Returns a new instance of Document.



15
16
17
18
19
# File 'lib/couchdb/document.rb', line 15

def initialize(db, attributes = nil)
  super attributes
  @db = db
  send :after_initialize if respond_to?(:after_initialize)
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



13
14
15
# File 'lib/couchdb/document.rb', line 13

def db
  @db
end

Instance Method Details

#_idObject Also known as: id



27
28
29
# File 'lib/couchdb/document.rb', line 27

def _id
  self['_id']
end

#_revObject Also known as: rev



21
22
23
# File 'lib/couchdb/document.rb', line 21

def _rev
  self['_rev']
end

#delete!Object

Raises:



48
49
50
51
52
# File 'lib/couchdb/document.rb', line 48

def delete!
  raise InvalidOperation, "Can not delete a document without _id or _rev." unless id and rev
  send :before_delete if respond_to?(:before_delete)
  db.delete id, rev
end

#new_record?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/couchdb/document.rb', line 33

def new_record?
  _id.nil?
end

#saveObject



37
38
39
40
# File 'lib/couchdb/document.rb', line 37

def save
  send :before_save if respond_to?(:before_save)
  replace db.put(self)
end

#update!(attributes) ⇒ Object



42
43
44
45
46
# File 'lib/couchdb/document.rb', line 42

def update!(attributes)
  send :before_update if respond_to?(:before_update)
  update attributes
  save
end