These are chat archives for dry-rb/chat

9th
Jan 2017
Adam Davies
@adz
Jan 09 2017 06:03

I’m a bit confused with Dry Struct….

It used to be in drytypes: Dry::Types::Struct now its on its own so change to Dry::Struct, right?

What stupid thing am I doing here:

class CreateClientForm < Dry::Struct
  module Types
    include Dry::Types.module
  end

  attribute :name, Types::Form::String
end

which appears to not find “String” constant…

specs in dry struct do:

attribute :name, ‘coercible.string’
is this how I should change things?

Maybe this should be Types::Coercible::String ??
that seems to work… hmmm

I just assumed all ‘form’ values here so use that, but perhaps I should read more closely descriptions of each….

Adam Davies
@adz
Jan 09 2017 07:07

…in
http://dry-rb.org/gems/dry-validation/comparison-with-activemodel/
trying to understand

    option :account
    def unique?(attr_name, value)

and how it’s params line up with

schema.with(object: user_account).call(input)

1) is that ‘object’ key special? I thought it would be ‘account’
2) i guess that the ‘unique?’ method gets curried, so ‘value’ comes in…
but a little down”
required(:email).filled(scoped_unique?: :email, scope?: { active: true })
seems to pass in as a key “scope?:” but receive it as a positional arg… ok i guess… if i have it straight

Adam Davies
@adz
Jan 09 2017 07:39

Just to report back… seems like that doc is wrong. Probably was specialised for ‘account’ then generalised to ‘object’ but not fully.

I think it should be something like:

      option :record

      def unique?(attr_name, value)
        record.class
        .where.not(id: record.id)
        .where(attr_name => value)
        .empty?
      end
Adam Davies
@adz
Jan 09 2017 08:06
Here’s a PR to correct ^ dry-rb/dry-rb.org#138
Tim Riley
@timriley
Jan 09 2017 09:47
@adz we don’t have form types for strings in dry-types since forms will always submit strings anyway. No need for coercion. You could always just use Types::Strict::String in such a case.
@adz Also, the typical setup (so far at least) for handling form submissions is to run the form params through a dry-validation schema first anyway, which’ll handle any form coercions and give you properly structured and typed Ruby values out the other end. This means that you can then avoid having to use form types in your structs.
Adam Davies
@adz
Jan 09 2017 10:02
Yeah I see. Actually did that with dry and ROM project. Was following this write-up to get started in a Rails app
It uses dry struct for form object with dry validation for save call
Tim Riley
@timriley
Jan 09 2017 10:50
oh that’s interesting
George Millo
@georgemillo
Jan 09 2017 16:28
is there a way to use a dry struct as a type in another dry struct?
Tim Riley
@timriley
Jan 09 2017 20:56
@georgemillo yep!
module Types
  include Dry::Types.module
end

class Address < Dry::Struct
  attribute :suburb, Types::Strict::String
end

class Customer < Dry::Struct
  attribute :name, Types::Strict::String
  attribute :address, Address
end

Customer.new(name: "George", address: {suburb: "Test Town”})
# => #<Customer name="George" address=#<Address suburb="Test Town">>
Tim Riley
@timriley
Jan 09 2017 22:27
@lastk I think you might be asking in the wrong room. I think you’re after trailblazer/chat
Rafael
@lastk
Jan 09 2017 22:29
ops
yes, you're right, sorry :P