dry-rb / dry-effects

Algebraic effects in Ruby
https://dry-rb.org/gems/dry-effects/
MIT License
112 stars 20 forks source link

Add type check to state and reader providers #43

Closed flash-gordon closed 5 years ago

flash-gordon commented 5 years ago

Normally, you want state to be typed:

class SetLanguage
  include Dry::Effects::Handler.Reader(:lang, type: Symbol)

  def call(env)
    # detect_lang has to return a Symbol
    with_lang(detect_lang(env)) { @app.(env) }
  end
end
class Authenticate
  include Dry::Effects::Handler.State(:current_user, type: Entities::User)

  def call(env)
    with_user do
      # will raise if authenticate returns anything different from Entities::User
      self.user = authenticate(env)
      @app.(env)
    end
  end
end