Module: MongoMapperExt::Storage
- Defined in:
- lib/mongomapper_ext/storage.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #add_mm_storage_errors ⇒ Object
- #fetch_file(name, options = {}) ⇒ Object
- #files(options = {}) ⇒ Object
- #mm_storage_errors ⇒ Object
- #put_file(name, io, options = {}) ⇒ Object
-
#upgrade_file_key(key, save = true) ⇒ Object
NOTE: this method will be removed on next release.
Class Method Details
.included(model) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/mongomapper_ext/storage.rb', line 3 def self.included(model) model.class_eval do extend ClassMethods validate :add_mm_storage_errors file_list :file_list end end |
Instance Method Details
#add_mm_storage_errors ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/mongomapper_ext/storage.rb', line 31 def add_mm_storage_errors mm_storage_errors.each do |k, msgs| msgs.each do |msg| self.errors.add(k, msg) end end end |
#fetch_file(name, options = {}) ⇒ Object
17 18 19 20 |
# File 'lib/mongomapper_ext/storage.rb', line 17 def fetch_file(name, = {}) file_list = send(.delete(:in) || :file_list) file_list.get(name) end |
#files(options = {}) ⇒ Object
22 23 24 25 |
# File 'lib/mongomapper_ext/storage.rb', line 22 def files( = {}) file_list = send(.delete(:in) || :file_list) file_list.files end |
#mm_storage_errors ⇒ Object
27 28 29 |
# File 'lib/mongomapper_ext/storage.rb', line 27 def mm_storage_errors @mm_storage_errors ||= {} end |
#put_file(name, io, options = {}) ⇒ Object
12 13 14 15 |
# File 'lib/mongomapper_ext/storage.rb', line 12 def put_file(name, io, = {}) file_list = send(.delete(:in) || :file_list) file_list.put(name, io, ) end |
#upgrade_file_key(key, save = true) ⇒ Object
NOTE: this method will be removed on next release
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mongomapper_ext/storage.rb', line 115 def upgrade_file_key(key, save = true) cname = self.collection.name files = self.database["#{cname}.files"] chunks = self.database["#{cname}.chunks"] fname = self["_#{key}"] rescue nil return if fname.blank? begin n = Mongo::GridIO.new(files, chunks, fname, "r", :query => {:filename => fname}) v = n.read if !v.empty? data = StringIO.new(v) self.put_file(key, data) self["_#{key}"] = nil self.save(:validate => false) if save end rescue => e puts "ERROR: #{e}" puts e.backtrace.join("\t\n") return end files.remove(:_id => fname) chunks.remove(:_id => fname) end |