Class: Pico::Context

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

Direct Known Subclasses

Application

Defined Under Namespace

Classes: ConstResolver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent: nil, root:) ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
# File 'lib/pico/context.rb', line 7

def initialize(name, parent: nil, root:)
  @mod    = build_mod
  @name   = name
  @root   = Pathname(root) if root
  @parent = parent
end

Instance Attribute Details

#modObject (readonly)

Returns the value of attribute mod.



5
6
7
# File 'lib/pico/context.rb', line 5

def mod
  @mod
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/pico/context.rb', line 5

def name
  @name
end

#rootObject (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

Returns:

  • (Boolean)


29
30
31
# File 'lib/pico/context.rb', line 29

def booted?
  parent.const_defined? name
end

#build_modObject



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

#parentObject



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

Returns:

  • (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(expanded_const)
  build_const_resolver(expanded_const).resolve
end

#shutdown!Object



62
63
64
# File 'lib/pico/context.rb', line 62

def shutdown!
  parent.send(:remove_const, name)
end