Class: Truck::Context
- Inherits:
-
Object
- Object
- Truck::Context
- Defined in:
- lib/truck/context.rb,
lib/truck/const_resolver.rb
Defined Under Namespace
Classes: ConstResolver
Instance Attribute Summary collapse
-
#autoload_paths ⇒ Object
readonly
Returns the value of attribute autoload_paths.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #boot! ⇒ Object
- #booted? ⇒ Boolean
- #eager_load! ⇒ Object
-
#initialize(name, parent: nil, root:, autoload_paths: ['.']) ⇒ Context
constructor
A new instance of Context.
- #load_file(rb_file) ⇒ Object
- #mod ⇒ Object
- #parent ⇒ Object
- #resolve_const(expanded_const) ⇒ Object
- #shutdown! ⇒ Object
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 = autoload_paths end |
Instance Attribute Details
#autoload_paths ⇒ Object (readonly)
Returns the value of attribute autoload_paths.
3 4 5 |
# File 'lib/truck/context.rb', line 3 def autoload_paths @autoload_paths end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/truck/context.rb', line 3 def name @name end |
#root ⇒ Object (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
28 29 30 |
# File 'lib/truck/context.rb', line 28 def booted? mod ? true : false end |
#eager_load! ⇒ Object
32 33 34 35 36 |
# File 'lib/truck/context.rb', line 32 def eager_load! Dir[root.join('**/*.rb')].each do |rb_file| load_file rb_file end end |
#load_file(rb_file) ⇒ Object
38 39 40 |
# File 'lib/truck/context.rb', line 38 def load_file(rb_file) mod.module_eval File.read(rb_file), rb_file.to_s end |
#mod ⇒ Object
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 |
#parent ⇒ Object
42 43 44 45 |
# File 'lib/truck/context.rb', line 42 def parent return Object unless @parent Truck.contexts.fetch(@parent.to_sym).mod end |
#resolve_const(expanded_const) ⇒ Object
47 48 49 |
# File 'lib/truck/context.rb', line 47 def resolve_const() build_const_resolver().resolve end |
#shutdown! ⇒ Object
51 52 53 |
# File 'lib/truck/context.rb', line 51 def shutdown! parent.send :remove_const, name end |