Module: Foreman::Thor::Base

Included in:
Foreman::Thor, Group
Defined in:
lib/foreman/vendor/thor/lib/thor/base.rb,
lib/foreman/vendor/thor/lib/thor/shell.rb

Defined Under Namespace

Modules: ClassMethods

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.shellObject

Returns the shell used in all Foreman::Thor classes. If you are in a Unix platform it will use a colored log, otherwise it will use a basic one without color.



11
12
13
14
15
16
17
18
19
# File 'lib/foreman/vendor/thor/lib/thor/shell.rb', line 11

def shell
  @shell ||= if ENV["THOR_SHELL"] && !ENV["THOR_SHELL"].empty?
    Foreman::Thor::Shell.const_get(ENV["THOR_SHELL"])
  elsif RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ && !ENV["ANSICON"]
    Foreman::Thor::Shell::Basic
  else
    Foreman::Thor::Shell::Color
  end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



26
27
28
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 26

def args
  @args
end

#optionsObject

Returns the value of attribute options.



26
27
28
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 26

def options
  @options
end

#parent_optionsObject

Returns the value of attribute parent_options.



26
27
28
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 26

def parent_options
  @parent_options
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



90
91
92
93
94
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 90

def included(base) #:nodoc:
  base.extend ClassMethods
  base.send :include, Invocation
  base.send :include, Shell
end

.register_klass_file(klass) ⇒ Object

Whenever a class inherits from Foreman::Thor or Foreman::Thor::Group, we should track the class and the file on Foreman::Thor::Base. This is the method responsable for it.



117
118
119
120
121
122
123
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 117

def register_klass_file(klass) #:nodoc:
  file = caller[1].match(/(.*):\d+/)[1]
  Foreman::Thor::Base.subclasses << klass unless Foreman::Thor::Base.subclasses.include?(klass)

  file_subclasses = Foreman::Thor::Base.subclass_files[File.expand_path(file)]
  file_subclasses << klass unless file_subclasses.include?(klass)
end

.subclass_filesObject

Returns the files where the subclasses are kept.

Returns

Hash[path<String> => Class]



110
111
112
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 110

def subclass_files
  @subclass_files ||= Hash.new { |h, k| h[k] = [] }
end

.subclassesObject

Returns the classes that inherits from Foreman::Thor or Foreman::Thor::Group.

Returns

Array



101
102
103
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 101

def subclasses
  @subclasses ||= []
end

Instance Method Details

#initialize(args = [], local_options = {}, config = {}) ⇒ Object

It receives arguments in an Array and two hashes, one for options and other for configuration.

Notice that it does not check if all required arguments were supplied. It should be done by the parser.

Parameters

args<Array>

An array of objects. The objects are applied to their respective accessors declared with argument.

options<Hash>

An options hash that will be available as self.options. The hash given is converted to a hash with indifferent access, magic predicates (options.skip?) and then frozen.

config<Hash>

Configuration for this Foreman::Thor class.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/foreman/vendor/thor/lib/thor/base.rb', line 44

def initialize(args = [], local_options = {}, config = {})
  parse_options = config[:current_command] && config[:current_command].disable_class_options ? {} : self.class.class_options

  # The start method splits inbound arguments at the first argument
  # that looks like an option (starts with - or --). It then calls
  # new, passing in the two halves of the arguments Array as the
  # first two parameters.

  command_options = config.delete(:command_options) # hook for start
  parse_options = parse_options.merge(command_options) if command_options
  if local_options.is_a?(Array)
    array_options = local_options
    hash_options = {}
  else
    # Handle the case where the class was explicitly instantiated
    # with pre-parsed options.
    array_options = []
    hash_options = local_options
  end

  # Let Foreman::Thor::Options parse the options first, so it can remove
  # declared options from the array. This will leave us with
  # a list of arguments that weren't declared.
  stop_on_unknown = self.class.stop_on_unknown_option? config[:current_command]
  opts = Foreman::Thor::Options.new(parse_options, hash_options, stop_on_unknown)
  self.options = opts.parse(array_options)
  self.options = config[:class_options].merge(options) if config[:class_options]

  # If unknown options are disallowed, make sure that none of the
  # remaining arguments looks like an option.
  opts.check_unknown! if self.class.check_unknown_options?(config)

  # Add the remaining arguments from the options parser to the
  # arguments passed in to initialize. Then remove any positional
  # arguments declared using #argument (this is primarily used
  # by Foreman::Thor::Group). Tis will leave us with the remaining
  # positional arguments.
  to_parse  = args
  to_parse += opts.remaining unless self.class.strict_args_position?(config)

  thor_args = Foreman::Thor::Arguments.new(self.class.arguments)
  thor_args.parse(to_parse).each { |k, v| __send__("#{k}=", v) }
  @args = thor_args.remaining
end