Module: Exegesis::Document::InstanceMethods

Defined in:
lib/exegesis/document.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



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

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

#attachmentsObject



85
86
87
# File 'lib/exegesis/document.rb', line 85

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

#idObject



61
62
63
# File 'lib/exegesis/document.rb', line 61

def id
  @attributes['_id']
end

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



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

def initialize hash={}, db=nil
  super hash
  @database = db
end

#reloadObject

Raises:



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

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



65
66
67
# File 'lib/exegesis/document.rb', line 65

def rev
  @attributes['_rev']
end

#saveObject



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

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



89
90
91
# File 'lib/exegesis/document.rb', line 89

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

#update_attributes(attrs = {}) ⇒ Object

Raises:

  • (ArgumentError)


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

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

#uriObject



43
44
45
46
47
# File 'lib/exegesis/document.rb', line 43

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