Class: Rodakase::Container

Inherits:
Object
  • Object
show all
Extended by:
Dry::Container::Mixin
Defined in:
lib/rodakase/container.rb

Class Method Summary collapse

Class Method Details

.auto_register!(dir, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rodakase/container.rb', line 59

def self.auto_register!(dir, &block)
  dir_root = root.join(dir.to_s.split('/')[0])

  Dir["#{root}/#{dir}/**/*.rb"].each do |path|
    component_path = path.to_s.gsub("#{dir_root}/", '').gsub('.rb', '')
    component = Rodakase.Component(component_path)

    next if key?(component.identifier)

    Kernel.require component.path

    if block
      register(component.identifier, yield(component.constant))
    else
      register(component.identifier) { component.instance }
    end
  end

  self
end

.boot!(name) ⇒ Object



80
81
82
# File 'lib/rodakase/container.rb', line 80

def self.boot!(name)
  require "core/boot/#{name}.rb"
end

.configure(env = config.env) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rodakase/container.rb', line 17

def self.configure(env = config.env, &block)
  if !configured?
    super() do |config|
      app_config = Config.load(root, env)
      config.app = app_config if app_config
    end

    load_paths!('core')

    @_configured = true
  end

  yield(self)

  self
end

.configured?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rodakase/container.rb', line 34

def self.configured?
  @_configured
end

.finalize! {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rodakase/container.rb', line 38

def self.finalize!(&block)
  yield(self) if block

  Dir[root.join('core/boot/**/*.rb')].each(&method(:require))

  if config.auto_register
    Array(config.auto_register).each(&method(:auto_register!))
  end

  freeze
end

.import_moduleObject



50
51
52
53
54
55
56
57
# File 'lib/rodakase/container.rb', line 50

def self.import_module
  auto_inject = Dry::AutoInject(self)

  -> *keys {
    keys.each { |key| load_component(key) unless key?(key) }
    auto_inject[*keys]
  }
end

.load_component(key) ⇒ Object



94
95
96
# File 'lib/rodakase/container.rb', line 94

def self.load_component(key)
  require_component(key) { |klass| register(key) { klass.new } }
end

.load_pathsObject



123
124
125
# File 'lib/rodakase/container.rb', line 123

def self.load_paths
  @_load_paths ||= []
end

.load_paths!(*dirs) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/rodakase/container.rb', line 114

def self.load_paths!(*dirs)
  dirs.map(&:to_s).each do |dir|
    path = root.join(dir)
    load_paths << path
    $LOAD_PATH.unshift(path.to_s)
  end
  self
end

.require(*paths) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/rodakase/container.rb', line 84

def self.require(*paths)
  paths
    .flat_map { |path|
      path.include?('*') ? Dir[root.join(path)] : root.join(path)
    }
    .each { |path|
      Kernel.require path
    }
end

.require_component(key, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rodakase/container.rb', line 98

def self.require_component(key, &block)
  component = Rodakase.Component(key)
  path = load_paths.detect { |p| p.join(component.file).exist? }

  if path
    Kernel.require component.path
    yield(component.constant) if block
  else
    raise ArgumentError, "could not resolve require file for #{key}"
  end
end

.rootObject



110
111
112
# File 'lib/rodakase/container.rb', line 110

def self.root
  config.root
end