Dry equivalent of defining custom Virtus attribute

I’m new to dry-rb. Is there an equivalent using dry-types of creating a custom type derived from Virtus::Attribute? Specifically how to reproduce this example using dry-rb?

Or, how to do eqivalent of these https://github.com/solnic/virtus#custom-coercions.


I thinks something like this might do (not tested, and will likely to fail if input is anything other than an array):

ArrayOfSchools = Dry::Types::Definition
                   .constructor { |input| School.find(input.reject(&:blank?)) }

Not sure that Dry::Types::Definition API is considered public though.

I added docs about the helpers we have for building custom types http://dry-rb.org/gems/dry-types/custom-types/. They are available in 0.12+

1 Like