Dry::Pipeline Join the chat at https://gitter.im/dryrb/chat

Gem Version Build Status Dependency Status Code Climate Documentation Status

Installation

Add this line to your application's Gemfile:

gem 'dry-pipeline'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dry-pipeline

Usage

USERS = []
User = Struct.new(:id, :first_name, :last_name, :email)

transform_user_attributes = Dry::Pipeline.new do |user_attributes|
  allowed_keys = [:id, :first_name, :last_name, :email]

  user_attributes.each_with_object({}) do |(key, value), hash|
    next unless allowed_keys.include?(key.to_sym)
    hash[key.to_sym] = value
  end
end

validate_user_attributes = Dry::Pipeline.new do |user_attributes|
  required_keys = [:first_name, :last_name, :email]

  if (required_keys - user_attributes.keys).empty?
    user_attributes
  else
    raise ':first_name, :last_name and :email must be present'
  end
end

create_user = Dry::Pipeline.new do |user_attributes|
  User.new(
    USERS.length.next, *user_attributes.values_at(:first_name, :last_name, :email)
  ).tap { |user| USERS << user }
end

(transform_user_attributes >> validate_user_attributes >> create_user)[
  first_name: 'Jane',
  last_name: 'Doe',
  email: '[email protected]'
]
# => #<struct User id=1, first_name="Jane", last_name="Doe", email="[email protected]">

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dryrb/dry-pipeline.