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.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/action_processor/base.rb', line 17 def initialize(params = {}) @params = if params.instance_of? 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.
9 10 11 |
# File 'lib/action_processor/base.rb', line 9 def errors @errors end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/action_processor/base.rb', line 9 def params @params end |
Class Method Details
.run(params = {}) ⇒ Object
11 12 13 14 15 |
# File 'lib/action_processor/base.rb', line 11 def self.run(params = {}) inst = new(params) inst.run inst end |
Instance Method Details
#errors? ⇒ Boolean
34 35 36 |
# File 'lib/action_processor/base.rb', line 34 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
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/action_processor/base.rb', line 77 def fail!(errs, attr = :not_specified) if errs.class.ancestors.map(&:to_s).include?("ActiveRecord::Base") fail_active_record!(errs) elsif errs.instance_of? ActionProcessor::Errors @errors.concat 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.
52 53 54 |
# File 'lib/action_processor/base.rb', line 52 def failed_json {success: false, errors: errors.grouped_by_attribute} end |
#json_outcome ⇒ Object
42 43 44 |
# File 'lib/action_processor/base.rb', line 42 def json_outcome errors? ? failed_json : successful_json end |
#run ⇒ Object
30 31 32 |
# File 'lib/action_processor/base.rb', line 30 def run raise 'Error: method "run" should be overridden to implement business logic!' end |
#step(step_method, **options) ⇒ Object
56 57 58 59 60 |
# File 'lib/action_processor/base.rb', line 56 def step(step_method, **) return if errors? # skip it if there are errors step_always(step_method, **) end |
#step_always(step_method, **options) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/action_processor/base.rb', line 62 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
38 39 40 |
# File 'lib/action_processor/base.rb', line 38 def success? @errors.all.empty? end |
#successful_json ⇒ Object
in most cases should be overriden to provide relevant data
47 48 49 |
# File 'lib/action_processor/base.rb', line 47 def successful_json {success: true} end |