Class: Startback::Operation

Inherits:
Object
  • Object
show all
Extended by:
Support::TransactionPolicy
Includes:
Errors, Support::OperationRunner
Defined in:
lib/startback/operation.rb,
lib/startback/event/ext/operation.rb,
lib/startback/operation/error_operation.rb,
lib/startback/operation/multi_operation.rb

Overview

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

Direct Known Subclasses

ErrorOperation, MultiOperation

Defined Under Namespace

Classes: ErrorOperation, MultiOperation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::TransactionPolicy

after_commit, transaction_policy, transaction_policy=

Methods included from Support::OperationRunner

included, #run

Methods included from Errors

bad_request_error!, conflict_error!, expectation_failed_error!, forbidden_error!, gone_error!, internal_server_error!, locked_error!, method_not_allowed_error!, not_acceptable_error!, not_found_error!, not_implemented_error!, precondition_failed_error!, precondition_required_error!, server_error!, unauthorized_error!, unsupported_media_type_error!, user_error!

Constructor Details

#initialize(input = {}) ⇒ Operation

Returns a new instance of Operation.



40
41
42
# File 'lib/startback/operation.rb', line 40

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &bl) ⇒ Object



51
52
53
54
55
# File 'lib/startback/operation.rb', line 51

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

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



43
44
45
# File 'lib/startback/operation.rb', line 43

def input
  @input
end

#worldObject

Returns the value of attribute world.



37
38
39
# File 'lib/startback/operation.rb', line 37

def world
  @world
end

Class Method Details

.emits(type, &bl) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/startback/event/ext/operation.rb', line 4

def self.emits(type, &bl)
  after_call do
    if event_data = instance_exec(&bl)
      event = type.new(type.to_s, event_data, context)
      context.engine.bus.emit(event)
    end
  end
end

Instance Method Details

#bind(world) ⇒ Object



45
46
47
48
49
# File 'lib/startback/operation.rb', line 45

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

#respond_to?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/startback/operation.rb', line 57

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

#with_context(ctx = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/startback/operation.rb', line 61

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