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.



15
16
17
# File 'lib/thor/base.rb', line 15

def options
  @options
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



52
53
54
55
56
# File 'lib/thor/base.rb', line 52

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. Also invoke the source_root if the klass respond to it. This is needed to ensure that the source_root does not change after FileUtils#cd is called.



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

def register_klass_file(klass) #:nodoc:
  file = caller[1].match(/(.*):\d+/)[1]

  klass.source_root if klass.respond_to?(:source_root)
  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<String> => Class]



72
73
74
# File 'lib/thor/base.rb', line 72

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



63
64
65
# File 'lib/thor/base.rb', line 63

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<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 Thor class.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thor/base.rb', line 33

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

  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
    Thor::Options.parse(parse_options, options)
  else
    Thor::Options.parse(parse_options, []).merge(options)
  end

  self.options = Thor::CoreExt::HashWithIndifferentAccess.new(options).freeze
end