Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Andy Holland
@AMHOL

I think calling

module Types
  include Dry::Types.module
end

in an initializer

Then defining your custom types somewhere in the app/lib directory should stop the problem from happening
Simon Schmid
@sled
I'll try right away :)
I also noticed there are no setters, do you have any best practices on how to modify certain attributes easily?
the one I came up with is Hypo::Types::Location.new(original.to_h.merge(phone_number: '123456'))
Andy Holland
@AMHOL
Yeah, mutable structs won't be added to dry-types core
Piotr Solnica
@solnic
we deliberately fight with mutability
(so dramatic)
Simon Schmid
@sled
@solnic, I totally support that - however it's still nice to have an easy way of creating a copy with the modifications
might be a cool case for keyword arguments
Simon Schmid
@sled
@AMHOL just tried with the initializer and putting custom types into app/lib, however if I auto-load these paths it fails with the same error
Andy Holland
@AMHOL
Ahh shit, yeah
Forgot types get registered automatically when you inherit from Struct
Simon Schmid
@sled
yep I think I need to "reset" the container's registry somehow
Piotr Solnica
@solnic
Screen Shot 2016-04-12 at 16.10.02.png
related ^^
that’s from Programming Clojure book but it’s a universal truth
Dung Nguyen
@mmeeoorroo
I have Product and Category, both are Types::Struct
Product has a field with Category type. How can we make that field nullable so that I don't have to join/query categories table?
Simon Schmid
@sled
attribute :category, Category.optional :D
just learned today
Dung Nguyen
@mmeeoorroo
thanks but I get error 'NoMethodError: undefined method `optional' for Category:Class' :D
Simon Schmid
@sled
is your category a type?
Dung Nguyen
@mmeeoorroo
class Category < Dry::Types::Struct; end
class Product < Dry::Types::Struct
attribute :category, Category.optional
end
is that correct?
Simon Schmid
@sled
mh... another guess: attribute :category, Types::Strict::Nil | Category
(I'm still learning too)
Dung Nguyen
@mmeeoorroo
still not work, I will learn more :D
thanks @sled
Simon Schmid
@sled
@mmeeoorroo actually the one with | works for me
ah nope
Dung Nguyen
@mmeeoorroo
the | works with this trick: Product.new({id: 1}.merge(category: nil))
Piotr Solnica
@solnic
@sled I use separate struct classes, one with a category and one w/o a category
Dung Nguyen
@mmeeoorroo
magic :D
Piotr Solnica
@solnic
a product with category would be a separate concept in my app
vs a product w/o a category
Simon Schmid
@sled

`a_type = Hypo::Types::Strict::Nil | Hyp::Types::Address

a_type[nil] => nil

a_type[Hypo::Types::Address.new] => TypeError: #<Hypo::Types::Address address_line1=nil, ...> is not a symbol nor a string`

Piotr Solnica
@solnic
if you want dynamic behavior just don’t use explicit classes and rely on auto-generated rom structs
this won’t work, struct classes are not types
Hyp::Types[“address”].optional should work but I wouldn’t recommend it
Dung Nguyen
@mmeeoorroo
what's wrong with it?
Piotr Solnica
@solnic
because nil is evil, mostly. your system will start relying on a product-with-or-without-a-category
Simon Schmid
@sled
@solnic but how to handle these permutations? If I have a struct with 3-4 optionals, I'd have to create 4! different variations
also a common scenario is to save "incomplete" forms (e.g auto-saving)
Piotr Solnica
@solnic
I don’t use structs along with forms
I use structs to represent specific concepts in a system, and treat them as values
Simon Schmid
@sled
do you have an example what is meant by a "concept" ? :)
Piotr Solnica
@solnic
a product, a user, a product with a category, a list of products etc