These are chat archives for dry-rb/chat

Oct 2016
Ricardo Bernardeli
Oct 13 2016 03:46
Hi guys! Hope you're all well. Playing with dry-struct and dry-types, and wanted to have an array with members of two types, where both types have same attribute BUT different type. Example below:
require "dry-struct"
require "dry-types"

module Types
  include Dry::Types.module

class Answer < Dry::Struct
  attribute :question_id, Types::String

  class Select < self
    attribute :value, Types::Hash.schema(id: Types::Int)

  class Text < self
    attribute :value, Types::String

class Survey < Dry::Struct
  attribute :answers, Types::Array.member(Answer::Text | Answer::Select)
  answers: [ 1, value: {id: 10}), 1, value: "foo")

#<Survey answers=[#<Answer::Text question_id=1 value={:id=>10}>, #<Answer::Text question_id=1 value="foo">]>
Problem here is, both answers came back as Answer::Text.
If I invert the order of the members to .member(Answer::Select | Answer::Text), things blow up.
The other thing I tried too is to have .member(Answer) since Select and Text are subclasses of Answer.
It doesn't blow up but turns Answer::Select into an Answer, getting rid of attribute :value.
Ricardo Bernardeli
Oct 13 2016 03:52
How can I achieve members of an array to accepting subclasses of a type?
Appreciate if someone can shed some light for me! :)
Tim Riley
Oct 13 2016 04:03
If you could both post these up to, we’ll see if we can get some answers for you - this is where we’re trying to handle support questions like this.
Ricardo Bernardeli
Oct 13 2016 04:05
Cool - will do!
Thanks Tim!
Tim Riley
Oct 13 2016 04:10