Module: Thor::Util

Defined in:
lib/thor/util.rb

Class Method Summary collapse

Class Method Details

.constant_from_thor_path(str) ⇒ Object



12
13
14
15
16
17
# File 'lib/thor/util.rb', line 12

def self.constant_from_thor_path(str)
  make_constant(to_constant(str))
rescue NameError => e
  raise e unless e.message =~ /^uninitialized constant (.*)$/
  raise Error, "There was no available namespace `#{str}'."
end

.constant_to_thor_path(str, remove_default = true) ⇒ Object



6
7
8
9
10
# File 'lib/thor/util.rb', line 6

def self.constant_to_thor_path(str, remove_default = true)
  str = snake_case(str.to_s).squeeze(":")
  str.gsub!(/^default/, '') if remove_default
  str
end

.constants_in_contents(str) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/thor/util.rb', line 24

def self.constants_in_contents(str)
  klasses = self.constants.dup
  eval(str)
  ret = self.constants - klasses
  ret.each {|k| self.send(:remove_const, k)}
  ret
end

.make_constant(str) ⇒ Object



32
33
34
# File 'lib/thor/util.rb', line 32

def self.make_constant(str)
  list = str.split("::").inject(Object) {|obj, x| obj.const_get(x)}
end

.snake_case(str) ⇒ Object



36
37
38
39
40
# File 'lib/thor/util.rb', line 36

def self.snake_case(str)
  return str.downcase if str =~ /^[A-Z_]+$/
  str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
  return $+.downcase
end

.to_constant(str) ⇒ Object



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

def self.to_constant(str)
  str = 'default' if str.empty?
  str.gsub(/:(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end