Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Pavel Argentov
@argent-smith
Hi ppl. How to package a Klass::ActiveRecord_Associations_CollectionProxy into a dry-struct (via dry-types)?
Specifically, I want to declare a certain model's scope as a struct member
Nikita Shilnikov
@flash-gordon
@argent-smith you can use Types::Any in comlex cases
Pavel Argentov
@argent-smith
thnx
I've tried Types.Definition() as well: it works
Nikita Shilnikov
@flash-gordon
hm, yeah, though Definition doesn't do a type check actually, .Instance() does, or its alias, .Strict()
Jonah
@jonahx
@solnic @timriley friendly poke re: question above ^^
Also separate question for today: I have an Dry::Struct with an attribute with a Class type constraint. When using rspec, I want to pass in a double for the object. But an rspec double does not have type class. Have you ever run into this? What was your workaround?
Tim Riley
@timriley
@jonahx What you have in that code example above seems fine. There’s not any kind of first-class support for something like that.
Jonah
@jonahx
@timriley thanks
Tim Riley
@timriley
@jonahx Alternatively, you could just use the type definition directly whenever you need it?
Webservices = Types::Strict::Array(Types::Instance(Webservice))
services = Types::Webservices[some_array]
Jonah
@jonahx
@timriley yeah, in this case i felt there was benefit in explictily naming the concept. Also, the real object has a from_string ctor so there’s a bit more reason to have it
Tim Riley
@timriley
Right, that makes sense then.
Jonah
@jonahx
any advice on the rspec question?
Tim Riley
@timriley
Mind repeating it?
Jonah
@jonahx
Also separate question for today: I have an Dry::Struct with an attribute with a Class type constraint. When using rspec, I want to pass in a double for the object. But an rspec double does not have type class. Have you ever run into this? What was your workaround?
Tim Riley
@timriley
I’d just construct an object of the actual class :)
Jonah
@jonahx
ha, you’re not as lazy as me. thanks :)
Tim Riley
@timriley
Code should inform tests :)
(and if it makes testing too hard, well, that’s a code problem)
Jonah
@jonahx
@timriley, well, the test is perfectly easy. it’s just that rspec double’s aren’t designed with type checking in mind, which is fair enough given how atypcal it is in ruby. imo dry::struct types bring a certain sanity to ruby that should be there, but the fact remains it’s still an addon to the language, so to speak.
Tim Riley
@timriley
Yeah. I’d only use doubles where duck-typing matters more than the actual strict class check
Jonah
@jonahx
@timriley which is indeed the case here. i sort of added the class constraint just as a way of enforcing the interface, but now that you mention it i think that’s where my mistake was — the contract really is just an interface and not a particular class. and removing the type constraint which i’ve what i did to fix the issue was probably the correct move anyway and not just a hack as i’ve been viewing it
Tim Riley
@timriley
Cool :)
Alexander Chernik
@achernik
Hello!
I have a question regarding dry-validation usage. Is it possible to "filter out" invalid parts instead of flat out rejecting entire hash?
I could use result.errors to filter result.output manually, but maybe there is another way?
Jonah
@jonahx
How can I get an attribute that is both optional and default? I want to be able to leave it out, and when I do, it should have a default value.
Nikita Shilnikov
@flash-gordon
@jonahx I think it's what dry-types/dry-struct does in master, check out the changelogs
Tiago Moraes
@tiagoefmoraes

Hello,
when defining an enum without 0 as a valid value:

subject(:enum) { Dry::Types['integer'].enum(4, 5, 6) }

I expected that it would be invalid

expect(enum.try(0)).to be_failure

but it is actually valid because of inverted_mapping. I found this to be really confusing.
Should't this inverted behavior only happen when the mapping is defined explicitly?

subject(:enum) { Dry::Types['integer'].enum(4 => 0) }

At least when the values are all integers?

Nikita Shilnikov
@flash-gordon
it always worked like that for integers but I think this behavior can be made configurable
well, try didn't work in prev version actually but fixed this :)
[] worked
I would say it's better not to have mapping by default
but this is a breaking change
Nikita Shilnikov
@flash-gordon
@tiagoefmoraes I'll do what you suggested before the release, thanks for bringing it
Jonah
@jonahx

Any idea why I could be getting the error:

Attribute :authenticator_name has already been defined (Dry::Struct::RepeatedAttributeError)

when that is not the case?
My class looks like this:

    class Input < ::Dry::Struct
      attribute :authenticator_name, Types::NonEmptyString
      attribute :service_id,         Types::Coercible::String
      attribute :account,            Types::NonEmptyString
      attribute :username,           Types::NonEmptyString
      attribute :password,           Types::NonEmptyString
    end
Nikita Shilnikov
@flash-gordon
@jonahx most likely the file containing Input getting loaded twice
Jonah
@jonahx
@flash-gordon yep, rails was autoloading and there was a require statement somewhere. when i removed the require the error vanished.
Tiago Moraes
@tiagoefmoraes
@flash-gordon thanks!
Ryan Bigg
@radar
in ur repos, submitting PRs: dry-rb/dry-core#25
Jonah
@jonahx

How can I mix required and optional/default attributes? That is, the following works fine:

class Test < Dry::Struct
  attribute :optional, Types::Any.default('default value')
end
p Test.new.optional #=> prints 'default value'

However

class Test2 < Dry::Struct
  attribute :required, Types::Any
  attribute :optional, Types::Any.default('default value')
end
p Test2.new(required: 'my required val')
# Raises:
# [Test2.new] :optional is missing in Hash input (Dry::Struct::Error)
Jean-Denis Koeck
@jeandenis-k
Hi @jonahx ! Seems to me you would have to use, in the body of your class, either constructor_type :strict_with_defaults or constructor_type :schema
Jonah
@jonahx
@jeandenis-k thanks, i’ll try it
Jean-Denis Koeck
@jeandenis-k
:+1:
Jonah
@jonahx
Separate question. Consider:
class Test < Dry::Struct
  attribute :cls, Types::Any.default(String)
end

p Test.new.cls == String # return true
Yet in my actual code I’m doing the same thing with a Sequel class:
    attribute :resource_class, ::Types::Any.default(Resource)
but when I later access resource_class it returns not the class, but it seems an instance of the class:
#<Resource @values=#<#<Class:0x00000003e7bf00> primitive=Object options={} meta={}>>