Course
Inspired and forked from opie
Course gives you a simple API for creating Operations using the Railsway oriented programming paradigm.
Usage
Simple Usage:
# Create an Operation for completing a Todo
class Todos::CompleteTodo < Course::Operation
step :find_todo
step :mark_as_complete
def find_todo(todo_id)
todo = Todo.find_by(id: todo_id)
fail(:not_found, "Could not find the Todo using id: #{todo_id}") unless todo
todo
end
def mark_as_complete(todo)
success = todo.update(completed_at: Time.zone.now)
fail(:update) unless success
todo
end
end
class TodosController < ApplicationController
def complete
# invoke the operation
result = Todos::CompleteTodo.(params[:id])
if result.success? # if #success?
render status: :created, json: result.output # use output
else
render status: :bad_request, json: { error: (result.failure) } # otherwise use #failure
end
end
private
def (failure)
case failure[:type]
when :not_found then failure[:data]
when :update then 'We were unable to make the changes to your todo'
else 'There was an unexpected error, sorry for the inconvenience'
end
end
end
API
The Course::Operation API:
::step(Symbol) -> voidindicates a method that is executed in the operation sequence#success? -> Booleanindicates whether the operation was successful#failure? -> Booleanindicates whether the operation was a failure#failure -> Hash | nilthe erorr if the operation is afailure?, nil when it's a success#failures -> Array<Hash> | nilan array with all errors#output -> *if succcessful, it returns the operation final output validation error
Internal API:
#fail(error_type: Symbol, error_data: *) -> Hash
Tentative API
::step(Array<Symbol>) -> voida series of methods to be called in parallel::step(Course::Step) -> voidan enforcer of a step signature which helps to compose other steps::failure(Symbol) -> voidindicates the method that handles failures
Installation
Add this line to your application's Gemfile:
gem 'course'
And then execute:
$ bundle
Or install it yourself as:
$ gem install course
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test 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/zhulik/course
License
The gem is available as open source under the terms of the MIT License.