Class: Truck::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/truck/context.rb,
lib/truck/const_resolver.rb

Defined Under Namespace

Classes: ConstResolver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent: nil, root:, autoload_paths: ['.']) ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
# File 'lib/truck/context.rb', line 5

def initialize(name, parent: nil, root:, autoload_paths: ['.'])
  @name   = name
  @root   = Pathname(root)
  @parent = parent
  @autoload_paths = expand_autoload_paths autoload_paths
end

Instance Attribute Details

#autoload_pathsObject (readonly)

Returns the value of attribute autoload_paths.



3
4
5
# File 'lib/truck/context.rb', line 3

def autoload_paths
  @autoload_paths
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/truck/context.rb', line 3

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/truck/context.rb', line 3

def root
  @root
end

Class Method Details

.owner(const) ⇒ Object



13
14
15
16
# File 'lib/truck/context.rb', line 13

def owner(const)
  owner, _ = Autoloader.owner_and_ascending_nibbles const
  owner
end

Instance Method Details

#boot!Object



19
20
21
# File 'lib/truck/context.rb', line 19

def boot!
  parent.const_set name, build_mod
end

#booted?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/truck/context.rb', line 32

def booted?
  mod ? true : false
end

#context_for?(other_mod) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# 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!Object



36
37
38
39
40
# 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) ⇒ Object



42
43
44
45
# 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

#modObject



23
24
25
26
# File 'lib/truck/context.rb', line 23

def mod
  return nil unless parent and parent.const_defined? name
  parent.const_get name 
end

#parentObject



47
48
49
50
# 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) ⇒ Object



52
53
54
# File 'lib/truck/context.rb', line 52

def resolve_const(expanded_const, skip: nil)
  build_const_resolver(expanded_const, Array[skip]).resolve
end

#shutdown!Object



56
57
58
# File 'lib/truck/context.rb', line 56

def shutdown!
  parent.send :remove_const, name
end