Operabl

Railway oriented aproach for your classes. Operabl let you define steps in your class or module to be executed sequentially. Steps can be class methods, procs or other Operabl classes/modules.

Installation

Add this line to your application's Gemfile:

gem 'operabl'

And then execute:

$ bundle

Or install it yourself as:

$ gem install operabl

Usage

Include Operable in your Class/Module. Steps can be class methods, procs or other Operabl class/module

class ProcOp

    include Operabl

    step ->(ctx) {
        ctx[:key] = ctx.params[:pkey]
        ctx[:amethod_call] = amethod
    }

    def self.amethod
        return "something else"
    end

end

op = ProcOp.call({pkey: "something"})
op.failure? # => false
op.success? # => true
op[:key] # => "something"
op[:amethod_call] # => "something else"

Steps are Class level methods. This is because steps only work with a context provided, they don't change classes state.

class MethodOp

    include Operabl

    step :a

    def self.a(ctx)
        ctx[:key] = ctx.params[:pkey]
    end
end

op = MethodOp.call({pkey: "something"})
op.success? # => true
op.result # => {}

Another example of an Operabl as a step.

class ComposedOp

    include Operabl

    step MethodOp
    step :b

    def self.b(ctx)
        ctx.success!({status: 200, response: "OK"})
    end

end

op = ComposedOp.call({pkey: "something else"})
op.success? # => true
op[:key] # => "something else"
op.result # => {status: 200, response: "OK"}

The result from #call is a Operabl::Context, it can be passed to a new Operabl call.

If Operation failed, AnotherOp is not going to succed but it will run any :failure step defined in it.


class AnotherOp

    include Operabl

    step :a
    failure :b

    def self.a(ctx)
        #do something
    end

    def self.b(ctx)
        #recover from failure
    end

end

op = Operation.call({pkey: "something else"})
op2 = AnotherOp.call(op)

License

The gem is available as open source under the terms of the MIT License.