These are chat archives for dry-rb/chat

29th
Sep 2017
Ahmad A.Elghany
@ahmgeek
Sep 29 2017 07:59
hey folks, if I used .default(false) for one of the attributes, and I passed nil it convert it to the default value used
why not throwing an error?
Markus Unterwaditzer
@untitaker
Sep 29 2017 11:08
Is there a way to define multiple constructors for different data sources on a single struct? E.g. I (probably) want to use the schemaconstructor when instantiating my struct from the database, but want to use strict_with_defaults everywhere else
I have the feeling that dry-rb wants me to have structs that are specific to the datasources I am pulling their content from, but since I'm refactoring an old codebase here, it would be a ton of work
Nikita Shilnikov
@flash-gordon
Sep 29 2017 11:10
@untitaker there is Struct.constructor that takes a block
Markus Unterwaditzer
@untitaker
Sep 29 2017 12:15
@flash-gordon I forgot to thank you for that, that seems good.
other question: what's up with this? https://gist.github.com/untitaker/a41afabcdf608c68f86cdbe854f9b905 I would've assumed it raises a validation error, but instead it raises NoMethodError
it works fine for valid input
Nikita Shilnikov
@flash-gordon
Sep 29 2017 13:30
@untitaker what's the stacktrace of the error? :)
Markus Unterwaditzer
@untitaker
Sep 29 2017 13:31
Markus Unterwaditzer
@untitaker
Sep 29 2017 13:41
@flash-gordon ^
Nikita Shilnikov
@flash-gordon
Sep 29 2017 14:42
@untitaker something's wrong there, could you file an issue to dry-struct with that example?^
Alexey Badenkov
@badenkov
Sep 29 2017 18:35
Hello all!
@flash-gordon, I would like to ask you about your example from your presentation:
How I can check that some instance of Events::PasswordChanged class is kind of Events::Update
module Events
  class PasswordChanged < Dry::Struct
    attribute :user_id, Types::UUID
    attribute :password, Types::String
  end

  class EmailChanged < Dry::Struct
    attribute :user_id, Types::UUID
    attribute :email, Types::String
  end

  Updated = PasswordChanged | EmailChanged
end
Nikita Shilnikov
@flash-gordon
Sep 29 2017 21:25
@badenkov Events::Update[event] <- this either will raise an error or return the value back
Alexey Badenkov
@badenkov
Sep 29 2017 21:26
Thanks! I found Events::Updated.valid?(event) also.
Nikita Shilnikov
@flash-gordon
Sep 29 2017 21:26
or you can use try if you don't want to raise an exception, although structs are supposed to be used with valid input where if anything doesn't match the format raises an exception
right, valid? is another option