Module: Thor::Util
- Defined in:
- lib/thor/lib/thor/util.rb
Class Method Summary collapse
- .constant_from_thor_path(str) ⇒ Object
- .constant_to_thor_path(str, remove_default = true) ⇒ Object
- .constants_in_contents(str, file = __FILE__) ⇒ Object
- .full_const_get(obj, name) ⇒ Object
- .make_constant(str, base = [Thor::Tasks, Object]) ⇒ Object
- .snake_case(str) ⇒ Object
- .to_constant(str) ⇒ Object
Class Method Details
.constant_from_thor_path(str) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/thor/lib/thor/util.rb', line 40 def self.constant_from_thor_path(str) make_constant(to_constant(str)) rescue NameError => e raise e unless e. =~ /^uninitialized constant (.*)$/ raise Error, "There was no available namespace `#{str}'." end |
.constant_to_thor_path(str, remove_default = true) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/thor/lib/thor/util.rb', line 33 def self.constant_to_thor_path(str, remove_default = true) str = str.to_s.gsub(/^Thor::Tasks::/, "") str = snake_case(str).squeeze(":") str.gsub!(/^default/, '') if remove_default str end |
.constants_in_contents(str, file = __FILE__) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/thor/lib/thor/util.rb', line 52 def self.constants_in_contents(str, file = __FILE__) klasses = ObjectSpace.classes.dup Module.new.class_eval(str, file) klasses = ObjectSpace.classes - klasses klasses = klasses.select {|k| k < Thor } klasses.map! {|k| k.to_s.gsub(/#<Module:\w+>::/, '')} end |
.full_const_get(obj, name) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/thor/lib/thor/util.rb', line 22 def self.full_const_get(obj, name) list = name.split("::") list.shift if list.first.empty? list.each do |x| # This is required because const_get tries to look for constants in the # ancestor chain, but we only want constants that are HERE obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x) end obj end |
.make_constant(str, base = [Thor::Tasks, Object]) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/thor/lib/thor/util.rb', line 60 def self.make_constant(str, base = [Thor::Tasks, Object]) which = base.find do |obj| full_const_get(obj, str) rescue nil end return full_const_get(which, str) if which raise NameError, "uninitialized constant #{str}" end |
.snake_case(str) ⇒ Object
68 69 70 71 72 |
# File 'lib/thor/lib/thor/util.rb', line 68 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
47 48 49 50 |
# File 'lib/thor/lib/thor/util.rb', line 47 def self.to_constant(str) str = 'default' if str.empty? str.gsub(/:(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end |