module Dry::Effects

Constants

Inflector
VERSION

Attributes

effects[R]
providers[R]

Public Class Methods

AutoInject(dynamic: false) click to toggle source
# File lib/dry/effects/extensions/auto_inject.rb, line 62
def self.AutoInject(dynamic: false)
  mod = Dry.AutoInject(EMPTY_HASH, strategies: DryAutoEffectsStrategies)
  dynamic ? mod.dynamic : mod
end
[](*args) click to toggle source

Build a handler. Normally, handlers are built via mixins. This method is useful for demonstration purposes.

@example providing current user

Dry::Effects[:reader, :current_user].(User.new) do
  code_using_current_user.()
end

@param [Array<Object>] args Handler parameters @return [Handler] @api public

# File lib/dry/effects.rb, line 65
def [](*args)
  Handler.new(*args)
end
yield(effect) { |effect, e| ... } click to toggle source

Handle an effect. If no handler is present in the stack it will either raise an exception and yield a block if given. It is not recommended to build effects manually, hence this method shouldn't be used often.

@example getting current user with yield

require 'dry/effects/effects/reader'
extend Dry::Effects::Constructors
Dry::Effects.yield(Read(:current_user))

@param [Effect] effect @return [Object] Result value is determined by effect type @api public

# File lib/dry/effects.rb, line 36
def yield(effect)
  result = ::Fiber.yield(effect)

  if result.is_a?(Instruction)
    result.()
  else
    result
  end
rescue ::FiberError => e
  if block_given?
    yield(effect, e)
  else
    raise Errors::UnhandledEffectError, effect
  end
end