class Truck::Context::ConstResolver

Attributes

current_path[RW]

Public Class Methods

new(context, expanded_const, skip_files) click to toggle source
# File lib/truck/const_resolver.rb, line 6
def initialize(context, expanded_const, skip_files)
  @context        = context
  @expanded_const = expanded_const
  @skip_files     = skip_files
end

Public Instance Methods

check_already_loaded() click to toggle source
# File lib/truck/const_resolver.rb, line 63
def check_already_loaded
  walk_const_parts.reduce context.mod do |mod, const_part|
    return nil unless mod.const_defined? const_part
    mod.const_get const_part
  end
end
check_loaded(rb_file) click to toggle source
# File lib/truck/const_resolver.rb, line 70
def check_loaded(rb_file)
  expected_const = expected_const_defined_in_rb_file rb_file
  walk_const_parts(expected_const).reduce context.mod do |mod, const_part|
    mod.const_defined?(const_part) or
      raise AutoloadError.new(expected_const, rb_file)
    mod.const_get const_part
  end
end
const_get() click to toggle source
# File lib/truck/const_resolver.rb, line 51
def const_get
  walk_const_parts.reduce context.mod do |mod, const_part|
    return nil unless mod.const_defined?(const_part)
    mod.const_get const_part
  end
end
each_autoload_path() { || ... } click to toggle source
# File lib/truck/const_resolver.rb, line 35
def each_autoload_path
  context.autoload_paths.each do |autoload_path|
    @current_path = autoload_path
    yield
  end
  @current_path = nil
end
each_possible_rb_file() { |rb_file| ... } click to toggle source
# File lib/truck/const_resolver.rb, line 24
def each_possible_rb_file
  each_autoload_path do
    snaked = StringInflections.to_snake_case expanded_const
    base_path = current_path.join snaked
    each_rb_file_from_base_path base_path do |rb_file|
      next if skip_files.include? rb_file.to_s
      yield rb_file if File.exist?(rb_file)
    end
  end
end
each_rb_file_from_base_path(base_path) { |rb_file| ... } click to toggle source
# File lib/truck/const_resolver.rb, line 43
def each_rb_file_from_base_path(base_path)
  base_path.ascend do |path|
    return if path == context.root
    rb_file = path.sub_ext '.rb'
    yield rb_file if rb_file.file?
  end
end
expected_const_defined_in_rb_file(rb_file) click to toggle source
# File lib/truck/const_resolver.rb, line 79
def expected_const_defined_in_rb_file(rb_file)
  rel_path = rb_file.sub_ext('').relative_path_from(current_path).to_s
  matcher = %r{\A(#{StringInflections.to_camel_case rel_path})}i
  expanded_const.match(matcher).captures.fetch 0
end
resolve() click to toggle source
# File lib/truck/const_resolver.rb, line 12
def resolve
  check_already_loaded or resolve!
end
resolve!() click to toggle source
# File lib/truck/const_resolver.rb, line 16
def resolve!
  each_possible_rb_file do |rb_file|
    context.load_file rb_file
    check_loaded rb_file
  end
  const_get
end
walk_const_parts(const = expanded_const) click to toggle source
# File lib/truck/const_resolver.rb, line 58
def walk_const_parts(const = expanded_const)
  return to_enum(:walk_const_parts, const) if block_given?
  const.split '::'
end