Module: CouchRest::Model::ExtendedAttachments

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/couchrest/model/extended_attachments.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) attachment_uri(attachment_name)

returns URI to fetch the attachment from



57
58
59
60
# File 'lib/couchrest/model/extended_attachments.rb', line 57

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

- (Object) attachment_url(attachment_name)

returns URL to fetch the attachment from



51
52
53
54
# File 'lib/couchrest/model/extended_attachments.rb', line 51

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

- (Object) attachments

return all attachments



17
18
19
# File 'lib/couchrest/model/extended_attachments.rb', line 17

def attachments
  self['_attachments'] ||= {}
end

- (Object) create_attachment(args = {})

Add a file attachment to the current document. Expects :file and :name to be included in the arguments.



8
9
10
11
12
13
14
# File 'lib/couchrest/model/extended_attachments.rb', line 8

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

- (Object) delete_attachment(attachment_name)

deletes a file attachment from the current doc



37
38
39
40
41
42
43
# File 'lib/couchrest/model/extended_attachments.rb', line 37

def delete_attachment(attachment_name)
  return unless attachments
  if attachments.include?(attachment_name)
    attribute_will_change!("_attachments")
    attachments.delete attachment_name
  end
end

- (Boolean) has_attachment?(attachment_name)

returns true if attachment_name exists

Returns:

  • (Boolean)


46
47
48
# File 'lib/couchrest/model/extended_attachments.rb', line 46

def has_attachment?(attachment_name)
  !!(attachments && attachments[attachment_name] && !attachments[attachment_name].empty?)
end

- (Object) read_attachment(attachment_name)

reads the data from an attachment



22
23
24
# File 'lib/couchrest/model/extended_attachments.rb', line 22

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

- (Object) update_attachment(args = {})

modifies a file attachment on the current doc



27
28
29
30
31
32
33
34
# File 'lib/couchrest/model/extended_attachments.rb', line 27

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