Module: Loader::AutoLoad::Support
- Defined in:
- lib/loader/autoload.rb
Class Method Summary collapse
- .generate_levels(klass, name) ⇒ Object
- .pwd ⇒ Object
- .try_fetch_constant(caller_class, name) ⇒ Object
- .try_load_by(caller_class, name) ⇒ Object
-
.underscore(camel_cased_word) ⇒ Object
Based on ActiveSupport, removed inflections.
Class Method Details
.generate_levels(klass, name) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/loader/autoload.rb', line 25 def generate_levels(klass, name) levels = klass.to_s.split('::').reduce([]) { |m, c| m << [(last_obj = m.last), c].compact.join('::');m }.map { |str| [str, name].join('::') } levels.unshift(name.to_s) return levels end |
.pwd ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/loader/autoload.rb', line 7 def pwd if !!ENV['BUNDLE_GEMFILE'] ENV['BUNDLE_GEMFILE'].split(File::Separator)[0..-2].join(File::Separator) else Dir.pwd end end |
.try_fetch_constant(caller_class, name) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/loader/autoload.rb', line 33 def try_fetch_constant(caller_class, name) levels = Support.generate_levels(caller_class, name).map { |str| Regexp.escape(str) } ObjectSpace.each_object(Module) { |obj| if !!(obj.to_s =~ /^(#{levels.join('|')})$/) return obj end };nil end |
.try_load_by(caller_class, name) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/loader/autoload.rb', line 15 def try_load_by(caller_class,name) levels = generate_levels(caller_class, name) ['lib',nil].each do |folder| levels.map{|str| File.join(*[pwd,folder,"#{underscore(str)}.rb"].compact) }.sort{|a,b| b.length <=> a.length }.each do |path| return if File.exist?(path) && require(path) end end end |
.underscore(camel_cased_word) ⇒ Object
Based on ActiveSupport, removed inflections. github.com/rails/rails/blob/v4.1.0.rc1/activesupport/lib/active_support/inflector/methods.rb
44 45 46 47 48 49 50 51 |
# File 'lib/loader/autoload.rb', line 44 def underscore(camel_cased_word) word = camel_cased_word.to_s.gsub('::', '/') word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |