dry-struct v0.5.0 Release Notes

Release Date: 2018-05-03 // almost 6 years ago
  • ๐Ÿ’ฅ BREAKING CHANGES

    • ๐Ÿšš constructor_type was removed, use transform_types and transform_keys as a replacement (see below)
    • Default types are evaluated only on missing values. Again, use tranform_types as a work around for nils
    • ๐Ÿ‘ Values are now stored within a single instance variable names @attributes, this sped up struct creation and improved support for reserved attribute names such as hash, 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 are dry-types' objects that come with a robust DSL it's rather simple to restore the behavior of constructor_type. See https://github.com/dry-rb/dry-struct/pull/64 for details (flash-gordon)

    Example: evaluate defaults on nil values

      class 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)

    Compare v0.4.0...v0.5.0