Class: MisterBin::Runner

Inherits:
Object
  • Object
show all
Includes:
Colsole
Defined in:
lib/mister_bin/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Runner



9
10
11
12
13
14
15
# File 'lib/mister_bin/runner.rb', line 9

def initialize(opts = {})
  @header = opts[:header]
  @footer = opts[:footer]
  @version = opts[:version]
  @commands = opts[:commands] || {}
  @handler = opts[:handler]
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



7
8
9
# File 'lib/mister_bin/runner.rb', line 7

def commands
  @commands
end

Returns the value of attribute footer.



7
8
9
# File 'lib/mister_bin/runner.rb', line 7

def footer
  @footer
end

#handlerObject (readonly)

Returns the value of attribute handler.



7
8
9
# File 'lib/mister_bin/runner.rb', line 7

def handler
  @handler
end

#headerObject (readonly)

Returns the value of attribute header.



7
8
9
# File 'lib/mister_bin/runner.rb', line 7

def header
  @header
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/mister_bin/runner.rb', line 7

def version
  @version
end

Instance Method Details

#aliasesObject



45
46
47
# File 'lib/mister_bin/runner.rb', line 45

def aliases
  @aliases ||= {}
end

#route(key, to:) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/mister_bin/runner.rb', line 17

def route(key, to:)
  if key.is_a? Array
    target = key.shift
    commands[target] = to
    key.each { |alias_name| aliases[alias_name] = target }
  else
    commands[key] = to
  end
end

#route_all(to:) ⇒ Object



27
28
29
# File 'lib/mister_bin/runner.rb', line 27

def route_all(to:)
  @handler = to
end

#run(argv = []) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mister_bin/runner.rb', line 31

def run(argv = [])
  if handler
    handler.execute argv
  elsif argv.empty?
    show_subs
  elsif (argv == ['--help']) || (argv == ['-h'])
    show_help
  elsif version && ((argv == ['--version']) || (argv == ['-v']))
    show_version
  else
    execute argv
  end
end