Class: CouchDocs::DocumentDirectory
- Inherits:
-
Object
- Object
- CouchDocs::DocumentDirectory
- Defined in:
- lib/couch_docs/document_directory.rb
Instance Attribute Summary collapse
-
#couch_doc_dir ⇒ Object
Returns the value of attribute couch_doc_dir.
Instance Method Summary collapse
- #each_document ⇒ Object
- #file_as_attachment(file) ⇒ Object
-
#initialize(path) ⇒ DocumentDirectory
constructor
A new instance of DocumentDirectory.
- #make_attachment_dir(id) ⇒ Object
- #save_attachment(id, filename, data) ⇒ Object
- #store_attachments(id, attachments) ⇒ Object
- #store_document(doc) ⇒ Object
Constructor Details
#initialize(path) ⇒ DocumentDirectory
Returns a new instance of DocumentDirectory.
8 9 10 11 |
# File 'lib/couch_docs/document_directory.rb', line 8 def initialize(path) Dir.new(path) @couch_doc_dir = path end |
Instance Attribute Details
#couch_doc_dir ⇒ Object
Returns the value of attribute couch_doc_dir.
6 7 8 |
# File 'lib/couch_docs/document_directory.rb', line 6 def couch_doc_dir @couch_doc_dir end |
Instance Method Details
#each_document ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/couch_docs/document_directory.rb', line 13 def each_document Dir["#{couch_doc_dir}/*.json"].each do |filename| id = File.basename(filename, '.json') json = JSON.parse(File.new(filename).read) if File.directory? "#{couch_doc_dir}/#{id}" json["_attachments"] ||= { } Dir["#{couch_doc_dir}/#{id}/*"].each do || next unless File.file? = File.basename() json["_attachments"][] = () end end yield [ id, json ] end end |
#file_as_attachment(file) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/couch_docs/document_directory.rb', line 38 def (file) type = mime_type(file) data = File.read(file) = { "data" => Base64.encode64(data).gsub(/\n/, '') } if type .merge!({"content_type" => type}) end end |
#make_attachment_dir(id) ⇒ Object
61 62 63 |
# File 'lib/couch_docs/document_directory.rb', line 61 def (id) FileUtils.mkdir_p "#{couch_doc_dir}/#{id}" end |
#save_attachment(id, filename, data) ⇒ Object
65 66 67 68 69 |
# File 'lib/couch_docs/document_directory.rb', line 65 def (id, filename, data) file = File.new "#{couch_doc_dir}/#{id}/#{filename}", "w" file.write Base64.decode64(data) file.close end |
#store_attachments(id, attachments) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/couch_docs/document_directory.rb', line 52 def (id, ) return unless (id) .each do |filename, opts| (id, filename, opts['data']) end end |
#store_document(doc) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/couch_docs/document_directory.rb', line 31 def store_document(doc) file = File.new("#{couch_doc_dir}/#{doc['_id']}.json", "w+") (doc['_id'], doc.delete('_attachments')) file.write(doc.to_json) file.close end |