module AuxiliaryRails::Concerns::Performable

Attributes

performable_status[RW]

Public Instance Methods

call(*args) click to toggle source
# File lib/auxiliary_rails/concerns/performable.rb, line 12
def call(*args)
  new(*args).call
end
failure?() click to toggle source
# File lib/auxiliary_rails/concerns/performable.rb, line 44
def failure?
  status?(:failure)
end
on(status) { |self| ... } click to toggle source

Provides ability to execude block of the code depending on command execution status

@param status [Symol] Desired command status @param &_block Code to be executed if status matches @return [self]

# File lib/auxiliary_rails/concerns/performable.rb, line 64
def on(status, &_block)
  ensure_execution!

  return self unless status?(status)

  yield(self) if block_given?

  self
end
perform() click to toggle source

Describes business logic.

Method should always return success! or failure! methods in order pro provide further correct method chaining.

@abstract @return [self]

# File lib/auxiliary_rails/concerns/performable.rb, line 40
def perform
  raise NotImplementedError
end
status?(value) click to toggle source
# File lib/auxiliary_rails/concerns/performable.rb, line 48
def status?(value)
  ensure_execution!

  performable_status == value&.to_sym
end
success?() click to toggle source
# File lib/auxiliary_rails/concerns/performable.rb, line 54
def success?
  status?(:success)
end
transaction(&block) click to toggle source

Shortcut for ActiveRecord::Base.transaction

# File lib/auxiliary_rails/concerns/performable.rb, line 75
def transaction(&block)
  ActiveRecord::Base.transaction(&block) if block_given?
end

Protected Instance Methods

ensure_empty_errors!() click to toggle source

@raise [AuxiliaryRails::Application::Error]

Error happens if command contains any errors.

@return [nil]

# File lib/auxiliary_rails/concerns/performable.rb, line 86
def ensure_empty_errors!
  return if errors.empty?

  error!("`#{self.class}` contains errors.")
end
ensure_empty_status!() click to toggle source
# File lib/auxiliary_rails/concerns/performable.rb, line 92
def ensure_empty_status!
  return if performable_status.nil?

  error!("`#{self.class}` was already executed.")
end
ensure_execution!() click to toggle source
# File lib/auxiliary_rails/concerns/performable.rb, line 98
def ensure_execution!
  return if performable_status.present?

  error!("`#{self.class}` was not executed yet.")
end
failure!(message = nil, options = {}) click to toggle source

Sets command's status to failure.

@return [self]

# File lib/auxiliary_rails/concerns/performable.rb, line 107
def failure!(message = nil, options = {})
  ensure_empty_status!

  errors.add(:base, message, options) if message.present?

  self.performable_status = :failure
  self
end
success!() click to toggle source

Sets command's status to success.

@return [self]

# File lib/auxiliary_rails/concerns/performable.rb, line 119
def success!
  ensure_empty_errors!
  ensure_empty_status!

  self.performable_status = :success
  self
end