Class: Runfile::Userfile
Overview
Represents a single runfile.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from DSL
#action, #actions, #env_var, #env_vars, #example, #examples, #help, #helper_blocks, #helpers, #import, #import_gem, #imports, #option, #options, #param, #params, #require_context, #required_contexts, #shortcut, #shortcuts, #summary, #title, #usage, #version
#inspect
Methods included from Renderable
#render
Constructor Details
#initialize(path, context: nil, host: nil) ⇒ Userfile
Returns a new instance of Userfile.
13
14
15
16
17
|
# File 'lib/runfile/userfile.rb', line 13
def initialize(path, context: nil, host: nil)
@path = path
@host = host
@context = context || {}
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
11
12
13
|
# File 'lib/runfile/userfile.rb', line 11
def context
@context
end
|
#host ⇒ Object
Returns the value of attribute host.
10
11
12
|
# File 'lib/runfile/userfile.rb', line 10
def host
@host
end
|
#path ⇒ Object
Returns the value of attribute path.
10
11
12
|
# File 'lib/runfile/userfile.rb', line 10
def path
@path
end
|
Instance Method Details
#basename ⇒ Object
19
20
21
|
# File 'lib/runfile/userfile.rb', line 19
def basename
@basename ||= File.basename(path, '.runfile')
end
|
#code ⇒ Object
23
24
25
|
# File 'lib/runfile/userfile.rb', line 23
def code
@code ||= File.read(path)
end
|
#commands ⇒ Object
27
28
29
|
# File 'lib/runfile/userfile.rb', line 27
def commands
actions.values.select(&:help)
end
|
#eval_code ⇒ Object
31
32
33
34
35
36
|
# File 'lib/runfile/userfile.rb', line 31
def eval_code
return if evaluated?
@evaluated = true
instance_eval code, path
end
|
#evaluated? ⇒ Boolean
38
39
40
|
# File 'lib/runfile/userfile.rb', line 38
def evaluated?
@evaluated
end
|
#full_name ⇒ Object
42
43
44
|
# File 'lib/runfile/userfile.rb', line 42
def full_name
@full_name ||= id.join ' '
end
|
#guests ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/runfile/userfile.rb', line 46
def guests
@guests ||= begin
result = imports.map do |glob, context|
Dir.glob("#{glob}.runfile").map do |guest_path|
Userfile.new guest_path, context: context, host: self
end
end
result.flatten
end
end
|
#id ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/runfile/userfile.rb', line 58
def id
@id ||= if host
(host.id + [name]).compact
else
[name].compact
end
end
|
#inspectable ⇒ Object
66
67
68
|
# File 'lib/runfile/userfile.rb', line 66
def inspectable
{ name: name, path: path }
end
|
#name ⇒ Object
70
71
72
|
# File 'lib/runfile/userfile.rb', line 70
def name
@name ||= (rootfile? ? nil : basename.downcase)
end
|
#rootfile? ⇒ Boolean
74
75
76
|
# File 'lib/runfile/userfile.rb', line 74
def rootfile?
@rootfile ||= basename.casecmp? 'runfile'
end
|
#run(argv = []) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/runfile/userfile.rb', line 78
def run(argv = [])
eval_code
if argv.any?
argv = expand_shortcuts argv
found_guest = find_guest argv
end
if found_guest
found_guest.run argv
else
run_local argv
end
end
|