lib/exegesis/document.rb in mattly-exegesis-0.2.0 vs lib/exegesis/document.rb in mattly-exegesis-0.2.1

- old
+ new

@@ -1,8 +1,13 @@ module Exegesis module Document + autoload :Attachments, 'exegesis/document/attachments' + autoload :Attachment, 'exegesis/document/attachment' + class MissingDatabaseError < StandardError; end + class NewDocumentError < StandardError; end + def self.included base base.send :include, Exegesis::Model base.extend ClassMethods base.send :include, InstanceMethods base.send :attr_accessor, :database @@ -33,10 +38,24 @@ def initialize hash={}, db=nil super hash @database = db end + def uri + raise MissingDatabaseError if database.nil? + raise NewDocumentError if rev.nil? || id.nil? + "#{database.uri}/#{id}" + end + + def reload + raise NewDocumentError if rev.nil? || id.nil? + raise MissingDatabaseError if database.nil? + @attachments = nil + @references = nil + @attributes = database.raw_get(id) + end + def == other self.id == other.id end def id @@ -52,22 +71,31 @@ if self.class.unique_id && id.nil? save_with_custom_unique_id else save_document end + @attachments.clean! if @attachments && @attachments.dirty? end def update_attributes attrs={} - raise ArgumentError, 'must include a matching _rev attribute' unless rev == attrs.delete('_rev') + raise ArgumentError, 'must include a matching _rev attribute' unless (rev || '') == (attrs.delete('_rev') || '') super attrs save end + def attachments + @attachments ||= Exegesis::Document::Attachments.new(self) + end + + def to_json + @attributes.merge({'_attachments' => @attachments}).to_json + end + private def save_document raise ArgumentError, "canont save without a database" unless database - database.save self.attributes + database.save self end def save_with_custom_unique_id attempt = 0 value = '' \ No newline at end of file