Module: WorkerTools::Basics
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/worker_tools/basics.rb
Instance Method Summary collapse
- #complete_with_warnings_note_levels ⇒ Object
- #finalize ⇒ Object
- #model ⇒ Object
- #model_class ⇒ Object
- #model_kind ⇒ Object
- #non_failure_error?(error) ⇒ Boolean
- #perform(model_id = nil) ⇒ Object
- #run ⇒ Object
- #with_wrapper_basics(&block) ⇒ Object
- #with_wrappers(wrapper_symbols, &block) ⇒ Object
- #wrapper_methods ⇒ Object
Instance Method Details
#complete_with_warnings_note_levels ⇒ Object
73 74 75 |
# File 'lib/worker_tools/basics.rb', line 73 def complete_with_warnings_note_levels %w[error warning] end |
#finalize ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/worker_tools/basics.rb', line 65 def finalize mark_with_warnings = model.notes.any? do |note| complete_with_warnings_note_levels.include?(note.with_indifferent_access[:level].to_s) end model.update!(state: mark_with_warnings ? :complete_with_warnings : :complete) end |
#model ⇒ Object
77 78 79 |
# File 'lib/worker_tools/basics.rb', line 77 def model @model ||= find_model end |
#model_class ⇒ Object
19 20 21 22 |
# File 'lib/worker_tools/basics.rb', line 19 def model_class # Ex: Import raise "model_class has to be defined in #{self}" end |
#model_kind ⇒ Object
24 25 26 27 |
# File 'lib/worker_tools/basics.rb', line 24 def model_kind # Ex: 'sdom' raise "model_kind has to be defined in #{self}" end |
#non_failure_error?(error) ⇒ Boolean
88 89 90 91 92 |
# File 'lib/worker_tools/basics.rb', line 88 def non_failure_error?(error) error.is_a?(WorkerTools::Errors::Invalid) # or add your list # [WorkerTools::Errors::Invalid, SomeOtherError].any? { |k| e.is_a?(k) } end |
#perform(model_id = nil) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/worker_tools/basics.rb', line 33 def perform(model_id = nil) @model_id = model_id with_wrappers(wrapper_methods) do run end end |
#run ⇒ Object
29 30 31 |
# File 'lib/worker_tools/basics.rb', line 29 def run raise "run has to be defined in #{self}" end |
#with_wrapper_basics(&block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/worker_tools/basics.rb', line 50 def with_wrapper_basics(&block) save_state_without_validate('running') block.yield finalize # this time we do want to catch Exception to attempt to handle some of the # critical errors. # rubocop:disable Lint/RescueException rescue Exception => e return finalize if non_failure_error?(e) # rubocop:enable Lint/RescueException save_state_without_validate('failed') raise end |
#with_wrappers(wrapper_symbols, &block) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/worker_tools/basics.rb', line 81 def with_wrappers(wrapper_symbols, &block) return yield if wrapper_symbols.blank? current_wrapper_symbol = wrapper_symbols.shift send(current_wrapper_symbol) { with_wrappers(wrapper_symbols, &block) } end |
#wrapper_methods ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/worker_tools/basics.rb', line 41 def wrapper_methods self.class.read_wrappers.map do |wrapper| symbolized_method = "with_wrapper_#{wrapper}".to_sym raise "Missing wrapper #{wrapper}" unless respond_to?(symbolized_method) symbolized_method end end |