Skip to content

A port of the chimney library in Scala to allow usage over dry-structs in Ruby

License

Notifications You must be signed in to change notification settings

adfineberg/chimney-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chimney-ruby

This gem allows easy transformation between two dry-struct classes. A port of the Scala chimney library (https://scalalandio.github.io/chimney) idea to allow usage over dry-structs in Ruby (https://dry-rb.org/gems/dry-struct/1.0/)

Installation

Add this line to your application's Gemfile:

gem 'chimney-ruby'

And then execute:

$ bundle

Or install it yourself as:

$ gem install chimney-ruby

Usage

This gem allows easy transformation between two dry-struct classes. Given two classes as such:

class FirstUser < Dry::Struct
  attribute :name, Types::String.optional
  attribute :age, Types::Coercible::Integer
end

class SecondUser < Dry::Struct
  attribute :name, Types::String.optional
  attribute :age, Types::Coercible::Integer
end  

You can create an immutable instance of the first class using transform_into:

first_user = FirstUser.new(name: 'john', age: '21')
second_user = first_user.transform_into(SecondUser)

For more advanced usages use into with transform, currently supported are:

Add missing values

Either statically:

class SecondUserWithFeeling < Dry::Struct
  attribute :name, Types::String.optional
  attribute :age, Types::Coercible::Integer
  attribute :feeling, Types::String
end

first_user = FirstUser.new(name: 'john', age: '21')
second_user = first_user.into(SecondUserWithFeeling)
                .with_field_const(:feeling, 'happy')
                .transform

Or dynamically using a proc:

dynamic_feeling = proc { |user| user.age > 2 ? 'sad' : 'happy' }
second_user = first_user.into(SecondUserWithFeeling)
                .with_field_computed(:feeling, dynamic_feeling)
                .transform

Provide default values

class SecondUserWithDefaultFeeling < Dry::Struct
  attribute :name, Types::String.optional
  attribute :age, Types::Coercible::Integer
  attribute :feeling, Types::String.default('happy'.freeze)
end

first_user = FirstUser.new(name: 'john', age: '21')
second_user = first_user.transform_into(SecondUserWithDefaultFeeling)

Re-labelling fields

class FirstUserRenamed < Dry::Struct
  attribute :shem, Types::String.optional
  attribute :gil, Types::Coercible::Integer
end

first_user = FirstUser.new(name: 'john', age: '21')
renamed_user = first_user.into(FirstUserRenamed)
                  .with_field_renamed(:name, :shem)
                  .with_field_renamed(:age, :gil)
                  .transform

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/adfineberg/chimney-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Chimney::Ruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

A port of the chimney library in Scala to allow usage over dry-structs in Ruby

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published