class Truck::Context

Attributes

autoload_paths[RW]

Public Class Methods

new(name, root, parent, autoload_paths) click to toggle source
# File lib/truck/context.rb, line 5
def initialize(name, root, parent, autoload_paths)
  @name   = name
  @root   = Pathname(root)
  @parent = parent
  @autoload_paths = expand_autoload_paths autoload_paths
end
owner(const) click to toggle source
# File lib/truck/context.rb, line 13
def owner(const)
  owner, _ = Autoloader.owner_and_ascending_nibbles const
  owner
end

Public Instance Methods

boot!() click to toggle source
# File lib/truck/context.rb, line 19
def boot!
  parent.const_set name, build_mod
end
booted?() click to toggle source
# File lib/truck/context.rb, line 32
def booted?
  mod ? true : false
end
context_for?(other_mod) click to toggle source
# File lib/truck/context.rb, line 28
def context_for?(other_mod)
  mod == other_mod or other_mod.included_modules.include? mod
end
eager_load!() click to toggle source
# File lib/truck/context.rb, line 36
def eager_load!
  Dir[root.join('**/*.rb')].each do |rb_file|
    load_file rb_file
  end
end
load_file(rb_file) click to toggle source
# File lib/truck/context.rb, line 42
def load_file(rb_file)
  ruby_code = File.read rb_file
  mod.module_eval ruby_code, rb_file.to_s
end
mod() click to toggle source
# File lib/truck/context.rb, line 23
def mod
  return nil unless parent and parent.const_defined? name
  parent.const_get name 
end
parent() click to toggle source
# File lib/truck/context.rb, line 47
def parent
  return Object unless @parent
  Truck.contexts.fetch(@parent.to_sym).mod
end
resolve_const(expanded_const, skip = nil) click to toggle source
# File lib/truck/context.rb, line 52
def resolve_const(expanded_const, skip = nil)
  build_const_resolver(expanded_const, Array[skip]).resolve
end
shutdown!() click to toggle source
# File lib/truck/context.rb, line 56
def shutdown!
  parent.send :remove_const, name
end

Private Instance Methods

build_const_resolver(expanded_const, skip_files) click to toggle source
# File lib/truck/context.rb, line 62
def build_const_resolver(expanded_const, skip_files)
  ConstResolver.new self, String(expanded_const).dup.freeze, skip_files
end
build_mod() click to toggle source
# File lib/truck/context.rb, line 66
def build_mod
  mod = Module.new
  mod.singleton_class.class_exec root do |__root__|
    define_method :root do __root__ ; end
  end
  mod
end
expand_autoload_paths(paths) click to toggle source
# File lib/truck/context.rb, line 74
def expand_autoload_paths(paths)
  paths.map do |path| root.join path; end
end