Project

opto-task

0.0
No commit activity in last 3 years
No release in over 3 years
Create tasks or service objects from Opto::Models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 10.0
~> 3.0

Runtime

~> 0.2.1
 Project Readme

Opto/Task

Build Status

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.