Class: ActionCommand::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/action_command/executable.rb

Overview

Root class for action commands that can be executed by this library. Override execute_internal to implement one, call one of the variants of ActionCommand.execute_… to execute one.

Direct Known Subclasses

ExecutableTransaction, PrettyPrintLogAction

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Executable) initialize(args)

Do not call new directly, instead use ActionCommand#execute_… variants.



11
12
13
# File 'lib/action_command/executable.rb', line 11

def initialize(args)
  self.class.describe_io.process_input(self, args)
end

Instance Attribute Details

- (Object) parent

Returns the value of attribute parent



8
9
10
# File 'lib/action_command/executable.rb', line 8

def parent
  @parent
end

- (Object) test

Returns the value of attribute test



8
9
10
# File 'lib/action_command/executable.rb', line 8

def test
  @test
end

Instance Method Details

- (Boolean) api_context?

Returns true if this command was executed using ActionCommand.execute_api

Returns:

  • (Boolean)

    true if this command was executed using ActionCommand.execute_api



39
40
41
# File 'lib/action_command/executable.rb', line 39

def api_context?
  return root_context == ActionCommand::CONTEXT_API
end

- (Boolean) child_context?

Returns true if this command is a child of another command

Returns:

  • (Boolean)

    true if this command is a child of another command



45
46
47
# File 'lib/action_command/executable.rb', line 45

def child_context?
  return !parent.is_a?(Symbol)
end

- (ActionCommand::Result) execute(result)

Execute the logic of a command. Should not usually be called directly. Command executors should call one of the ActionCommand.execute_… variants. Command implementors should override execute_internal.



54
55
56
57
58
# File 'lib/action_command/executable.rb', line 54

def execute(result)
  execute_internal(result)
  self.class.describe_io.process_output(self, result)
  return result
end

- (Object) execute_internal(result)

Override this method to implement the logic of your command

Parameters:

  • result (ActionCommand::Result)

    a result object where you can store the results of your logic, or indicate that the command failed.



75
76
77
# File 'lib/action_command/executable.rb', line 75

def execute_internal(result)
  
end

- (Boolean) rails_context?

Returns true if this command was executed using ActionCommand.execute_rails

Returns:

  • (Boolean)

    true if this command was executed using ActionCommand.execute_rails



29
30
31
# File 'lib/action_command/executable.rb', line 29

def rails_context?
  return root_context == ActionCommand::CONTEXT_RAILS
end

- (Boolean) rake_context?

Returns true if this command was executed using ActionCommand.execute_rake

Returns:

  • (Boolean)

    true if this command was executed using ActionCommand.execute_rake



34
35
36
# File 'lib/action_command/executable.rb', line 34

def rake_context?
  return root_context == ActionCommand::CONTEXT_RAKE
end

- (Symbol) root_context

action was executed in, see the ActionCommand::CONTEXT_ constants.

Returns:

  • (Symbol)

    the symbol indicating what context this



17
18
19
20
21
# File 'lib/action_command/executable.rb', line 17

def root_context
  context = parent
  context = context.parent until context.is_a? Symbol
  return context
end

- (Boolean) test_context?

Returns true if this command was executed using ActionCommand.execute_test

Returns:

  • (Boolean)

    true if this command was executed using ActionCommand.execute_test



24
25
26
# File 'lib/action_command/executable.rb', line 24

def test_context?
  return root_context == ActionCommand::CONTEXT_TEST
end

- (Object) testing {|context| ... }

Call this within a commands execution if you'd like to perform validations within the testing context.

Yields:

  • (context)

    Yields back the testing context that you passed in to ActionCommand#execute_test.



64
65
66
# File 'lib/action_command/executable.rb', line 64

def testing
  yield @test if @test
end