Class: Pico::Context
- Inherits:
-
Object
- Object
- Pico::Context
- Defined in:
- lib/pico/context.rb,
lib/pico/context/const_resolver.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ConstResolver
Instance Attribute Summary collapse
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
-
#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
- #build_mod ⇒ Object
- #eager_load! ⇒ Object
-
#initialize(name, parent: nil, root:) ⇒ Context
constructor
A new instance of Context.
- #load_file(rb_file) ⇒ Object
- #parent ⇒ Object
- #possible_implicit_namespace?(path) ⇒ Boolean
- #reload! ⇒ Object
- #resolve_const(expanded_const) ⇒ Object
- #shutdown! ⇒ Object
Constructor Details
Instance Attribute Details
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
5 6 7 |
# File 'lib/pico/context.rb', line 5 def mod @mod end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/pico/context.rb', line 5 def name @name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
5 6 7 |
# File 'lib/pico/context.rb', line 5 def root @root end |
Class Method Details
.owner(const) ⇒ Object
15 16 17 18 |
# File 'lib/pico/context.rb', line 15 def owner(const) owner, _ = Autoloader.owner_and_ascending_nibbles const owner end |
Instance Method Details
#boot! ⇒ Object
25 26 27 |
# File 'lib/pico/context.rb', line 25 def boot! parent.const_set name, mod end |
#booted? ⇒ Boolean
29 30 31 |
# File 'lib/pico/context.rb', line 29 def booted? parent.const_defined? name end |
#build_mod ⇒ Object
21 22 23 |
# File 'lib/pico/context.rb', line 21 def build_mod Module.new end |
#eager_load! ⇒ Object
33 34 35 36 37 |
# File 'lib/pico/context.rb', line 33 def eager_load! Dir[root.join('**/*.rb')].each do |rb_file| load_file rb_file end end |
#load_file(rb_file) ⇒ Object
39 40 41 |
# File 'lib/pico/context.rb', line 39 def load_file(rb_file) mod.module_eval File.read(rb_file), rb_file.to_s end |
#parent ⇒ Object
43 44 45 46 |
# File 'lib/pico/context.rb', line 43 def parent return Object unless @parent Pico.contexts.fetch(@parent.to_sym).mod end |
#possible_implicit_namespace?(path) ⇒ Boolean
54 55 56 |
# File 'lib/pico/context.rb', line 54 def possible_implicit_namespace?(path) root.join(path).directory? end |
#reload! ⇒ Object
48 49 50 51 52 |
# File 'lib/pico/context.rb', line 48 def reload! shutdown! @mod = build_mod boot! end |
#resolve_const(expanded_const) ⇒ Object
58 59 60 |
# File 'lib/pico/context.rb', line 58 def resolve_const() build_const_resolver().resolve end |
#shutdown! ⇒ Object
62 63 64 |
# File 'lib/pico/context.rb', line 62 def shutdown! parent.send(:remove_const, name) end |