Module: AuxiliaryRails::Concerns::Performable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#performable_statusObject (protected)

Returns the value of attribute performable_status.



79
80
81
# File 'lib/auxiliary_rails/concerns/performable.rb', line 79

def performable_status
  @performable_status
end

Instance Method Details

#call(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/auxiliary_rails/concerns/performable.rb', line 15

def call(options = {})
  ensure_empty_status!

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

  perform

  ensure_execution!
  self
end

#ensure_empty_errors!nil (protected)

Returns:

  • (nil)

Raises:



84
85
86
87
88
# File 'lib/auxiliary_rails/concerns/performable.rb', line 84

def ensure_empty_errors!
  return if errors.empty?

  error!("`#{self.class}` contains errors.")
end

#ensure_empty_status!Object (protected)



90
91
92
93
94
# File 'lib/auxiliary_rails/concerns/performable.rb', line 90

def ensure_empty_status!
  return if performable_status.nil?

  error!("`#{self.class}` was already executed.")
end

#ensure_execution!Object (protected)



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

def ensure_execution!
  return if performable_status.present?

  error!("`#{self.class}` was not executed yet.")
end

#error!(message = nil) ⇒ Object (protected)

Raises:



102
103
104
105
# File 'lib/auxiliary_rails/concerns/performable.rb', line 102

def error!(message = nil)
  message ||= "`#{self.class}` raised error."
  raise error_class, message
end

#error_classObject (protected)



108
109
110
# File 'lib/auxiliary_rails/concerns/performable.rb', line 108

def error_class
  ApplicationError
end

#failure!(message = nil, options = {}) ⇒ self (protected)

Sets command’s status to failure.

Returns:

  • (self)


120
121
122
123
124
125
126
127
# File 'lib/auxiliary_rails/concerns/performable.rb', line 120

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)


42
43
44
# File 'lib/auxiliary_rails/concerns/performable.rb', line 42

def failure?
  status?(:failure)
end

#on(status) {|_self| ... } ⇒ self

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

Parameters:

  • status (Symol)

    Desired command status

  • &_block

    Code to be executed if status matches

Yields:

  • (_self)

Yield Parameters:

Returns:

  • (self)


62
63
64
65
66
67
68
69
70
# File 'lib/auxiliary_rails/concerns/performable.rb', line 62

def on(status, &_block)
  ensure_execution!

  return self unless status?(status)

  yield(self) if block_given?

  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)


38
39
40
# File 'lib/auxiliary_rails/concerns/performable.rb', line 38

def perform
  raise NotImplementedError
end

#status?(value) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/auxiliary_rails/concerns/performable.rb', line 46

def status?(value)
  ensure_execution!

  performable_status == value&.to_sym
end

#success!self (protected)

Sets command’s status to success.

Returns:

  • (self)


132
133
134
135
136
137
138
# File 'lib/auxiliary_rails/concerns/performable.rb', line 132

def success!
  ensure_empty_errors!
  ensure_empty_status!

  self.performable_status = :success
  self
end

#success?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/auxiliary_rails/concerns/performable.rb', line 52

def success?
  status?(:success)
end

#transaction(&block) ⇒ Object

Shortcut for ActiveRecord::Base.transaction



73
74
75
# File 'lib/auxiliary_rails/concerns/performable.rb', line 73

def transaction(&block)
  ActiveRecord::Base.transaction(&block) if block_given?
end