Sha256: df1510f9292bd833b56b3524a90677ed8053f3b9abe2d7fd470310eb9d101309

Contents?: true

Size: 1.94 KB

Versions: 31

Compression:

Stored size: 1.94 KB

Contents

module Startback
  #
  # High-level Operation abstraction, that is a piece of code that executes
  # on demand and (generally) changes the state of the software system.
  #
  # An operation is basically an object that respond to `call`, but that
  # executes within a given world (see `bind`). It also has before and
  # after hooks that allows specifying what needs to be done before invoking
  # call and after having invoked it. All this protocol is actually under
  # the responsibility of an `OperationRunner`. Operations should not be
  # called manually by third-party code.
  #
  # Example:
  #
  #     class SayHello < Startback::Operation
  #
  #       before_call do
  #         # e.g. check_some_permissions
  #       end
  #
  #       def call
  #         puts "Hello"
  #       end
  #
  #       after_call do
  #         # e.g. log and/or emit something on a bus
  #       end
  #
  #     end
  #
  class Operation
    extend Support::TransactionPolicy
    include Errors
    include Support::OperationRunner
    include Support::Hooks.new(:call)

    attr_accessor :world
    protected :world=

    def initialize(input = {})
      @input = input
    end
    attr_reader :input

    def bind(world)
      return self unless world
      self.world = world
      self
    end

    def method_missing(name, *args, &bl)
      return super unless args.empty? and bl.nil?
      return super unless world
      world.fetch(name){ super }
    end

    def respond_to?(name, *args)
      super || (world && world.has_key?(name))
    end

    def with_context(ctx = nil)
      old_world = self.world
      self.world = self.world.merge(context: ctx || old_world.context.dup)
      result = ctx ? yield : yield(self.world.context)
      self.world = old_world
      result
    end

  protected

    def operation_world(op)
      self.world
    end

  end # class Operation
end # module Startback
require_relative 'operation/error_operation'
require_relative 'operation/multi_operation'

Version data entries

31 entries across 31 versions & 3 rubygems

Version Path
startback-1.0.3 lib/startback/operation.rb
startback-1.0.2 lib/startback/operation.rb
startback-1.0.1 lib/startback/operation.rb
startback-1.0.0 lib/startback/operation.rb
startback-0.19.4 lib/startback/operation.rb
startback-0.19.3 lib/startback/operation.rb
startback-0.19.1 lib/startback/operation.rb
startback-0.19.0 lib/startback/operation.rb
startback-0.18.2 lib/startback/operation.rb
startback-0.18.1 lib/startback/operation.rb
startback-0.18.0 lib/startback/operation.rb
startback-0.17.4 lib/startback/operation.rb
startback-0.17.3 lib/startback/operation.rb
startback-0.17.2 lib/startback/operation.rb
startback-0.17.1 lib/startback/operation.rb
startback-0.17.0 lib/startback/operation.rb
startback-0.16.0 lib/startback/operation.rb
startback-0.15.5 lib/startback/operation.rb
startback-0.15.4 lib/startback/operation.rb
startback-0.15.3 lib/startback/operation.rb