Sha256: 52e052c54d8b941e42502026082a1efa8082006f4273f9eb088ca9b9448f855e

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require 'dry/core/constants'
require 'dry/effects/version'
require 'dry/effects/container'
require 'dry/effects/errors'
require 'dry/effects/instructions/raise'

module Dry
  module Effects
    include Core::Constants

    class Error < StandardError; end

    @effects = Container.new
    @providers = Container.new

    class << self
      attr_reader :effects, :providers

      # 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
      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

      # 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
      def [](*args)
        Handler.new(*args)
      end
    end
  end
end

require 'dry/effects/all'
require 'dry/effects/extensions'

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dry-effects-0.1.5 lib/dry/effects.rb
dry-effects-0.1.4 lib/dry/effects.rb
dry-effects-0.1.3 lib/dry/effects.rb
dry-effects-0.1.2 lib/dry/effects.rb
dry-effects-0.1.1 lib/dry/effects.rb
dry-effects-0.1.0 lib/dry/effects.rb