Class: WingedCouch::Native::Document

Inherits:
Abstract::Document show all
Defined in:
lib/winged_couch/native/document.rb

Overview

Low-level class for working with documents

Direct Known Subclasses

Design::Document

Instance Attribute Summary

Attributes inherited from Abstract::Document

#data, #database

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract::Document

#==, #_id, #_id=, #_rev, #_rev=, #initialize, #inspect

Constructor Details

This class inherits a constructor from WingedCouch::Abstract::Document

Class Method Details

.find(database, document_id) ⇒ WingedCouch::Native::Document

Finds document in the database by id

Parameters:

  • database (WingedCouch::Database::Database)
  • document_id (String)

Returns:


75
76
77
78
# File 'lib/winged_couch/native/document.rb', line 75

def self.find(database, document_id)
  data = { _id: document_id }
  self.new(database, data).reload
end

Instance Method Details

#deleteObject

Removed document from the database

Returns:

  • true

Raises:


54
55
56
57
# File 'lib/winged_couch/native/document.rb', line 54

def delete
  HTTP.delete(path, default_params)
  @data["_rev"] = nil
end

#errorsObject

Returns raw errors from CouchDB


84
85
86
# File 'lib/winged_couch/native/document.rb', line 84

def errors
  @errors ||= []
end

#exist?true, false

Returns true if document exist

Returns:

  • (true, false)

16
17
18
19
20
21
# File 'lib/winged_couch/native/document.rb', line 16

def exist?
  HTTP.head path
  true
rescue Exceptions::DocumentMissing
  false
end

#fetch_revision!Object


8
9
10
# File 'lib/winged_couch/native/document.rb', line 8

def fetch_revision!
  self._rev = get["_rev"]
end

#pathObject

Returns database path to the document


92
93
94
# File 'lib/winged_couch/native/document.rb', line 92

def path
  database.path.join(_id, :document)
end

#reloadWingedCouch::Native::Document

Reloads document with latest data from database

Returns:


43
44
45
46
# File 'lib/winged_couch/native/document.rb', line 43

def reload
  @data = get.with_indifferent_access
  self
end

#savetrue, false

Saves document in the database

Returns:

  • (true, false)

    result of saving document


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/winged_couch/native/document.rb', line 27

def save
  begin
    @errors = []
    response = HTTP.put(path, data)
    @data[:_rev] = response["rev"]
    true
  rescue Exceptions::InvalidDocument => e
    errors << e.message
    false
  end
end

#update(data = {}) ⇒ WingedCouch::Native::Document

Updates document with passed data

Returns:


63
64
65
66
# File 'lib/winged_couch/native/document.rb', line 63

def update(data = {})
  @data.merge!(data)
  save
end