class FileTransactions::BaseCommand
A Base class that all commands must inherit from.
This class provides all the necessary methods/hooks to make it possible to group commands together and/or nested inside transactions (and other commands).
Attributes
executed[RW]
failure_state[RW]
state[RW]
Public Class Methods
execute(*args, **kwargs, &block)
click to toggle source
# File lib/file_transactions/base_command.rb, line 10 def self.execute(*args, **kwargs, &block) if RUBY_VERSION < '3.0' && kwargs.empty? new(*args, &block).tap { |cmd| cmd.execute } else new(*args, **kwargs, &block).tap { |cmd| cmd.execute } end end
Public Instance Methods
execute()
click to toggle source
Execute the command. This will trigger the following methods:
* #before * #execute! * #after
# File lib/file_transactions/base_command.rb, line 22 def execute scope = Transaction.scope scope&.register self prepare run_before run_excecute.tap { run_after } rescue StandardError self.failure_state = state raise ensure Transaction.scope = scope end
executed?()
click to toggle source
Returns true of false depending on if the commands has been executed.
# File lib/file_transactions/base_command.rb, line 54 def executed? !!executed end
failed?()
click to toggle source
Returns true if the command has been unsuccessfully executed, otherwise false.
# File lib/file_transactions/base_command.rb, line 59 def failed? !!failure_state end
register(command)
click to toggle source
This registers a nested command. This method is called whever a command is executed and should not be called manually.
# File lib/file_transactions/base_command.rb, line 49 def register(command) sub_commands[state] << command end
undo()
click to toggle source
Undo the changes made from a previous call to execute
. All previouly executed commands will be undone in reverse order.
# File lib/file_transactions/base_command.rb, line 37 def undo raise Error, "Cannot undo #{self.class} which hasn't been executed" unless executed? sub_commands[:after].reverse_each(&:undo) ret = undo! unless failure_state == :before sub_commands[:before].reverse_each(&:undo) ret end
Private Instance Methods
after()
click to toggle source
# File lib/file_transactions/base_command.rb, line 105 def after; end
before()
click to toggle source
# File lib/file_transactions/base_command.rb, line 95 def before; end
execute!()
click to toggle source
# File lib/file_transactions/base_command.rb, line 97 def execute! raise NotImplementedError, "#{self.clas} must implement #execute" end
prepare()
click to toggle source
# File lib/file_transactions/base_command.rb, line 67 def prepare Transaction.scope = self self.executed = true end
run_after()
click to toggle source
# File lib/file_transactions/base_command.rb, line 90 def run_after self.state = :after after end
run_before()
click to toggle source
# File lib/file_transactions/base_command.rb, line 80 def run_before self.state = :before before end
run_excecute()
click to toggle source
# File lib/file_transactions/base_command.rb, line 85 def run_excecute self.state = :exec execute! end
sub_commands()
click to toggle source
# File lib/file_transactions/base_command.rb, line 72 def sub_commands @sub_commands ||= { before: [], exec: [], after: [], } end
undo!()
click to toggle source
# File lib/file_transactions/base_command.rb, line 101 def undo! raise NotImplementedError, "#{self.clas} must implement #undo" end