Module: Exegesis::Document::InstanceMethods

Defined in:
lib/exegesis/document.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



69
70
71
# File 'lib/exegesis/document.rb', line 69

def == other
  self.id == other.id
end

#_attachments=(val) ⇒ Object



101
102
103
# File 'lib/exegesis/document.rb', line 101

def _attachments= val
  @attributes['_attachments'] = val
end

#attachmentsObject



97
98
99
# File 'lib/exegesis/document.rb', line 97

def attachments
  @attachments ||= Exegesis::Document::Attachments.new(self)
end

#idObject



73
74
75
# File 'lib/exegesis/document.rb', line 73

def id
  @attributes['_id']
end

#initialize(hash = {}, db = nil) ⇒ Object



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

def initialize hash={}, db=nil
  super hash
  @database = db || self.class.database
end

#reloadObject

Raises:



61
62
63
64
65
66
67
# File 'lib/exegesis/document.rb', line 61

def reload
  raise NewDocumentError if rev.nil? || id.nil?
  raise MissingDatabaseError if database.nil?
  @attachments = nil
  @references = nil
  @attributes = database.raw_get(id)
end

#revObject



77
78
79
# File 'lib/exegesis/document.rb', line 77

def rev
  @attributes['_rev']
end

#saveObject



81
82
83
84
85
86
87
88
89
# File 'lib/exegesis/document.rb', line 81

def save
  set_timestamps if respond_to?(:set_timestamps)
  if self.class.unique_id && id.nil?
    save_with_custom_unique_id
  else
    save_document
  end
  @attachments.clean! if @attachments && @attachments.dirty?
end

#to_jsonObject



105
106
107
# File 'lib/exegesis/document.rb', line 105

def to_json
  @attributes.merge({'_attachments' => @attachments}).to_json
end

#update_attributes(attrs = {}) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
# File 'lib/exegesis/document.rb', line 91

def update_attributes attrs={}
  raise ArgumentError, 'must include a matching _rev attribute' unless (rev || '') == (attrs.delete('_rev') || '')
  super attrs
  save
end

#uriObject



55
56
57
58
59
# File 'lib/exegesis/document.rb', line 55

def uri
  raise MissingDatabaseError if database.nil?
  raise NewDocumentError if rev.nil? || id.nil?
  "#{database.uri}/#{id}"
end