v0.5.0
v0.5.0 2018-05-03
BREAKING CHANGES
constructor_typewas removed, usetransform_typesandtransform_keysas a replacement (see below)- Default types are evaluated only on missing values. Again, use
tranform_typesas a work around fornils - 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_typesaccepts 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 #64 for details (flash-gordon)Example: evaluate defaults on
nilvaluesclass User < Dry::Struct transform_types do |type| type.constructor { |value| value.nil? ? Undefined : value } end end
-
Data::Struct.transform_keysaccepts a block/proc that transforms keys of input hashes. The most obvious usage is simbolization but arbitrary transformations are allowed (flash-gordon) -
Dry.Structbuilds 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.metareturns 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.attributeyields 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)