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