Module: AuxiliaryRails::Concerns::Performable

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations, Callable, Errorable
Included in:
Application::Command, Application::Form
Defined in:
lib/auxiliary_rails/concerns/performable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errorable

#error!

Methods included from Callable

included

Instance Attribute Details

#performable_statusObject (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(options = {})
  ensure_empty_status!

  if options[:validate!] == true
    validate!
  elsif options[:validate] != false && invalid?
    return failure!
  end

  perform

  ensure_execution!
  self
end

#ensure_empty_errors!nil (protected)

Returns:

  • (nil)

Raises:



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.

Returns:

  • (self)


94
95
96
97
98
99
100
101
# File 'lib/auxiliary_rails/concerns/performable.rb', line 94

def failure!(message = nil, options = {})
  ensure_empty_status!

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

  self.performable_status = :failure
  self
end

#failure?Boolean

Returns:

  • (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

Parameters:

  • status (Symbol)

    Desired command status

  • &block

    Code to be executed if status matches

Yields:

  • (_self)

Yield Parameters:

Returns:

  • (self)


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

#performself

This method is abstract.

Describes business logic.

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

Returns:

  • (self)

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/auxiliary_rails/concerns/performable.rb', line 36

def perform
  raise NotImplementedError
end

#status?(value) ⇒ Boolean

Returns:

  • (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.

Returns:

  • (self)


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

Returns:

  • (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