Module: Bauxite::ActionModule

Included in:
Action
Defined in:
lib/bauxite/core/action.rb

Overview

Action common state and behavior.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cmdObject (readonly)

Parsed action command (i.e. action name)



30
31
32
# File 'lib/bauxite/core/action.rb', line 30

def cmd
  @cmd
end

#ctxObject (readonly)

Test context



27
28
29
# File 'lib/bauxite/core/action.rb', line 27

def ctx
  @ctx
end

#textObject (readonly)

Raw action text.



33
34
35
# File 'lib/bauxite/core/action.rb', line 33

def text
  @text
end

Instance Method Details

#args(quote = false) ⇒ Object

Returns the action arguments after applying variable expansion.

See Context#expand.

If quote is true, the arguments are surrounded by quote characters (“) and every quote inside an argument is doubled.

For example:

# assuming 
#     action.new(ctx, cmd,
#         [ 'dude', 'say "hi"', '${myvar} ], # args
#         text, file, line)
#     ctx.variables = { 'myvar' => 'world' }

action.args
# => [ 'dude', 'say "hi"', 'world' ]

action.args(true)
# => [ '"dude"', '"say ""hi"""', '"world"' ]


69
70
71
72
73
# File 'lib/bauxite/core/action.rb', line 69

def args(quote = false)
  ret = @args.map { |a| @ctx.expand(a) }
  ret = ret.map { |a| '"'+a.gsub('"', '""')+'"' } if quote
  ret
end

#executeObject

Executes the action evaluating the arguments in the current context.

Note that #execute calls #args to expand variables. This means that two calls to #execute on the same instance might yield different results.

For example:

action = ctx.parse_action('echo ${message}')

ctx.variables = { 'message' => 'hi!' }
action.execute()
# => outputs 'hi!'

ctx.variables['message'] = 'hello world'
action.execute()
# => outputs 'hello world!' yet the instance of action is same!


91
92
93
# File 'lib/bauxite/core/action.rb', line 91

def execute()
  send(@cmd_real, *args)
end

#initialize(ctx, cmd, args, text, file, line) ⇒ Object

Constructs a new test action instance.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bauxite/core/action.rb', line 36

def initialize(ctx, cmd, args, text, file, line)
  @ctx  = ctx
  @cmd  = cmd
  @args = args
  @text = text
  
  @cmd_real = (respond_to? cmd+'_action') ? (cmd+'_action') : cmd
  
  unless respond_to? @cmd_real and Context::actions.include? @cmd
    raise "#{file} (line #{line+1}): Unknown command #{cmd}."
  end
end