Class: MongoMapperExt::FileList
- Inherits:
-
Hash
- Object
- Hash
- MongoMapperExt::FileList
- Defined in:
- lib/mongomapper_ext/file_list.rb
Instance Attribute Summary collapse
-
#parent_document ⇒ Object
Returns the value of attribute parent_document.
Class Method Summary collapse
Instance Method Summary collapse
- #destroy_files ⇒ Object
- #files ⇒ Object
- #get(id) ⇒ Object
- #put(id, io, metadata = {}) ⇒ Object
- #sync_files ⇒ Object
Instance Attribute Details
#parent_document ⇒ Object
Returns the value of attribute parent_document.
3 4 5 |
# File 'lib/mongomapper_ext/file_list.rb', line 3 def parent_document @parent_document end |
Class Method Details
.from_mongo(value) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mongomapper_ext/file_list.rb', line 14 def self.from_mongo(value) return value if value.kind_of?(self) result = FileList.new (value||{}).each do |k, v| result[k] = v.kind_of?(MongoMapperExt::File) ? v : MongoMapperExt::File.new(v) end result end |
.to_mongo(value) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/mongomapper_ext/file_list.rb', line 5 def self.to_mongo(value) result = {} value.each do |k, v| result[k] = v.to_mongo end result end |
Instance Method Details
#destroy_files ⇒ Object
64 65 66 67 68 69 |
# File 'lib/mongomapper_ext/file_list.rb', line 64 def destroy_files self.delete_if do |id, file| file._parent_document = parent_document file.delete end end |
#files ⇒ Object
42 43 44 |
# File 'lib/mongomapper_ext/file_list.rb', line 42 def files self.values end |
#get(id) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/mongomapper_ext/file_list.rb', line 46 def get(id) file = self[id] if file.nil? file = self[id] = MongoMapperExt::File.new end file._parent_document = parent_document file end |
#put(id, io, metadata = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mongomapper_ext/file_list.rb', line 25 def put(id, io, = {}) if !parent_document.new? filename = id if io.respond_to?(:original_filename) filename = io.original_filename elsif io.respond_to?(:path) && io.path filename = ::File.basename(io.path) elsif io.kind_of?(String) io = StringIO.new(io) end get(id).put(filename, io, ) else (@_pending_files ||= {})[id] = [io, ] end end |
#sync_files ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/mongomapper_ext/file_list.rb', line 55 def sync_files if @_pending_files @_pending_files.each do |filename, data| put(filename, data[0], data[1]) end @_pending_files = nil end end |