Skip to content

A handy way (I hope) to convert a service object like things to a series of steps

License

Notifications You must be signed in to change notification settings

Rukomoynikov/processable

Repository files navigation

Processable

Ruby gem to help you organize any process into series of steps.

class PrintGithubRepos < Processable
  step :get_repos do
    github_client.where('rails', per_page: 100)
  end

  step :get_only_essential_fields do |repos_json|
    repos_json.except(:title, :description)
  end
  
  step :print_to_console do |result_from_prev_step, current_step, list_of_steps|
    print result_from_prev_step
  end
end

PrintGithubRepos.new.process

Installation

Add this line to your application's Gemfile:

gem 'processable'

And then execute:

bundle install

Or install it yourself as:

gem install processable

Usage

Define new class with required behavior

class SiteScrapper < Processable
end

Add steps to the process

Each step accepts three optional paramaters: [result from_previous step, current_step, all_steps]. Each step has some result as we usually do it you can either call return explicitly or last line will be accepted as result of the step.

require 'net/http'

class SiteScrapper < Processable
  step :get_web_pages do
    Net::HTTP.get('example.com', '/index.html')    
  end
  
  step :save_to_database do |web_page|
    DB.save(web_page)
  end
end

Trigger the process

To start the process simply call...

SiteScrapper.new.process

To run the process until some certain step you can pass the name of the step as an argument. Also keep in mind the step defined in run_until will not be executed as well.

SiteScrapper.new.process(run_until: :save_to_database)

To exec only one particular step you can pass the name of the step as an argument.

SiteScrapper.new.process(exec_step: :save_to_database)

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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Rukomoynikov/processable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the 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 Processable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

A handy way (I hope) to convert a service object like things to a series of steps

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks