lib/dry/system/components/bootable.rb in dry-system-0.12.0 vs lib/dry/system/components/bootable.rb in dry-system-0.13.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'dry/system/lifecycle' require 'dry/system/settings' require 'dry/system/components/config' require 'dry/system/constants' @@ -63,14 +65,16 @@ # @!attribute [r] namespace # @return [Symbol,String] default namespace for the container keys attr_reader :namespace + TRIGGER_MAP = Hash.new { |h, k| h[k] = [] }.freeze + # @api private def initialize(identifier, options = {}, &block) @identifier = identifier - @triggers = { before: Hash.new { |h, k| h[k] = [] }, after: Hash.new { |h, k| h[k] = [] } } + @triggers = { before: TRIGGER_MAP.dup, after: TRIGGER_MAP.dup } @options = block ? options.merge(block: block) : options @namespace = options[:namespace] finalize = options[:finalize] || DEFAULT_FINALIZE instance_exec(&finalize) end @@ -157,15 +161,11 @@ # # @return [Dry::Struct] # # @api public def config - if @config - @config - else - configure! - end + @config || configure! end # Return a list of lifecycle steps that were executed # # @return [Array<Symbol>] @@ -189,11 +189,11 @@ # @return [Bootable] # # @api private def finalize lifecycle.container.each do |key, item| - container.register(key, item) unless container.key?(key) + container.register(key, item) unless container.registered?(key) end self end # Trigger a callback @@ -239,12 +239,12 @@ # # @return [String] # # @api private def boot_file - container_boot_files. - detect { |path| Pathname(path).basename(RB_EXT).to_s == identifier.to_s } + container_boot_files + .detect { |path| Pathname(path).basename(RB_EXT).to_s == identifier.to_s } end # Return path to boot dir # # @return [String] @@ -258,11 +258,11 @@ # # @return [String] # # @api private def container_boot_files - Dir[container.boot_path.join("**/#{RB_GLOB}")] + ::Dir[container.boot_path.join("**/#{RB_GLOB}")].sort end private # Return lifecycle object used for this component @@ -288,10 +288,12 @@ when true container.namespace(identifier) { |c| return c } when nil container else - raise RuntimeError, "+namespace+ boot option must be true, string or symbol #{namespace.inspect} given." + raise <<-STR + +namespace+ boot option must be true, string or symbol #{namespace.inspect} given. + STR end end # Set config object #