Sha256: dc399cd63154295db771bb8911e2a74f632d550dbab917ca3509ff04aff28e9f

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require "dry/system/container"

Dry::Effects.load_extensions(:auto_inject)

module Dry
  module Effects
    module System
      class AutoRegistrar < ::Dry::System::AutoRegistrar
        # Always memoize and freeze registered components
        def call(component_dir)
          components(component_dir).each do |component|
            next unless register_component?(component)

            container.register(component.key, memoize: true) { component.instance.freeze }
          end
        end
      end

      class Container < ::Dry::System::Container
        config.auto_registrar = AutoRegistrar

        def self.injector(effects: true, **kwargs)
          if effects
            Dry::Effects.AutoInject(**kwargs)
          else
            super()
          end
        end

        def self.finalize!
          return self if finalized?

          super

          # Force all components to load
          each_key { |key| resolve(key) }
          self
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-effects-0.2.0 lib/dry/effects/extensions/system.rb