Course
Inspired and forked from opie
Course gives you a simple API for creating Operations using the
Railsway oriented programming paradigm.
Usage
Simple Usage:
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
result = Todos::CompleteTodo.(params[:id])
if result.success?
render status: :created, json: result.output
else
render status: :bad_request, json: { error: error_message(result.failure) }
end
end
private
def error_message(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) -> void
indicates a method that is executed in the operation sequence#success? -> Boolean
indicates whether the operation was successful#failure? -> Boolean
indicates whether the operation was a failure#failure -> Hash | nil
the erorr if the operation is a failure?
, nil when it's a success#failures -> Array<Hash> | nil
an 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>) -> void
a series of methods to be called in parallel::step(Course::Step) -> void
an enforcer of a step signature which helps to compose other steps::failure(Symbol) -> void
indicates 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.