Class: Outbacker::OutcomeHandlerSet

Inherits:
Struct
  • Object
show all
Defined in:
lib/outbacker.rb

Overview

Class to encapsulate the processing of a block of outcome handlers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &outcome_block) ⇒ Object

Provides an alternate way to specify a callback block using method names.



80
81
82
83
84
85
# File 'lib/outbacker.rb', line 80

def method_missing(method_name, *args, &outcome_block)
  super unless /^outcome_of_(?<suffix>.*)/ =~ method_name.to_s
  outcome_key = suffix.to_sym

  execute_outcome_block(outcome_key, &outcome_block)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



38
39
40
# File 'lib/outbacker.rb', line 38

def args
  @args
end

#handled_outcomeObject

Returns the value of attribute handled_outcome

Returns:

  • (Object)

    the current value of handled_outcome



38
39
40
# File 'lib/outbacker.rb', line 38

def handled_outcome
  @handled_outcome
end

#outcome_handlersObject

Returns the value of attribute outcome_handlers

Returns:

  • (Object)

    the current value of outcome_handlers



38
39
40
# File 'lib/outbacker.rb', line 38

def outcome_handlers
  @outcome_handlers
end

#triggered_outcomeObject

Returns the value of attribute triggered_outcome

Returns:

  • (Object)

    the current value of triggered_outcome



38
39
40
# File 'lib/outbacker.rb', line 38

def triggered_outcome
  @triggered_outcome
end

Instance Method Details

#handle(outcome_key, *args) ⇒ Object

Process the outcome specified by the given outcome_key, using the outcome handlers set on this OutcomeHandlerSet instance. Any additiona arbitrary arguments can be passed through to the corresponding outcome handler callback.



49
50
51
52
53
54
55
56
57
58
# File 'lib/outbacker.rb', line 49

def handle(outcome_key, *args)
  self.triggered_outcome = outcome_key
  self.args = args

  if outcome_handlers
    outcome_handlers.call(self)
    raise "No outcome handler for outcome #{outcome_key}" unless outcome_handled?
  end
  true
end

#of(outcome_key, &outcome_block) ⇒ Object

Specify an outcome handler callback block for the specified outcome key.



72
73
74
# File 'lib/outbacker.rb', line 72

def of(outcome_key, &outcome_block)
  execute_outcome_block(outcome_key, &outcome_block)
end

#outcome_handled?Boolean

Internal method to indicate that the outcome has been handled by some han dler.

Returns:

  • (Boolean)


64
65
66
# File 'lib/outbacker.rb', line 64

def outcome_handled?
  !!self.handled_outcome
end