Sha256: dadf7f9a454df67fe2e7db7958b290a39aec93e41e6b9e628a9a25c0e1247173

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Dry
  module Effects
    module Providers
      class Reader < Provider[:reader]
        def self.handle_method(scope, as: Undefined, **)
          Undefined.default(as) { :"with_#{scope}" }
        end

        Any = ::Object.new.tap { |any|
          def any.===(_)
            true
          end
        }.freeze

        include ::Dry::Equalizer(:scope, :state, inspect: false)

        attr_reader :state

        param :scope

        option :type, as: :state_type, default: -> { Any }

        def initialize(*, **)
          super

          @state = Undefined
        end

        def read
          state
        end

        # Yield the block with the handler installed
        #
        # @api private
        def call(state)
          case state
          when state_type
            @state = state
            yield
          else
            raise Errors::InvalidValueError.new(state, scope)
          end
        end

        # @return [String]
        # @api public
        def represent
          if Undefined.equal?(state)
            "#{type}[#{scope} not set]"
          else
            "#{type}[#{scope} set]"
          end
        end

        # @return [Boolean]
        # @api public
        def provide?(effect)
          effect.type.equal?(:state) && effect.name.equal?(:read) && scope.equal?(effect.scope)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-effects-0.4.1 lib/dry/effects/providers/reader.rb
dry-effects-0.4.0 lib/dry/effects/providers/reader.rb
dry-effects-0.3.1 lib/dry/effects/providers/reader.rb