Class: Runfile::Action

Inherits:
Object
  • Object
show all
Includes:
Inspectable
Defined in:
lib/runfile/action.rb

Overview

Represents a single action inside a Userfile and executes its block on demand. Properties of this class are populated by the DSL module when the runfile is executed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

#inspect

Instance Attribute Details

#blockObject

Returns the value of attribute block.



9
10
11
# File 'lib/runfile/action.rb', line 9

def block
  @block
end

#helpObject

Returns the value of attribute help.



9
10
11
# File 'lib/runfile/action.rb', line 9

def help
  @help
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/runfile/action.rb', line 9

def host
  @host
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/runfile/action.rb', line 8

def name
  @name
end

#shortcutObject

Returns the value of attribute shortcut.



8
9
10
# File 'lib/runfile/action.rb', line 8

def shortcut
  @shortcut
end

Instance Method Details

#command_stringObject



11
12
13
14
# File 'lib/runfile/action.rb', line 11

def command_string
  result = names.join(', ')
  result.empty? ? '(default)' : result
end

#implicit_usagesObject



16
17
18
# File 'lib/runfile/action.rb', line 16

def implicit_usages
  usages.empty? ? [usage_string] : usages
end

#inspectableObject



20
21
22
# File 'lib/runfile/action.rb', line 20

def inspectable
  { name: name, prefix: prefix, 'host.path': host&.path }
end

#prefixObject



37
38
39
# File 'lib/runfile/action.rb', line 37

def prefix
  host&.full_name
end

#run(args = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/runfile/action.rb', line 24

def run(args = {})
  validate_context

  instance_eval do
    host.helpers.each { |b| b.call args }
    block.call args if block
  end
end

#usage_stringObject



45
46
47
48
49
50
51
# File 'lib/runfile/action.rb', line 45

def usage_string
  @usage_string ||= if shortcut
    "#{prefix} (#{name} | #{shortcut})".strip
  else
    "#{prefix} #{name}".strip
  end
end

#usagesObject



53
54
55
# File 'lib/runfile/action.rb', line 53

def usages
  @usages ||= []
end