dry-struct v0.5.0 Release Notes
Release Date: 2018-05-03 // almost 6 years ago-
๐ฅ BREAKING CHANGES
- ๐
constructor_type
was removed, usetransform_types
andtransform_keys
as a replacement (see below) - Default types are evaluated only on missing values. Again, use
tranform_types
as a work around fornil
s - ๐ Values are now stored within a single instance variable names
@attributes
, this sped up struct creation and improved support for reserved attribute names such ashash
, they don't get a getter but still can be read via#[]
- ๐ Ruby 2.3 is a minimal supported version
โ Added
- โช
Dry::Struct.transform_types
accepts a block which is yielded on every type to add. Since types aredry-types
' objects that come with a robust DSL it's rather simple to restore the behavior ofconstructor_type
. See https://github.com/dry-rb/dry-struct/pull/64 for details (flash-gordon)
Example: evaluate defaults on
nil
valuesclass User < Dry::Struct transform_types do |type| type.constructor { |value| value.nil? ? Undefined : value } end end
Data::Struct.transform_keys
accepts a block/proc that transforms keys of input hashes. The most obvious usage is simbolization but arbitrary transformations are allowed (flash-gordon)๐
Dry.Struct
builds a struct by a hash of attribute names and types (citizen428)
User = Dry::Struct(name: 'strict.string') do attribute :email, 'strict.string' end
- ๐ Support for
Struct.meta
, note that.meta
returns a new class (flash-gordon)
class User < Dry::Struct attribute :name, Dry::Types['strict.string'] end UserWithMeta = User.meta(foo: :bar) User.new(name: 'Jade').class == UserWithMeta.new(name: 'Jade').class # => false
- ๐
Struct.attribute
yields a block with definition for nested structs. It defines a nested constant for the new struct and supports arrays (AMHOL + flash-gordon)
class User < Dry::Struct attribute :name, Types::Strict::String attribute :address do attribute :country, Types::Strict::String attribute :city, Types::Strict::String end attribute :accounts, Types::Strict::Array do attribute :currency, Types::Strict::String attribute :balance, Types::Strict::Decimal end end # ^This automatically defines User::Address and User::Account
๐ Fixed
- โ Adding a new attribute invalidates
attribute_names
(flash-gordon) - Struct classes track subclasses and define attributes in them, now it doesn't matter whether you define attributes first and then subclass or vice versa. Note this can lead to memory leaks in Rails environment when struct classes are reloaded (flash-gordon)
- ๐