Class: FileModel::Adapter
- Inherits:
-
Object
- Object
- FileModel::Adapter
- Defined in:
- lib/file_model/adapter.rb
Constant Summary collapse
- EXPIRATION_TIME =
5
- BASE_DIR =
"/tmp/file_model_cache"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(something) ⇒ Adapter
constructor
A new instance of Adapter.
- #to_file ⇒ Object
Constructor Details
#initialize(something) ⇒ Adapter
Returns a new instance of Adapter.
2 3 4 |
# File 'lib/file_model/adapter.rb', line 2 def initialize something @something = something end |
Class Method Details
.cache_dir ⇒ Object
32 33 34 |
# File 'lib/file_model/adapter.rb', line 32 def cache_dir "#{BASE_DIR}/#{Time.now.min}/#{rand(1_000_000)}" end |
.clear_expired_cache ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/file_model/adapter.rb', line 36 def clear_expired_cache time = Time.now.min dir = BASE_DIR.to_dir if dir.exist? dir.to_dir.entries do |entry| entry_time = entry.name.to_i entry.delete if (entry_time > time) or (time > (entry_time + EXPIRATION_TIME)) end end end |
Instance Method Details
#to_file ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/file_model/adapter.rb', line 6 def to_file self.class.clear_expired_cache if something.respond_to? :to_file something.to_file elsif something.is_a? Hash data = something["tempfile"] || something[:tempfile] || raise("no file!") name = something["filename"] || something[:filename] || raise("no filename!") file = "#{self.class.cache_dir}/#{name}".to_file file.write data.read file elsif something.is_a? File path = something.path name = File.basename path file = "#{self.class.cache_dir}/#{name}".to_file file.write something.read file else raise "unknown file format!" end end |