Module: CouchRest::Mixins::ExtendedAttachments

Included in:
ExtendedDocument
Defined in:
lib/couchrest/mixins/extended_attachments.rb

Instance Method Summary collapse

Instance Method Details

#attachment_uri(attachment_name) ⇒ Object

returns URI to fetch the attachment from



48
49
50
51
# File 'lib/couchrest/mixins/extended_attachments.rb', line 48

def attachment_uri(attachment_name)
  return unless has_attachment?(attachment_name)
  "#{database.uri}/#{self.id}/#{attachment_name}"
end

#attachment_url(attachment_name) ⇒ Object

returns URL to fetch the attachment from



42
43
44
45
# File 'lib/couchrest/mixins/extended_attachments.rb', line 42

def attachment_url(attachment_name)
  return unless has_attachment?(attachment_name)
  "#{database.root}/#{self.id}/#{attachment_name}"
end

#create_attachment(args = {}) ⇒ Object

creates a file attachment to the current doc



6
7
8
9
10
11
12
13
# File 'lib/couchrest/mixins/extended_attachments.rb', line 6

def create_attachment(args={})
  raise ArgumentError unless args[:file] && args[:name]
  return if has_attachment?(args[:name])
  self['_attachments'] ||= {}
  set_attachment_attr(args)
rescue ArgumentError => e
  raise ArgumentError, 'You must specify :file and :name'
end

#delete_attachment(attachment_name) ⇒ Object

deletes a file attachment from the current doc



31
32
33
34
# File 'lib/couchrest/mixins/extended_attachments.rb', line 31

def delete_attachment(attachment_name)
  return unless self['_attachments']
  self['_attachments'].delete attachment_name
end

#has_attachment?(attachment_name) ⇒ Boolean

returns true if attachment_name exists

Returns:

  • (Boolean)


37
38
39
# File 'lib/couchrest/mixins/extended_attachments.rb', line 37

def has_attachment?(attachment_name)
  !!(self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?)
end

#read_attachment(attachment_name) ⇒ Object

reads the data from an attachment



16
17
18
# File 'lib/couchrest/mixins/extended_attachments.rb', line 16

def read_attachment(attachment_name)
  Base64.decode64(database.fetch_attachment(self, attachment_name))
end

#update_attachment(args = {}) ⇒ Object

modifies a file attachment on the current doc



21
22
23
24
25
26
27
28
# File 'lib/couchrest/mixins/extended_attachments.rb', line 21

def update_attachment(args={})
  raise ArgumentError unless args[:file] && args[:name]
  return unless has_attachment?(args[:name])
  delete_attachment(args[:name])
  set_attachment_attr(args)
rescue ArgumentError => e
  raise ArgumentError, 'You must specify :file and :name'
end