Class: Exegesis::Document::Attachments

Inherits:
Hash
  • Object
show all
Defined in:
lib/exegesis/document/attachments.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Attachments

Returns a new instance of Attachments.



8
9
10
11
12
13
14
15
# File 'lib/exegesis/document/attachments.rb', line 8

def initialize doc
  @document = doc
  if @document['_attachments']
    @document['_attachments'].each do |name,meta| 
      update(name => Exegesis::Document::Attachment.new(name, meta, document))
    end
  end
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



6
7
8
# File 'lib/exegesis/document/attachments.rb', line 6

def document
  @document
end

Instance Method Details

#[]=(name, contents_and_type) ⇒ Object



44
45
46
47
48
49
# File 'lib/exegesis/document/attachments.rb', line 44

def []= name, contents_and_type
  @dirty = true
  content = contents_and_type.shift
  meta = {'data' => Base64.encode64(content).gsub(/\s/,''), 'content_type' => contents_and_type.first, 'length' => content.length}
  update(name => Exegesis::Document::Attachment.new(name, meta, document))
end

#clean!Object



26
27
28
29
30
31
32
33
# File 'lib/exegesis/document/attachments.rb', line 26

def clean!
  each do |name, attachment|
    next if attachment.stub?
    attachment.['stub'] = true
    attachment..delete('data')
  end
  @dirty = false
end

#dirty?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/exegesis/document/attachments.rb', line 22

def dirty?
  @dirty || false
end

#put(name, contents, type) ⇒ Object

saves the attachment to the database NOW. does not keep the attachment in memory once this is done.



36
37
38
39
40
41
42
# File 'lib/exegesis/document/attachments.rb', line 36

def put(name, contents, type)
  r = Exegesis::Http.put("#{document.uri}/#{name}?rev=#{document.rev}", contents, {:content_type => type})
  if r['ok']
    document['_rev'] = r['rev']
    update(name => Exegesis::Document::Attachment.new(name, {'content_type' => type, 'stub' => true, 'length' => contents.length}, document))
  end
end

#to_sObject Also known as: inspect



17
18
19
# File 'lib/exegesis/document/attachments.rb', line 17

def to_s
  "#<Exegesis::Document::Attachments document=#{@document.uri} attachments=#{keys.join(', ')}>"
end