Class: CouchDB::Document

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

Overview

Base is the main super class of all models that should be stored in CouchDB. See the README file for more information.

Direct Known Subclasses

Design

Defined Under Namespace

Classes: NotFoundError, UnauthorizedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, properties = { }) ⇒ Document

Returns a new instance of Document.



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

def initialize(database, properties = { })
  @database = database
  @properties = properties
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (private)



134
135
136
# File 'lib/couchdb/document.rb', line 134

def method_missing(method_name, *arguments, &block)
  @properties.send method_name, *arguments, &block
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



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

def database
  @database
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/couchdb/document.rb', line 62

def ==(other)
  self.id == other.id
end

#[](key) ⇒ Object



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

def [](key)
  @properties[key.to_s]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @properties[key.to_s] = value
end

#clear_revObject



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

def clear_rev
  @properties.delete "_rev"
end

#destroyObject



90
91
92
93
94
95
96
97
# File 'lib/couchdb/document.rb', line 90

def destroy
  return false if new?
  Transport::JSON.request :delete, url, authentication_options.merge(:headers => { "If-Match" => self.rev }, :expected_status_code => 200)
  self.clear_rev
  true
rescue Transport::UnexpectedStatusCodeError => error
  upgrade_status_error error
end

#exists?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/couchdb/document.rb', line 70

def exists?
  Transport::JSON.request :get, url, authentication_options.merge(:expected_status_code => 200)
  true
rescue Transport::UnexpectedStatusCodeError => error
  raise error unless error.status_code == 404
  false
end

#fetch_revObject



54
55
56
57
58
59
60
# File 'lib/couchdb/document.rb', line 54

def fetch_rev
  properties = Transport::JSON.request :get, url, authentication_options.merge(:expected_status_code => 200)
  self.rev = properties["_rev"]
rescue Transport::UnexpectedStatusCodeError => error
  raise error unless error.status_code == 404
  @properties.delete "_rev"
end

#idObject



30
31
32
# File 'lib/couchdb/document.rb', line 30

def id
  self["_id"]
end

#id=(value) ⇒ Object



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

def id=(value)
  self["_id"] = value
end

#loadObject Also known as: reload



78
79
80
81
82
83
# File 'lib/couchdb/document.rb', line 78

def load
  @properties = Transport::JSON.request :get, url, authentication_options.merge(:expected_status_code => 200)
  true
rescue Transport::UnexpectedStatusCodeError => error
  upgrade_status_error error
end

#new?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/couchdb/document.rb', line 66

def new?
  !self.rev?
end

#revObject



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

def rev
  self["_rev"]
end

#rev=(value) ⇒ Object



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

def rev=(value)
  self["_rev"] = value
end

#rev?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/couchdb/document.rb', line 46

def rev?
  @properties.has_key? "_rev"
end

#saveObject



86
87
88
# File 'lib/couchdb/document.rb', line 86

def save
  new? ? create : update
end

#urlObject



99
100
101
# File 'lib/couchdb/document.rb', line 99

def url
  "#{self.database.url}/#{self.id}"
end