class Kontena::Cli::SubcommandLoader

Attributes

path[R]

Public Class Methods

new(path) click to toggle source

Create a subcommand loader instance

@param [String] path path to command definition

# File lib/kontena/cli/subcommand_loader.rb, line 9
def initialize(path)
  @path = path
end

Public Instance Methods

class()
Alias for: klass
const_defined?(const) click to toggle source
# File lib/kontena/cli/subcommand_loader.rb, line 77
def const_defined?(const)
  klass.const_defined?(const)
end
const_get(const) click to toggle source
# File lib/kontena/cli/subcommand_loader.rb, line 73
def const_get(const)
  klass.const_get(const)
end
const_get_tree(tree) click to toggle source

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

@param tree [Array<Symbol] @return [Class]

# 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
klass() click to toggle source
# 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
Also aliased as: class
method_missing(meth, *args) click to toggle source
# File lib/kontena/cli/subcommand_loader.rb, line 65
def method_missing(meth, *args)
  klass.send(meth, *args)
end
new(*args) click to toggle source
# File lib/kontena/cli/subcommand_loader.rb, line 61
def new(*args)
  klass.new(*args)
end
prepend_kontena_cli(tree) click to toggle source

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

# File lib/kontena/cli/subcommand_loader.rb, line 24
def prepend_kontena_cli(tree)
  [:Kontena, :Cli] + (tree - [:Cli])
end
respond_to_missing?(meth) click to toggle source
# File lib/kontena/cli/subcommand_loader.rb, line 69
def respond_to_missing?(meth)
  klass.respond_to?(meth)
end
safe_require(path) click to toggle source

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

@param path [String] @return [TrueClass,FalseClass]

# File lib/kontena/cli/subcommand_loader.rb, line 46
def safe_require(path)
  require path
  true
rescue LoadError
  false
end
symbolize_path(path) click to toggle source

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

@param path [String] @return [Array<Symbol>]

# 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