Class: Saviour::Uploader::ProcessorsRunner
- Inherits:
-
Object
- Object
- Saviour::Uploader::ProcessorsRunner
- Defined in:
- lib/saviour/uploader/processors_runner.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
- #file ⇒ Object
-
#filename ⇒ Object
Returns the value of attribute filename.
Instance Method Summary collapse
- #advance!(processor, previous_type) ⇒ Object
-
#initialize(uploader) ⇒ ProcessorsRunner
constructor
A new instance of ProcessorsRunner.
- #matching_processors ⇒ Object
- #run!(content_data, initial_filename) ⇒ Object
- #run_as_file!(start_file, initial_filename) ⇒ Object
- #run_method_or_block(method_or_block, opts, data) ⇒ Object
- #run_processor(processor) ⇒ Object
Constructor Details
#initialize(uploader) ⇒ ProcessorsRunner
Returns a new instance of ProcessorsRunner.
8 9 10 |
# File 'lib/saviour/uploader/processors_runner.rb', line 8 def initialize(uploader) @uploader = uploader end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
5 6 7 |
# File 'lib/saviour/uploader/processors_runner.rb', line 5 def contents @contents end |
#file ⇒ Object
16 17 18 |
# File 'lib/saviour/uploader/processors_runner.rb', line 16 def file @file ||= Tempfile.new([SecureRandom.hex, ::File.extname(filename)]).tap { |x| x.binmode } end |
#filename ⇒ Object
Returns the value of attribute filename.
6 7 8 |
# File 'lib/saviour/uploader/processors_runner.rb', line 6 def filename @filename end |
Instance Method Details
#advance!(processor, previous_type) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/saviour/uploader/processors_runner.rb', line 32 def advance!(processor, previous_type) if processor[:type] != previous_type if processor[:type] == :memory self.contents = ::File.read(file) else file.rewind file.truncate(0) file.binmode file.write(contents) file.flush file.rewind end end end |
#matching_processors ⇒ Object
12 13 14 |
# File 'lib/saviour/uploader/processors_runner.rb', line 12 def matching_processors @uploader.class.processors end |
#run!(content_data, initial_filename) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/saviour/uploader/processors_runner.rb', line 67 def run!(content_data, initial_filename) self.contents = content_data self.filename = initial_filename previous_type = :memory matching_processors.each do |processor| advance!(processor, previous_type) run_processor(processor) previous_type = processor[:type] end if previous_type == :file self.contents = ::File.read(file) end file.close! [contents, filename] end |
#run_as_file!(start_file, initial_filename) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/saviour/uploader/processors_runner.rb', line 88 def run_as_file!(start_file, initial_filename) @file = start_file @contents = nil @filename = initial_filename previous_type = :file matching_processors.each do |processor| advance!(processor, previous_type) run_processor(processor) previous_type = processor[:type] end if previous_type == :memory file.rewind file.truncate(0) file.binmode file.write(contents) file.flush file.rewind end [file, filename] end |
#run_method_or_block(method_or_block, opts, data) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/saviour/uploader/processors_runner.rb', line 20 def run_method_or_block(method_or_block, opts, data) if method_or_block.respond_to?(:call) @uploader.instance_exec(data, filename, &method_or_block) else if opts.empty? @uploader.send(method_or_block, data, filename) else @uploader.send(method_or_block, data, filename, **opts) end end end |
#run_processor(processor) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/saviour/uploader/processors_runner.rb', line 47 def run_processor(processor) method_or_block = processor[:method_or_block] opts = processor[:opts] if processor[:type] == :memory result = run_method_or_block(method_or_block, opts, contents) self.contents = result[0] self.filename = result[1] else result = run_method_or_block(method_or_block, opts, file) self.file = result[0] file.reopen(file.path, "r+") self.filename = result[1] end end |