Module: Dry::Effects

Extended by:
Core::Extensions
Includes:
Core::Constants
Defined in:
lib/dry/effects.rb,
lib/dry/effects/all.rb,
lib/dry/effects/halt.rb,
lib/dry/effects/frame.rb,
lib/dry/effects/stack.rb,
lib/dry/effects/effect.rb,
lib/dry/effects/errors.rb,
lib/dry/effects/handler.rb,
lib/dry/effects/version.rb,
lib/dry/effects/provider.rb,
lib/dry/effects/container.rb,
lib/dry/effects/inflector.rb,
lib/dry/effects/effects/cmp.rb,
lib/dry/effects/effects/env.rb,
lib/dry/effects/initializer.rb,
lib/dry/effects/instruction.rb,
lib/dry/effects/constructors.rb,
lib/dry/effects/effects/fork.rb,
lib/dry/effects/effects/lock.rb,
lib/dry/effects/instructions.rb,
lib/dry/effects/effects/async.rb,
lib/dry/effects/effects/cache.rb,
lib/dry/effects/effects/defer.rb,
lib/dry/effects/effects/retry.rb,
lib/dry/effects/effects/state.rb,
lib/dry/effects/providers/cmp.rb,
lib/dry/effects/providers/env.rb,
lib/dry/effects/effects/random.rb,
lib/dry/effects/effects/reader.rb,
lib/dry/effects/providers/fork.rb,
lib/dry/effects/providers/lock.rb,
lib/dry/effects/effects/resolve.rb,
lib/dry/effects/effects/timeout.rb,
lib/dry/effects/providers/async.rb,
lib/dry/effects/providers/cache.rb,
lib/dry/effects/providers/defer.rb,
lib/dry/effects/providers/retry.rb,
lib/dry/effects/providers/state.rb,
lib/dry/effects/effects/implicit.rb,
lib/dry/effects/effects/parallel.rb,
lib/dry/effects/providers/random.rb,
lib/dry/effects/providers/reader.rb,
lib/dry/effects/effects/interrupt.rb,
lib/dry/effects/effects/timestamp.rb,
lib/dry/effects/extensions/system.rb,
lib/dry/effects/providers/resolve.rb,
lib/dry/effects/providers/timeout.rb,
lib/dry/effects/instructions/raise.rb,
lib/dry/effects/providers/implicit.rb,
lib/dry/effects/providers/parallel.rb,
lib/dry/effects/providers/interrupt.rb,
lib/dry/effects/providers/timestamp.rb,
lib/dry/effects/effects/current_time.rb,
lib/dry/effects/instructions/execute.rb,
lib/dry/effects/extensions/auto_inject.rb,
lib/dry/effects/providers/current_time.rb,
lib/dry/effects/provider/class_interface.rb,
lib/dry/effects/providers/current_time/time_generators.rb

Defined Under Namespace

Modules: Constructors, Effects, Errors, Initializer, Instructions, Providers, System Classes: Container, DryAutoEffectsStrategies, Effect, Error, Frame, Halt, Handler, Instruction, Provider, Stack

Constant Summary collapse

VERSION =
"0.4.1"
Inflector =
::Dry::Inflector.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.effectsObject (readonly)

Returns the value of attribute effects.



42
43
44
# File 'lib/dry/effects.rb', line 42

def effects
  @effects
end

.providersObject (readonly)

Returns the value of attribute providers.



42
43
44
# File 'lib/dry/effects.rb', line 42

def providers
  @providers
end

Class Method Details

.[]Handler

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

Examples:

providing current user


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

Parameters:

  • args (Array<Object>)

    Handler parameters

Returns:



88
89
90
# File 'lib/dry/effects.rb', line 88

def [](...)
  Handler.new(...)
end

.AutoInject(dynamic: false) ⇒ Object



66
67
68
69
# File 'lib/dry/effects/extensions/auto_inject.rb', line 66

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

.loaderObject

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.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dry/effects.rb', line 28

def loader
  @loader ||= ::Zeitwerk::Loader.new.tap do |loader|
    root = ::File.expand_path("..", __dir__)
    loader.tag = "dry-effects"
    loader.inflector = ::Zeitwerk::GemInflector.new("#{root}/dry-effects.rb")
    loader.push_dir(root)
    loader.ignore(
      "#{root}/dry-effects.rb",
      "#{root}/dry/effects/{all,error,extensions,inflector,initializer,version}.rb",
      "#{root}/dry/effects/extensions/**/*.rb"
    )
  end
end

.yield(effect) ⇒ Object

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.

Examples:

getting current user with yield


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

Parameters:

Returns:

  • (Object)

    Result value is determined by effect type



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dry/effects.rb', line 59

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