All Versions
18
Latest Version
Avg Release Cycle
97 days
Latest Release
1338 days ago
Changelog History
Page 1
Changelog History
Page 1
-
v1.4.0 Changes
January 21, 2021โ 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) ```ruby 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) # => #
[Compare v1.3.0...v1.4.0](https://github.com/dry-rb/dry-struct/compare/v1.3.0...v1.4.0)
-
v1.3.0 Changes
February 10, 2020โ 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)- 0๏ธโฃ
Dry::Struct.abstract
declares a struct class as abstract. An abstract class is used as a default superclass for nested structs (@flash-gordon) Dry::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 Changes
December 20, 20191.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 Changes
October 13, 2019๐ 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: '[email protected]') case user in User(name: 'John Doe', email:) puts email else puts 'Not John' end
See more examples in the specs.
- Pattern matching syntax is simplified with
-
v1.1.0 Changes
October 07, 20191.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: '[email protected]')case userin User({ name: 'John Doe', email: }) puts emailelseputs 'Not John'end
See more examples in the specs.
-
v1.0.0 Changes
April 23, 2019โ 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)ruby User = Dry::Struct(name: 'string') User.(1) { :oh_no } # => :oh_no
๐ Changed
valid?
and===
behave differently,===
works the same wayClass#===
does andvalid?
checks if the value can be coerced to the struct (flash-gordon)
-
v0.7.0 Changes
March 22, 2019๐ Changed
- [BREAKING]
Struct.input
was renamedStruct.schema
, henceStruct.schema
returns an instance ofDry::Types::Hash::Schema
rather than aHash
. Schemas are also implementingEnumerable
but they iterate over key types. New API:ruby User.schema.each do |key| puts "Key name: #{ key.name }" puts "Key type: #{ key.type }" end
To get a type by its name use.key
:ruby User.schema.key(:id) # => #<Dry::Types::Hash::Key ...>
- [BREAKING]
transform_types
now passes one argument to the block, an instance of theKey
type. Combined with the new API from dry-types it simplifies declaring omittable keys:ruby class StructWithOptionalKeys < Dry::Struct transform_types { |key| key.required(false) } # or simply transform_types(&:omittable) end
- โก๏ธ
Dry::Stuct#new
is now more efficient for partial updates (flash-gordon) - โ Ruby 2.3 is EOL and not officially supported. It may work but we don't test it.
- [BREAKING]
-
v0.6.0 Changes
March 22, 2019โ Added
Struct.attribute?
is an easy way to define omittable attributes (flash-gordon):
class User < Dry::Struct attribute :name, Types::Strict::String attribute? :email, Types::Strict::String end # User.new(name: 'John') # => #<User name="John">
๐ Fixed
Struct#to_h
recursively converts hash values to hashes, this was done to be consistent with current behavior for arrays (oeoeaio + ZimbiX)
๐ Changed
- ๐ [BREAKING]
Struct.attribute?
in the old sense is deprecated, usehas_attribute?
as a replacement
-
v0.5.1 Changes
August 11, 2018โ Added
- ๐จ Pretty print extension (ojab)
ruby Dry::Struct.load_extensions(:pretty_print) PP.pp(user) #<Test::User name="Jane", age=21, address=#<Test::Address city="NYC", zipcode="123">>
๐ Fixed
- Constant resolution is now restricted to the current module when structs are automatically defined using the block syntax. This shouldn't break any existing code (piktur)
- ๐จ Pretty print extension (ojab)
-
v0.5.0 Changes
May 03, 2018๐ฅ 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)
- ๐