Releases: dry-rb/dry-struct
Releases · dry-rb/dry-struct
v1.6.0
v1.5.2
Fixed
- Coercion failures keep the original error instead of just having a string (@flash-gordon)
v1.5.1
v1.5.0
v1.4.0
Added
- Support for wrapping constructors and fallbacks, see release notes for dry-types 1.5.0 (@flash-gordon)
- Improvements of the attribute DSL, now it's possible to use optional structs as a base class (@flash-gordon)
class User < Dry::Struct attribute :name, Types::String attribute :address, Dry::Struct.optional do attribute :city, Types::String end end User.new(name: "John", address: nil) # => #<User name="John" address=nil>
v1.3.0
Added
-
Nested structures will reuse type and key transformations from the enclosing struct (@flash-gordon)
class User < Dry::Struct transform_keys(&:to_sym) attribute :name, Types::String attribute :address do # this struct will inherit transform_keys(&:to_sym) attribute :city, Types::String end # nested struct will _not_ transform keys because a parent # struct is given attribute :contacts, Dry::Struct do attribute :email, Types::String end end
-
Dry::Struct::Constructor
finally acts like a fully-featured type (@flash-gordon) -
Dry::Struct.abstract
declares a struct class as abstract. An abstract class is used as a default superclass for nested structs (@flash-gordon) -
Struct.to_ast
and struct compiler (@flash-gordon) -
Struct composition with
Dry::Struct.attributes_from
. It's more flexible than inheritance (@waiting-for-dev + @flash-gordon)class Address < Dry::Struct attribute :city, Types::String attribute :zipcode, Types::String end class Buyer < Dry::Struct attribute :name, Types::String attributes_from Address end class Seller < Dry::Struct attribute :name, Types::String attribute :email, Types::String attributes_from Address end
Changed
- [internal] metadata is now stored inside schema (@flash-gordon)
v1.2.0
1.2.0 2019-12-20
Changed
Dry::Struct::Value
is deprecated.Dry::Struct
instances were never meant to be mutable, we have no support for this. The only difference betweenDry::Struct
andDry::Struct::Value
is that the latter is deeply frozen. Freezing objects slows the code down and gives you very little benefit in return. If you have a use case forValue
, it won't be hard to roll your own solution using ice_nine (flash-gordon)- In the thread of the previous change, structs now use immutable equalizer. This means
Struct#hash
memoizes its value after the first invocation. Depending on the case, this may speed up your code significantly (flash-gordon)
v1.1.1
1.1.1 2019-10-13
Changed
-
Pattern matching syntax is simplified with
deconstruct_keys
(k-tsj)User = Dry.Struct(name: 'string', email: 'string') user = User.new(name: 'John Doe', email: 'john@acme.org') case user in User(name: 'John Doe', email:) puts email else puts 'Not John' end
See more examples in the specs.
v1.1.0
1.1.0 2019-10-07
Added
-
Experimental support for pattern matching 🎉 (flash-gordon)
User = Dry.Struct(name: 'string', email: 'string') user = User.new(name: 'John Doe', email: 'john@acme.org') case user in User({ name: 'John Doe', email: }) puts email else puts 'Not John' end
See more examples in the specs.
v1.0.0
1.0.0 2019-04-23
Changed
valid?
and===
behave differently,===
works the same wayClass#===
does andvalid?
checks if the value can be coerced to the struct (flash-gordon)
Added
Struct.call
now accepts an optional block that will be called on failed coercion. This behavior is consistent with dry-types 1.0. Note that.new
doesn't take a block (flash-gordon)User = Dry::Struct(name: 'string') User.(1) { :oh_no } # => :oh_no