Class: Runfile::Userfile

Inherits:
Object
  • Object
show all
Includes:
DSL, Inspectable, Renderable
Defined in:
lib/runfile/userfile.rb

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

Methods included from Inspectable

#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

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/runfile/userfile.rb', line 10

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/runfile/userfile.rb', line 10

def path
  @path
end

Instance Method Details

#basenameObject



19
20
21
# File 'lib/runfile/userfile.rb', line 19

def basename
  @basename ||= File.basename(path, '.runfile')
end

#codeObject



23
24
25
# File 'lib/runfile/userfile.rb', line 23

def code
  @code ||= File.read(path)
end

#commandsObject



27
28
29
# File 'lib/runfile/userfile.rb', line 27

def commands
  actions.values.select(&:help)
end

#eval_codeObject



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

Returns:

  • (Boolean)


38
39
40
# File 'lib/runfile/userfile.rb', line 38

def evaluated?
  @evaluated
end

#full_nameObject



42
43
44
# File 'lib/runfile/userfile.rb', line 42

def full_name
  @full_name ||= id.join ' '
end

#guestsObject



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

#idObject



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

#inspectableObject



66
67
68
# File 'lib/runfile/userfile.rb', line 66

def inspectable
  { name: name, path: path }
end

#nameObject



70
71
72
# File 'lib/runfile/userfile.rb', line 70

def name
  @name ||= (rootfile? ? nil : basename.downcase)
end

#rootfile?Boolean

Returns:

  • (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