Skip to content

v0.5.0

Choose a tag to compare

@flash-gordon flash-gordon released this 03 May 15:18
· 638 commits to main since this release
v0.5.0
cf953e1

v0.5.0 2018-05-03

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 #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