Class: Kontena::Cli::SubcommandLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/cli/subcommand_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SubcommandLoader

Create a subcommand loader instance

Parameters:

  • path (String)

    path to command definition



9
10
11
# File 'lib/kontena/cli/subcommand_loader.rb', line 9

def initialize(path)
  @path = path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



65
66
67
# File 'lib/kontena/cli/subcommand_loader.rb', line 65

def method_missing(meth, *args)
  klass.send(meth, *args)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/kontena/cli/subcommand_loader.rb', line 4

def path
  @path
end

Instance Method Details

#const_defined?(const) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/kontena/cli/subcommand_loader.rb', line 77

def const_defined?(const)
  klass.const_defined?(const)
end

#const_get(const) ⇒ Object



73
74
75
# File 'lib/kontena/cli/subcommand_loader.rb', line 73

def const_get(const)
  klass.const_get(const)
end

#const_get_tree(tree) ⇒ Class

Takes an array such as [:Master, :FooCommand] and returns Master::FooCommand

Parameters:

  • tree (Array<Symbol])

    ree [Array<Symbol]

Returns:

  • (Class)


32
33
34
35
36
37
38
39
40
# File 'lib/kontena/cli/subcommand_loader.rb', line 32

def const_get_tree(tree)
  if tree.size == 1
    Object.const_get(tree.first)
  else
    tree[1..-1].inject(Object.const_get(tree.first)) { |new_base, part| new_base.const_get(part) }
  end
rescue
  raise ArgumentError, "Can't figure out command class name from path #{path} - tried #{tree}"
end

#klassObject Also known as: class



53
54
55
56
57
58
59
# File 'lib/kontena/cli/subcommand_loader.rb', line 53

def klass
  return @subcommand_class if @subcommand_class
  unless safe_require(path) || safe_require(Kontena.cli_root(path))
    raise ArgumentError, "Can't load #{path} or #{Kontena.cli_root(path)}"
  end
  @subcommand_class = const_get_tree(prepend_kontena_cli(symbolize_path(path)))
end

#new(*args) ⇒ Object



61
62
63
# File 'lib/kontena/cli/subcommand_loader.rb', line 61

def new(*args)
  klass.new(*args)
end

#prepend_kontena_cli(tree) ⇒ Object

Takes an array such as [:Foo] or [:Cli, :Foo] and returns [:Kontena, :Cli, :Foo]



24
25
26
# File 'lib/kontena/cli/subcommand_loader.rb', line 24

def prepend_kontena_cli(tree)
  [:Kontena, :Cli] + (tree - [:Cli])
end

#respond_to_missing?(meth) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/kontena/cli/subcommand_loader.rb', line 69

def respond_to_missing?(meth)
  klass.respond_to?(meth)
end

#safe_require(path) ⇒ TrueClass, FalseClass

Tries to require a file, returns false instead of raising LoadError unless succesful

Parameters:

Returns:

  • (TrueClass, FalseClass)


46
47
48
49
50
51
# File 'lib/kontena/cli/subcommand_loader.rb', line 46

def safe_require(path)
  require path
  true
rescue LoadError
  false
end

#symbolize_path(path) ⇒ Array<Symbol>

Takes something like /foo/bar/cli/master/foo_coimmand and returns [:Master, :FooCommand]

Parameters:

Returns:

  • (Array<Symbol>)


17
18
19
20
21
# File 'lib/kontena/cli/subcommand_loader.rb', line 17

def symbolize_path(path)
  path.gsub(/.*\/cli\//, '').split('/').map do |path_part|
    path_part.split('_').map{ |e| e.capitalize }.join
  end.map(&:to_sym)
end