Class: PikoTransaction::CustomCommand

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/piko_transaction/custom_command.rb

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(do_action = nil, undo_action = nil, &alternative_do_action) ⇒ CustomCommand

Returns a new instance of CustomCommand.



26
27
28
29
30
31
32
33
# File 'lib/piko_transaction/custom_command.rb', line 26

def initialize(do_action = nil, undo_action = nil, &alternative_do_action)
  @name = nil
  @do_action = choose_do_action(do_action, alternative_do_action)
  @undo_action = undo_action
  @success_callbacks = []
  @failure_callbacks = []
  @done = false
end

Instance Method Details

#add_failure_callback(callback) ⇒ Object



56
57
58
59
# File 'lib/piko_transaction/custom_command.rb', line 56

def add_failure_callback(callback)
  @failure_callbacks << callback if callback.respond_to?(:call)
  logger.debug { format "%s Registered failure callbacks: %i", to_s, @failure_callbacks.count }
end

#add_success_callback(callback) ⇒ Object



51
52
53
54
# File 'lib/piko_transaction/custom_command.rb', line 51

def add_success_callback(callback)
  @success_callbacks << callback if callback.respond_to?(:call)
  logger.debug { format "%s Registered success callbacks: %i", to_s, @success_callbacks.count }
end

#doObject



35
36
37
# File 'lib/piko_transaction/custom_command.rb', line 35

def do
  execute_do_action ? call_success_callbacks : call_failure_callbacks
end

#name(value) ⇒ Object



47
48
49
# File 'lib/piko_transaction/custom_command.rb', line 47

def name(value)
  @name = value.to_s
end

#to_sObject



43
44
45
# File 'lib/piko_transaction/custom_command.rb', line 43

def to_s
  format "[%s]", @name || "custom_cmd"
end

#undoObject



39
40
41
# File 'lib/piko_transaction/custom_command.rb', line 39

def undo
  execute_undo_action
end