Sha256: 11ef7b5a692cecc40ebbb93e322f4e52aa615e7ec360d0e8d5002bed0fd66969

Contents?: true

Size: 897 Bytes

Versions: 2

Compression:

Stored size: 897 Bytes

Contents

require "ostruct"

module Rankum
  module Utils
    module Interactor

      def initialize context={}
        @context = OpenStruct.new(context)
      end

      def execute
        begin
          yield
          exit if @context.fail?
          success!
         rescue Exception => e
           fail!
        end

        @context
      end

      def organize *interactors
        interactors.each do |interactor|
          context_hash = @context.to_h
          context = interactor.run(context_hash)

          @context = OpenStruct.new(context_hash.merge!(context.to_h))
        end

        @context
      end

      def context
        @context
      end

      def fail!
        @context.send("success?=",false)
        @context.send("fail?=",true)
      end

      def success!
        @context.send("success?=",true)
        @context.send("fail?=",false)
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rankum-1.1.0 lib/rankum/utils/interactor.rb
rankum-1.0.0 lib/rankum/utils/interactor.rb