dry-rb / dry-types

Flexible type system for Ruby with coercions and constraints
https://dry-rb.org/gems/dry-types
MIT License
860 stars 134 forks source link

Document use of custom method in Constructor #401

Closed timstott closed 4 years ago

timstott commented 4 years ago

Add example in Types.Constructor on how to use a custom method.

The documentation will now cover the signature of Types.Constructor(klass, cons = nil, &block) in full.

flash-gordon commented 4 years ago

Hm, it doesn't look right

class User
  def self.build(values)
    new(values)
  end

  def initialize(attributes)
  end
end

UserType = Types.Constructor(User, &:build)

UserType.(name: 'John') # => Dry::Types::CoercionError: undefined method `build' for {:name=>"John"}:Hash

@timstott what did you try to explain with that example?

timstott commented 4 years ago

You are absolutely correct @flash-gordon, it should be

UserType = Types.Constructor(User, User.method(:build))

I pushed a fix in #402, and apologize for the mix-up.