Opto/Task
Uses Opto based Opto/Model to create Mutations kind of runnable tasks or service objects.
Installation
Add this line to your application's Gemfile:
gem 'opto-task'
And then execute:
$ bundle
Or install it yourself as:
$ gem install opto-task
Usage
You can define tasks:
require 'opto-task'
class RemoveFileTask
include Opto.task
attribute :path, :string
def validate
add_error :path, :not_found, "The file isn't there" unless File.exist?(Path)
end
def perform
begin
File.unlink(path)
"great success"
rescue Errno::EACCES
add_error :path, :no_access, "You don't have the rights"
end
end
def after
puts "Your file will be missed"
end
end
And then run them:
task = RemoveFileTask.new(path: '/tmp/foo.txt')
result = task.run
result.success?
=> true
result.outcome
=> "great success"
..unless they're not valid:
task = RemoveFileTask.new(path: '/tmp/foobar.txt')
task.valid?
=> false
result = task.run
result.success?
=> false
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/kontena/opto-task.