Class: ActionProcessor::Base
- Inherits:
-
Object
- Object
- ActionProcessor::Base
- Defined in:
- lib/action_processor/base.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
- #errors? ⇒ Boolean
-
#fail!(errs, attr = :not_specified) ⇒ Object
As an “errs” params we could pass several types: - String with error description - array of Strings (error messages) - ActiveRecord model (invalid) - its errors will be copied.
-
#failed_json ⇒ Object
could be overriden to provide some specifics/advices/etc.
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
- #json_outcome ⇒ Object
- #run ⇒ Object
- #step(step_method, **options) ⇒ Object
- #step_always(step_method, **options) ⇒ Object
- #success? ⇒ Boolean
-
#successful_json ⇒ Object
in most cases should be overriden to provide relevant data.
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/action_processor/base.rb', line 16 def initialize(params = {}) @params = if params.class.name == 'Hash' params.with_indifferent_access else params end @errors = ActionProcessor::Errors.new @steps_stack = [] @transaction_level = ActiveRecord::Base.connection.open_transactions rescue ActiveRecord::ConnectionNotEstablished @transaction_level = nil end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/action_processor/base.rb', line 8 def errors @errors end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/action_processor/base.rb', line 8 def params @params end |
Class Method Details
.run(params = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/action_processor/base.rb', line 10 def self.run(params = {}) inst = new(params) inst.run inst end |
Instance Method Details
#errors? ⇒ Boolean
33 34 35 |
# File 'lib/action_processor/base.rb', line 33 def errors? @errors.all.any? end |
#fail!(errs, attr = :not_specified) ⇒ Object
As an “errs” params we could pass several types:
-
String with error description
-
array of Strings (error messages)
-
ActiveRecord model (invalid) - its errors will be copied
76 77 78 79 80 81 82 83 84 |
# File 'lib/action_processor/base.rb', line 76 def fail!(errs, attr = :not_specified) if errs.class.ancestors.map(&:to_s).include?("ActiveRecord::Base") fail_active_record!(errs) else @errors.add(errs, @current_step, attr) end raise ActiveRecord::Rollback if in_transaction? end |
#failed_json ⇒ Object
could be overriden to provide some specifics/advices/etc.
51 52 53 |
# File 'lib/action_processor/base.rb', line 51 def failed_json { success: false, errors: errors.grouped_by_attribute } end |
#json_outcome ⇒ Object
41 42 43 |
# File 'lib/action_processor/base.rb', line 41 def json_outcome errors? ? failed_json : successful_json end |
#run ⇒ Object
29 30 31 |
# File 'lib/action_processor/base.rb', line 29 def run raise 'Error: method "run" should be overridden to implement business logic!' end |
#step(step_method, **options) ⇒ Object
55 56 57 58 59 |
# File 'lib/action_processor/base.rb', line 55 def step(step_method, **) return if errors? # skip it if there are errors step_always(step_method, **) end |
#step_always(step_method, **options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/action_processor/base.rb', line 61 def step_always(step_method, **) @steps_stack << (@current_step || :not_specified) @current_step = step_method # performs even if there are errors # useful for: # - validation steps to return list of all errors # - errors reporting and making decisions at the end of processing send step_method, ** @current_step = @steps_stack.pop end |
#success? ⇒ Boolean
37 38 39 |
# File 'lib/action_processor/base.rb', line 37 def success? @errors.all.empty? end |
#successful_json ⇒ Object
in most cases should be overriden to provide relevant data
46 47 48 |
# File 'lib/action_processor/base.rb', line 46 def successful_json { success: true } end |