Class: Dry::System::Booter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/system/booter.rb,
lib/dry/system/booter/component_registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Default booter implementation

This is currently configured by default for every System::Container. Booter objects are responsible for loading system/boot files and expose an API for calling lifecycle triggers.

Defined Under Namespace

Classes: ComponentRegistry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Booter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Booter.



24
25
26
27
28
# File 'lib/dry/system/booter.rb', line 24

def initialize(path)
  @path = path
  @booted = []
  @components = ComponentRegistry.new
end

Instance Attribute Details

#bootedObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/dry/system/booter.rb', line 19

def booted
  @booted
end

#componentsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
# File 'lib/dry/system/booter.rb', line 21

def components
  @components
end

#pathObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/dry/system/booter.rb', line 17

def path
  @path
end

Instance Method Details

#boot_dependency(component) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



166
167
168
169
# File 'lib/dry/system/booter.rb', line 166

def boot_dependency(component)
  boot_file = boot_file(component)
  start(boot_file.basename('.*').to_s.to_sym) if boot_file.exist?
end

#boot_file(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
40
# File 'lib/dry/system/booter.rb', line 36

def boot_file(name)
  name = name.respond_to?(:root_key) ? name.root_key.to_s : name

  path.join("#{name}#{RB_EXT}")
end

#boot_filesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



161
162
163
# File 'lib/dry/system/booter.rb', line 161

def boot_files
  Dir["#{path}/**/#{RB_GLOB}"]
end

#bootable?(component) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


31
32
33
# File 'lib/dry/system/booter.rb', line 31

def bootable?(component)
  boot_file(component).exist?
end

#call(name_or_component) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dry/system/booter.rb', line 121

def call(name_or_component)
  with_component(name_or_component) do |component|
    unless component
      raise ComponentFileMismatchError.new(name, registered_booted_keys)
    end

    yield(component) if block_given?

    component
  end
end

#finalize!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dry/system/booter.rb', line 60

def finalize!
  boot_files.each do |path|
    load_component(path)
  end

  components.each do |component|
    start(component)
  end

  freeze
end

#init(name_or_component) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



82
83
84
85
86
87
88
89
90
91
# File 'lib/dry/system/booter.rb', line 82

def init(name_or_component)
  with_component(name_or_component) do |component|
    call(component) do
      component.init.finalize
      yield if block_given?
    end

    self
  end
end

#lifecycle_container(container) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



134
135
136
# File 'lib/dry/system/booter.rb', line 134

def lifecycle_container(container)
  LifecycleContainer.new(container)
end

#load_component(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
54
55
56
57
# File 'lib/dry/system/booter.rb', line 49

def load_component(path)
  identifier = Pathname(path).basename(RB_EXT).to_s.to_sym

  unless components.exists?(identifier)
    require path
  end

  self
end

#register_component(component) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
# File 'lib/dry/system/booter.rb', line 43

def register_component(component)
  components.register(component)
  self
end

#require_boot_file(identifier) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



155
156
157
158
# File 'lib/dry/system/booter.rb', line 155

def require_boot_file(identifier)
  boot_file = boot_files.detect { |path| Pathname(path).basename(RB_EXT).to_s == identifier.to_s }
  require boot_file if boot_file
end

#shutdownObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
79
# File 'lib/dry/system/booter.rb', line 73

def shutdown
  components.each do |component|
    next unless booted.include?(component)

    stop(component)
  end
end

#start(name_or_component) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dry/system/booter.rb', line 94

def start(name_or_component)
  with_component(name_or_component) do |component|
    return self if booted.include?(component)

    init(name_or_component) do
      component.start
    end

    booted << component.finalize

    self
  end
end

#stop(name_or_component) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



109
110
111
112
113
114
115
116
117
118
# File 'lib/dry/system/booter.rb', line 109

def stop(name_or_component)
  call(name_or_component) do |component|
    raise ComponentNotStartedError.new(name_or_component) unless booted.include?(component)

    component.stop
    booted.delete(component)

    yield if block_given?
  end
end

#with_component(id_or_component) {|component| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (component)

Raises:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dry/system/booter.rb', line 139

def with_component(id_or_component)
  component =
    case id_or_component
    when Symbol
      require_boot_file(id_or_component) unless components.exists?(id_or_component)
      components[id_or_component]
    when Components::Bootable
      id_or_component
    end

  raise InvalidComponentError, id_or_component unless component

  yield(component)
end