Class: MultiFileProcessor
- Inherits:
-
Object
- Object
- MultiFileProcessor
- Defined in:
- lib/multi-file-processor.rb
Overview
MultiFileProcessor processes files in a folder moving to the various states inprogress, done or failed.
Defined Under Namespace
Classes: FailedException
Constant Summary collapse
- DEFAULT_INPROGRESS_EXT =
'inprogress'- DEFAULT_DONE_EXT =
'done'- DEFAULT_FAILED_EXT =
'failed'
Instance Attribute Summary collapse
-
#file_pattern ⇒ Object
readonly
Returns the value of attribute file_pattern.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #done_ext ⇒ Object
- #each ⇒ Object
- #failed! ⇒ Object
- #failed_ext ⇒ Object
-
#initialize(file_pattern, options = {}) ⇒ MultiFileProcessor
constructor
A new instance of MultiFileProcessor.
- #inprogress_ext ⇒ Object
-
#move_inprogress_file_to_done(inprogress_file) ⇒ Object
rubocop:enable Lint/SuppressedException.
- #move_inprogress_file_to_failed(inprogress_file) ⇒ Object
-
#next_inprogress_file ⇒ Object
rubocop:disable Lint/SuppressedException.
- #reset_files! ⇒ Object
Constructor Details
#initialize(file_pattern, options = {}) ⇒ MultiFileProcessor
Returns a new instance of MultiFileProcessor.
16 17 18 19 |
# File 'lib/multi-file-processor.rb', line 16 def initialize(file_pattern, = {}) @file_pattern = file_pattern @options = end |
Instance Attribute Details
#file_pattern ⇒ Object (readonly)
Returns the value of attribute file_pattern.
13 14 15 |
# File 'lib/multi-file-processor.rb', line 13 def file_pattern @file_pattern end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/multi-file-processor.rb', line 13 def @options end |
Instance Method Details
#done_ext ⇒ Object
69 70 71 |
# File 'lib/multi-file-processor.rb', line 69 def done_ext [:done_ext] || DEFAULT_DONE_EXT end |
#each ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/multi-file-processor.rb', line 21 def each while (inprogress_file = next_inprogress_file) begin yield inprogress_file move_inprogress_file_to_done(inprogress_file) rescue FailedException move_inprogress_file_to_failed(inprogress_file) end end end |
#failed! ⇒ Object
61 62 63 |
# File 'lib/multi-file-processor.rb', line 61 def failed! raise FailedException, 'file processing failed' end |
#failed_ext ⇒ Object
73 74 75 |
# File 'lib/multi-file-processor.rb', line 73 def failed_ext [:failed_ext] || DEFAULT_FAILED_EXT end |
#inprogress_ext ⇒ Object
65 66 67 |
# File 'lib/multi-file-processor.rb', line 65 def inprogress_ext [:inprogress_ext] || DEFAULT_INPROGRESS_EXT end |
#move_inprogress_file_to_done(inprogress_file) ⇒ Object
rubocop:enable Lint/SuppressedException
45 46 47 |
# File 'lib/multi-file-processor.rb', line 45 def move_inprogress_file_to_done(inprogress_file) move_inprogress_file_to_ext(inprogress_file, done_ext) end |
#move_inprogress_file_to_failed(inprogress_file) ⇒ Object
49 50 51 |
# File 'lib/multi-file-processor.rb', line 49 def move_inprogress_file_to_failed(inprogress_file) move_inprogress_file_to_ext(inprogress_file, failed_ext) end |
#next_inprogress_file ⇒ Object
rubocop:disable Lint/SuppressedException
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/multi-file-processor.rb', line 33 def next_inprogress_file while (file = next_file) begin inprogress_file = "#{file}.#{inprogress_ext}" FileUtils.mv(file, inprogress_file) return inprogress_file rescue Errno::ENOENT end end end |
#reset_files! ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/multi-file-processor.rb', line 53 def reset_files! ext_reg = Regexp.new("\\.(#{inprogress_ext}|#{done_ext}|#{failed_ext})$") Dir.glob("#{file_pattern}.{#{inprogress_ext},#{done_ext},#{failed_ext}}").each do |file| original_file = file.sub(ext_reg, '') FileUtils.mv(file, original_file) end end |