Class: RKit::Core::Loader

Inherits:
Object show all
Defined in:
lib/r_kit/core/loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Loader

Returns a new instance of Loader.



15
16
17
18
19
# File 'lib/r_kit/core/loader.rb', line 15

def initialize base
  @_base = base
  @load_paths = []
  @dependencies = []
end

Instance Attribute Details

#_baseObject

Returns the value of attribute _base.



2
3
4
# File 'lib/r_kit/core/loader.rb', line 2

def _base
  @_base
end

#dependenciesObject

Returns the value of attribute dependencies.



2
3
4
# File 'lib/r_kit/core/loader.rb', line 2

def dependencies
  @dependencies
end

#load_pathsObject

Returns the value of attribute load_paths.



2
3
4
# File 'lib/r_kit/core/loader.rb', line 2

def load_paths
  @load_paths
end

Class Method Details

.loadedObject



6
7
8
# File 'lib/r_kit/core/loader.rb', line 6

def self.loaded
  @loaded.map{ |name| name.demodulize.underscore }
end

.loaded?(name) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/r_kit/core/loader.rb', line 10

def self.loaded? name
  @loaded.include? name
end

Instance Method Details

#dependencies!Object



30
31
32
# File 'lib/r_kit/core/loader.rb', line 30

def dependencies!
  dependencies.each &:load!
end

#dependency(*services) ⇒ Object

TODO: when a dependency is added, we must define in the opposite service a “dependency forbidden” TODO: in order to avoid recursive dependency



24
25
26
27
28
# File 'lib/r_kit/core/loader.rb', line 24

def dependency *services
  services.each do |service|
    dependencies << Dependency.new(_base, service: service)
  end
end

#load!Object



51
52
53
54
55
# File 'lib/r_kit/core/loader.rb', line 51

def load!
  dependencies!
  load_paths!
  loaded!
end

#load_path(file, *paths, **options) ⇒ Object



35
36
37
38
39
# File 'lib/r_kit/core/loader.rb', line 35

def load_path file, *paths, **options
  paths.each do |path|
    load_paths << LoadPath.new(_base, file: file, path: path, **options.slice(:priority, :if, :unless))
  end
end

#load_paths!Object



41
42
43
# File 'lib/r_kit/core/loader.rb', line 41

def load_paths!
  load_paths.sort{ |a, b| a.priority <=> b.priority }.each &:load!
end

#loaded!Object



46
47
48
# File 'lib/r_kit/core/loader.rb', line 46

def loaded!
  self.class.instance_variable_get("@loaded") << _base.name
end