Class: Pico::Application

Inherits:
Context show all
Defined in:
lib/pico/application.rb

Defined Under Namespace

Classes: ApplicationModule, ConfigurationContext

Instance Attribute Summary collapse

Attributes inherited from Context

#mod, #name, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Context

#booted?, #eager_load!, #load_file, owner, #parent, #reload!, #resolve_const, #shutdown!

Constructor Details

#initialize(name, root: nil, **params, &config_block) ⇒ Application

Returns a new instance of Application.



5
6
7
8
9
10
# File 'lib/pico/application.rb', line 5

def initialize(name, root: nil, **params, &config_block)
  super name, root: root, **params
  @autoload_paths = []
  @injector = build_injector
  configure(config_block) if block_given?
end

Instance Attribute Details

#autoload_pathsObject (readonly)

Returns the value of attribute autoload_paths.



3
4
5
# File 'lib/pico/application.rb', line 3

def autoload_paths
  @autoload_paths
end

Class Method Details

.default_rootObject



51
52
53
54
55
56
57
58
# File 'lib/pico/application.rb', line 51

def default_root
  caller_locations.reduce do |previous_location, location|
    if previous_location.label == "boot!" && File.basename(location.path) != "pico.rb"
      return Pathname(location.absolute_path).dirname
    end
    location
  end
end

Instance Method Details

#boot!Object



16
17
18
19
# File 'lib/pico/application.rb', line 16

def boot!
  self.root ||= self.class.default_root
  super
end

#build_const_resolver(expanded_const) ⇒ Object



21
22
23
24
25
# File 'lib/pico/application.rb', line 21

def build_const_resolver(expanded_const)
  resolver = super
  resolver.autoload_paths = autoload_paths
  resolver
end

#configure(config_block) ⇒ Object



12
13
14
# File 'lib/pico/application.rb', line 12

def configure(config_block)
  ConfigurationContext.evaluate(config_block, application: self)
end

#possible_implicit_namespace?(path) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/pico/application.rb', line 27

def possible_implicit_namespace?(path)
  autoload_paths.any? do |autoload_path|
    root.join(autoload_path, path).directory?
  end
end

#root=(dir) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
# File 'lib/pico/application.rb', line 33

def root=(dir)
  raise ArgumentError, "Cannot alter root after booting" if booted?
  @root = Pathname(dir)
end