Module: AuxiliaryRails::Concerns::Performable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Application::Command, Application::Form
- Defined in:
- lib/auxiliary_rails/concerns/performable.rb
Instance Attribute Summary collapse
-
#performable_status ⇒ Object
protected
Returns the value of attribute performable_status.
Instance Method Summary collapse
- #call(options = {}) ⇒ Object
- #ensure_empty_errors! ⇒ nil protected
- #ensure_empty_status! ⇒ Object protected
- #ensure_execution! ⇒ Object protected
-
#failure!(message = nil, options = {}) ⇒ self
protected
Sets command’s status to
failure
. - #failure? ⇒ Boolean
-
#on(status) {|_self| ... } ⇒ self
Provides ability to execude block of the code depending on command execution status.
-
#perform ⇒ self
abstract
Describes business logic.
- #status?(value) ⇒ Boolean
-
#success! ⇒ self
protected
Sets command’s status to
success
. - #success? ⇒ Boolean
-
#transaction(&block) ⇒ Object
protected
Shortcut for
ActiveRecord::Base.transaction
.
Methods included from Errorable
Methods included from Callable
Instance Attribute Details
#performable_status ⇒ Object (protected)
Returns the value of attribute performable_status.
68 69 70 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 68 def performable_status @performable_status end |
Instance Method Details
#call(options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 13 def call( = {}) ensure_empty_status! if [:validate!] == true validate! elsif [:validate] != false && invalid? return failure! end perform ensure_execution! self end |
#ensure_empty_errors! ⇒ nil (protected)
73 74 75 76 77 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 73 def ensure_empty_errors! return if errors.empty? error!("'#{self.class}' contains errors.") end |
#ensure_empty_status! ⇒ Object (protected)
79 80 81 82 83 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 79 def ensure_empty_status! return if performable_status.nil? error!("'#{self.class}' was already executed.") end |
#ensure_execution! ⇒ Object (protected)
85 86 87 88 89 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 85 def ensure_execution! return if performable_status.present? error!("'#{self.class}' was not executed yet.") end |
#failure!(message = nil, options = {}) ⇒ self (protected)
Sets command’s status to failure
.
94 95 96 97 98 99 100 101 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 94 def failure!( = nil, = {}) ensure_empty_status! errors.add(:base, , ) if .present? self.performable_status = :failure self end |
#failure? ⇒ Boolean
40 41 42 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 40 def failure? status?(:failure) end |
#on(status) {|_self| ... } ⇒ self
Provides ability to execude block of the code depending on command execution status
60 61 62 63 64 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 60 def on(status, &block) yield(self) if status?(status) && block self end |
#perform ⇒ self
This method is abstract.
Describes business logic.
Method should always return success!
or failure!
methods in order pro provide further correct method chaining.
36 37 38 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 36 def perform raise NotImplementedError end |
#status?(value) ⇒ Boolean
44 45 46 47 48 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 44 def status?(value) ensure_execution! performable_status == value&.to_sym end |
#success! ⇒ self (protected)
Sets command’s status to success
.
106 107 108 109 110 111 112 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 106 def success! ensure_empty_errors! ensure_empty_status! self.performable_status = :success self end |
#success? ⇒ Boolean
50 51 52 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 50 def success? status?(:success) end |
#transaction(&block) ⇒ Object (protected)
Shortcut for ActiveRecord::Base.transaction
115 116 117 |
# File 'lib/auxiliary_rails/concerns/performable.rb', line 115 def transaction(&block) ActiveRecord::Base.transaction(&block) if block end |