class Truck::Autoloader
Attributes
base_nibbles[RW]
Public Class Methods
new(from, file)
click to toggle source
# File lib/truck/autoloader.rb, line 5 def initialize(from, file) @from = from @file = file @context, @base_nibbles = fetch_context_and_base_nibbles @dir_paths = [nil] end
Public Instance Methods
<<(const_name)
click to toggle source
# File lib/truck/autoloader.rb, line 12 def <<(const_name) raise_name_error!(const_name) unless context @dir_paths = each_possible_const(const_name).reduce [] do |new_paths, possible_const| resolved_const = context.resolve_const possible_const, file throw :const, resolved_const if resolved_const new_paths << possible_const if possible_namespace?(possible_const) new_paths end raise_name_error!(const_name) if dir_paths.empty? end
constify(*nibbles)
click to toggle source
# File lib/truck/autoloader.rb, line 36 def constify(*nibbles) nibbles.compact.join '::' end
each_base_nibble() { |mod, const| ... }
click to toggle source
given “Foo::Bar::Baz”, return [“Foo”, “Bar”, “Baz”]
# File lib/truck/autoloader.rb, line 58 def each_base_nibble return to_enum(:each_base_nibble) unless block_given? from.name.split('::').reduce Object do |mod, const| mod = mod.const_get const yield [mod, const] mod end end
each_possible_const(const_name) { |constify(e, *dir_path, const_name)| ... }
click to toggle source
# File lib/truck/autoloader.rb, line 23 def each_possible_const(const_name) return to_enum(:each_possible_const, const_name) unless block_given? dir_paths.each do |dir_path| base_nibbles.map { |e| yield constify(e, *dir_path, const_name) } yield constify(*dir_path, const_name) end end
fetch_context_and_base_nibbles()
click to toggle source
given “Foo::Bar::Baz”, return [“Foo::Bar::Baz”, “Foo::Bar”, etc.]
# File lib/truck/autoloader.rb, line 47 def fetch_context_and_base_nibbles each_base_nibble.to_a.reverse.reduce [] do |ary, (mod, const)| owner = Truck.contexts.each_value.detect { |c| c.context_for? mod } return [owner, ary] if owner ary.map! do |e| e.insert 0, '::'; e.insert 0, const; end ary << const end nil end
possible_namespace?(possible_const)
click to toggle source
# File lib/truck/autoloader.rb, line 31 def possible_namespace?(possible_const) snaked = StringInflections.to_snake_case possible_const context.root.join(snaked).directory? end
raise_name_error!(const_name = dir_paths.last)
click to toggle source
# File lib/truck/autoloader.rb, line 40 def raise_name_error!(const_name = dir_paths.last) message = "uninitialized constant #{const_name}" message << " (in #{context.mod})" if context raise NameError, message end