Skip to content
DSL to build Pipelines with mountable Segments that process stuff
Branch: master
Clone or download
Latest commit 965684f Apr 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Add circleci config Mar 10, 2019
bin Setup guard for rspec Mar 10, 2019
lib Version bump Apr 19, 2019
spec
.gitignore Simplistic pipeline version (#1) Apr 19, 2019
.rspec
.ruby-version Add .ruby-version Feb 22, 2019
.travis.yml bundle gem nxt_pipeline Feb 22, 2019
CHANGELOG.md
Gemfile Add rspec_junit_formatter dependency for circleci Mar 10, 2019
Gemfile.lock
Guardfile Setup guard for rspec Mar 10, 2019
LICENSE.txt bundle gem nxt_pipeline Feb 22, 2019
README.md Simplistic pipeline version (#1) Apr 19, 2019
Rakefile
nxt_pipeline.gemspec Simplistic pipeline version (#1) Apr 19, 2019

README.md

CircleCI

NxtPipeline

nxt_pipeline provides a DSL to define pipeline classes which take an object and pass it through multiple steps which can read or modify the object.

Installation

Add this line to your application's Gemfile:

gem 'nxt_pipeline'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nxt_pipeline

Usage

Constructors

First you probably want to configure a pipeline so that it can execute your steps. Therefore you want to define constructors for your steps. Constructors take a name as the first argument and step options as the second. All step options are being exposed by the step yielded to the constructor.

pipeline = NxtPipeline::Pipeline.new do |p|
  # Add a named constructor that will be used to execute your steps later
  # All options that you pass in your step will be available through accessors in your constructor 
  p.constructor(:service, default: true) do |step, arg|
    step.service_class.new(options: arg).call
  end
  
  p.constructor(:job) do |step, arg|
    step.job_class.perform_later(*arg) && arg
  end
end

# Once a pipeline was created you can still configure it 
pipeline.constructor(:call) do |step, arg|
  step.caller.new(arg).call
end

# same with block syntax 
# You can use this to split up execution from configuration  
pipeline.configure do |p|
 p.constructor(:call) do |step, arg|
   step.caller.new(arg).call
 end
end

Defining steps

Once your pipeline knows how to execute your steps you can add those.

pipeline.step :service, service_class: MyServiceClass, to_s: 'First step'
pipeline.step service_class: MyOtherServiceClass, to_s: 'Second step' 
# ^ Since service is the default step you don't have to specify it the step type each time
pipeline.step :job, job_class: MyJobClass # to_s is optional
pipeline.step :job, job_class: MyOtherJobClass

pipeline.step :step_name_for_better_log do |_, arg|
  # ...
end

pipeline.step to_s: 'This is the same as above' do |step, arg|
  # ... step.to_s => 'This is the same as above'
end

You can also define inline steps, meaning the block will be executed

Execution

You can then execute the steps with:

pipeline.execute('initial argument')

# Or run the steps directly using block syntax

pipeline.execute do |p|
  p.step :service, service_class: MyServiceClass, to_s: 'First step'
  p.step :service, service_class: MyOtherServiceClass, to_s: 'Second step'
  p.step :job, job_class: MyJobClass # to_s is optional
  p.step :job, job_class: MyOtherJobClass
end

You can also directly execute a pipeline with:

NxtPipeline::Pipeline.execute('initial argument') do |p|
  p.step do |_, arg|
    arg.upcase
  end
end

Error callbacks

Apart from defining constructors and steps you can also define error callbacks.

NxtPipeline::Pipeline.new do |p|
  p.step do |_, arg|
    arg.upcase
  end
  
  p.on_error MyCustomError do |step, arg, error|
    # First matching error callback will be executed!
  end
  
  p.on_errors ArgumentError, KeyError do |step, arg, error|
    # First matching error callback will be executed!
  end
  
  p.on_errors do |step, arg, error|
    # This will match all errors inheriting from StandardError
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/rspec 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.

You can also run bin/guard to automatically run specs when files are saved.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nxt-insurance/nxt_pipeline.

License

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

You can’t perform that action at this time.