Class: Dry::System::Components::Bootable
- Inherits:
-
Object
- Object
- Dry::System::Components::Bootable
- Defined in:
- lib/dry/system/components/bootable.rb
Overview
Bootable components can provide one or more objects and typically depend on 3rd-party code. A typical bootable component can be a database library, or an API client.
These components can be registered via ‘Container.boot` and external component providers can register their components too, which then can be used and configured by your system.
Constant Summary collapse
- DEFAULT_FINALIZE =
proc {}
Instance Attribute Summary collapse
-
#finalize ⇒ Bootable
readonly
private
Automatically called by the booter object after starting a component.
- #identifier ⇒ Object readonly
- #namespace ⇒ Object readonly
- #options ⇒ Object readonly
- #triggers ⇒ Object readonly
Instance Method Summary collapse
-
#after(event, &block) ⇒ Bootable
Specify an after callback.
-
#before(event, &block) ⇒ Bootable
Specify a before callback.
-
#boot? ⇒ TrueClass
private
Return true.
-
#boot_file ⇒ String
private
Return path to component’s boot file.
-
#boot_path ⇒ String
private
Return path to boot dir.
-
#config ⇒ Dry::Struct
Return component’s configuration.
-
#configure(&block) ⇒ Bootable
Configure a component.
-
#container ⇒ Dry::Struct
Return system’s container used by this component.
-
#container_boot_files ⇒ String
private
Return all boot files defined under container’s boot path.
-
#init ⇒ Bootable
Execute ‘init` step.
-
#initialize(identifier, options = {}, &block) ⇒ Bootable
constructor
private
A new instance of Bootable.
-
#new(identifier, new_options = EMPTY_HASH) ⇒ Dry::Struct
private
Return a new instance with updated name and options.
-
#settings(&block) ⇒ Bootable
Define configuration settings with keys and types.
-
#start ⇒ Bootable
Execute ‘start` step.
-
#statuses ⇒ Array<Symbol>
Return a list of lifecycle steps that were executed.
-
#stop ⇒ Bootable
Execute ‘stop` step.
-
#trigger(key, event) ⇒ Bootable
private
Trigger a callback.
-
#with(new_options) ⇒ Dry::Struct
private
Return a new instance with updated options.
Constructor Details
#initialize(identifier, options = {}, &block) ⇒ Bootable
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 Bootable.
69 70 71 72 73 74 75 76 |
# File 'lib/dry/system/components/bootable.rb', line 69 def initialize(identifier, = {}, &block) @identifier = identifier @triggers = { before: Hash.new { |h, k| h[k] = [] }, after: Hash.new { |h, k| h[k] = [] } } @options = block ? .merge(block: block) : @namespace = [:namespace] finalize = [:finalize] || DEFAULT_FINALIZE instance_exec(&finalize) end |
Instance Attribute Details
#finalize ⇒ Bootable (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.
Automatically called by the booter object after starting a component
54 55 56 |
# File 'lib/dry/system/components/bootable.rb', line 54 def finalize @finalize end |
#identifier ⇒ Object (readonly)
50 51 52 |
# File 'lib/dry/system/components/bootable.rb', line 50 def identifier @identifier end |
#namespace ⇒ Object (readonly)
66 67 68 |
# File 'lib/dry/system/components/bootable.rb', line 66 def namespace @namespace end |
#options ⇒ Object (readonly)
58 59 60 |
# File 'lib/dry/system/components/bootable.rb', line 58 def @options end |
#triggers ⇒ Object (readonly)
62 63 64 |
# File 'lib/dry/system/components/bootable.rb', line 62 def triggers @triggers end |
Instance Method Details
#after(event, &block) ⇒ Bootable
Specify an after callback
127 128 129 130 |
# File 'lib/dry/system/components/bootable.rb', line 127 def after(event, &block) triggers[:after][event] << block self end |
#before(event, &block) ⇒ Bootable
Specify a before callback
117 118 119 120 |
# File 'lib/dry/system/components/bootable.rb', line 117 def before(event, &block) triggers[:before][event] << block self end |
#boot? ⇒ TrueClass
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.
Return true
234 235 236 |
# File 'lib/dry/system/components/bootable.rb', line 234 def boot? true end |
#boot_file ⇒ String
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.
Return path to component’s boot file
243 244 245 246 |
# File 'lib/dry/system/components/bootable.rb', line 243 def boot_file container_boot_files. detect { |path| Pathname(path).basename(RB_EXT).to_s == identifier.to_s } end |
#boot_path ⇒ String
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.
Return path to boot dir
253 254 255 |
# File 'lib/dry/system/components/bootable.rb', line 253 def boot_path container.boot_path end |
#config ⇒ Dry::Struct
Return component’s configuration
161 162 163 164 165 166 167 |
# File 'lib/dry/system/components/bootable.rb', line 161 def config if @config @config else configure! end end |
#configure(&block) ⇒ Bootable
Configure a component
137 138 139 |
# File 'lib/dry/system/components/bootable.rb', line 137 def configure(&block) @config_block = block end |
#container ⇒ Dry::Struct
Return system’s container used by this component
183 184 185 |
# File 'lib/dry/system/components/bootable.rb', line 183 def container .fetch(:container) end |
#container_boot_files ⇒ String
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.
Return all boot files defined under container’s boot path
262 263 264 |
# File 'lib/dry/system/components/bootable.rb', line 262 def container_boot_files Dir[container.boot_path.join("**/#{RB_GLOB}")] end |
#init ⇒ Bootable
Execute ‘init` step
83 84 85 86 87 88 |
# File 'lib/dry/system/components/bootable.rb', line 83 def init trigger(:before, :init) lifecycle.(:init) trigger(:after, :init) self end |
#new(identifier, new_options = EMPTY_HASH) ⇒ Dry::Struct
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.
Return a new instance with updated name and options
216 217 218 |
# File 'lib/dry/system/components/bootable.rb', line 216 def new(identifier, = EMPTY_HASH) self.class.new(identifier, .merge()) end |
#settings(&block) ⇒ Bootable
Define configuration settings with keys and types
146 147 148 149 150 151 152 153 154 |
# File 'lib/dry/system/components/bootable.rb', line 146 def settings(&block) if block @settings_block = block elsif @settings_block @settings = Settings::DSL.new(identifier, &@settings_block).call else @settings end end |
#start ⇒ Bootable
Execute ‘start` step
95 96 97 98 99 100 |
# File 'lib/dry/system/components/bootable.rb', line 95 def start trigger(:before, :start) lifecycle.(:start) trigger(:after, :start) self end |
#statuses ⇒ Array<Symbol>
Return a list of lifecycle steps that were executed
174 175 176 |
# File 'lib/dry/system/components/bootable.rb', line 174 def statuses lifecycle.statuses end |
#stop ⇒ Bootable
Execute ‘stop` step
107 108 109 110 |
# File 'lib/dry/system/components/bootable.rb', line 107 def stop lifecycle.(:stop) self end |
#trigger(key, event) ⇒ Bootable
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.
Trigger a callback
204 205 206 207 208 209 |
# File 'lib/dry/system/components/bootable.rb', line 204 def trigger(key, event) triggers[key][event].each do |fn| container.instance_exec(lifecycle.container, &fn) end self end |
#with(new_options) ⇒ Dry::Struct
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.
Return a new instance with updated options
225 226 227 |
# File 'lib/dry/system/components/bootable.rb', line 225 def with() self.class.new(identifier, .merge()) end |