Sha256: 8e5ce6fed9bebee52d043eb47ae27eb146e345105f32e86fb2f86c5f288686c3

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require "concurrent/map"
require "dry/auto_inject/strategies/constructor"

module Dry
  module Effects
    class DryAutoEffectsStrategies
      extend ::Dry::Container::Mixin

      class Base < AutoInject::Strategies::Constructor
        private

        def define_new
          # nothing to do
        end

        def define_initialize(_)
          # nothing to do
        end
      end

      class Static < Base
        private

        def define_readers(dynamic: false)
          map = dependency_map.to_h
          cache = ::Concurrent::Map.new
          instance_mod.class_exec do
            map.each do |name, identifier|
              resolve = ::Dry::Effects::Constructors::Resolve(identifier)

              if dynamic
                define_method(name) { ::Dry::Effects.yield(resolve) }
              else
                define_method(name) do
                  cache.fetch_or_store(name) do
                    ::Dry::Effects.yield(resolve)
                  end
                end
              end
            end
          end
          self
        end
      end

      class Dynamic < Static
        private

        def define_readers(dynamic: true)
          super
        end
      end

      register :static, Static
      register :dynamic, Dynamic
      register :default, Static
    end

    def self.AutoInject(dynamic: false)
      mod = Dry.AutoInject(EMPTY_HASH, strategies: DryAutoEffectsStrategies)
      dynamic ? mod.dynamic : mod
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-effects-0.3.1 lib/dry/effects/extensions/auto_inject.rb