0.02
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Pipeline operator for callable objects
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Dry::Pipeline Join the chat at https://gitter.im/dry-rb/chat

Gem Version

ARCHIVED

Ruby 2.6 introduced Proc composition with >> and <<, that's essentially rendered this project obsolete.

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: 'jane.doe@gmail.com'
]
# => #<struct User id=1, first_name="Jane", last_name="Doe", email="jane.doe@gmail.com">