Module: Thor::Base

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/thor/base.rb', line 19

def options
  @options
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



58
59
60
61
62
# File 'lib/thor/base.rb', line 58

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

.register_klass_file(klass) ⇒ Object

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



85
86
87
88
89
90
91
# File 'lib/thor/base.rb', line 85

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

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

.shellObject

Returns the shell used in all Thor classes. Default to color one.



7
8
9
# File 'lib/thor/shell.rb', line 7

def self.shell
  @shell ||= Thor::Shell::Color
end

.shell=(klass) ⇒ Object

Sets the shell used in all Thor classes.



13
14
15
# File 'lib/thor/shell.rb', line 13

def self.shell=(klass)
  @shell = klass
end

.subclass_filesObject

Returns the files where the subclasses are kept.

Returns

Hash[path => Class]



78
79
80
# File 'lib/thor/base.rb', line 78

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

.subclassesObject

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

Returns

Array



69
70
71
# File 'lib/thor/base.rb', line 69

def subclasses
  @subclasses ||= []
end

Instance Method Details

#initialize(args = [], 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

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

Configuration for this Thor class.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/thor/base.rb', line 37

def initialize(args=[], options={}, config={})
  Thor::Arguments.parse(self.class.arguments, args).each do |key, value|
    send("#{key}=", value)
  end

  parse_options = self.class.class_options

  if options.is_a?(Array)
    task_options  = config.delete(:task_options) # hook for start
    parse_options = parse_options.merge(task_options) if task_options
    array_options, hash_options = options, {}
  else
    array_options, hash_options = [], options
  end

  options = Thor::Options.parse(parse_options, array_options)
  self.options = Thor::CoreExt::HashWithIndifferentAccess.new(options).merge!(hash_options)
  self.options.freeze
end