Class: ConvenientService::Common::Plugins::HasAroundCallbacks::Middleware

Inherits:
MethodChainMiddleware
  • Object
show all
Includes:
Support::Delegate
Defined in:
lib/convenient_service/common/plugins/has_around_callbacks/middleware.rb

Instance Method Summary collapse

Methods included from Support::Concern

included

Instance Method Details

#any_methodObject



14
# File 'lib/convenient_service/common/plugins/has_around_callbacks/middleware.rb', line 14

intended_for any_method, entity: any_entity

#callback_classClass<ConvenientService::Common::Plugins::HasCallbacks::Entities::Callback>



19
# File 'lib/convenient_service/common/plugins/has_around_callbacks/middleware.rb', line 19

delegate :callback_class, to: :callbacks

#next(*args, **kwargs, &block) ⇒ Object

Returns Can be any type.

Parameters:

  • args (Array<Object>)
  • kwargs (Hash{Symbol => Object})
  • block (Proc, nil)

Returns:

  • (Object)

    Can be any type.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/convenient_service/common/plugins/has_around_callbacks/middleware.rb', line 30

def next(*args, **kwargs, &block)
  ##
  # A variable that stores return value of middleware `chain.next` aka `original_value`.
  # It is reassigned later by the `initial_around_callback`.
  #
  original_value = nil

  ##
  # A list of around callbacks.
  #
  # class Service
  #   around do |chain|
  #     # part before `chain.yield`
  #     original_value = chain.yield
  #     # part after `chain.yield`
  #   end
  # end
  #
  # class Service
  #   around do |chain, arguments|
  #     # part before `chain.yield`
  #     original_value = chain.yield
  #     # part after `chain.yield`
  #   end
  # end
  #
  around_callbacks = callbacks.for([:around, method])

  ##
  #
  #
  initial_around_callback = callback_class.new(
    types: [:around, method],
    block: proc { original_value = chain.next(*args, **kwargs, &block) }
  )

  ##
  # Let's suppose that we have 3 `around` callbacks:
  #
  # class Service
  #   # first
  #   around do |chain|
  #     # part before `chain.yield`
  #     original_value = chain.yield
  #     # part after `chain.yield`
  #   end
  #
  #   # second
  #   around do |chain|
  #     # part before `chain.yield`
  #     original_value = chain.yield
  #     # part after `chain.yield`
  #   end
  #
  #   # third
  #   around do |chain|
  #     # part before `chain.yield`
  #     original_value = chain.yield
  #     # part after `chain.yield`
  #   end
  # end
  #
  # NOTE: if a user forgets to call `chain.yield` - an error is raised.
  #
  # Then `composed` may be built with the following preudocode (that is why `reverse` is needed):
  #
  #   composed = original
  #   composed = proc { instance_exec(composed, &third) }
  #   composed = proc { instance_exec(composed, &second) }
  #   composed = proc { instance_exec(composed, &first) }
  #
  # Where `first`, `second`, `third` are taken from `entity.callbacks.for([:around, method])`.
  #
  # Original implementation is modified in order to return `original_value` from all `chain.yield` calls.
  #
  #   # Original implementation:
  #   composed =
  #     callbacks.for([:around, method]).reverse.reduce(original) do |composed, callback|
  #       proc { instance_exec(composed, &callback) }
  #     end
  #
  # Original implementation is modified for the second time in order to raise an error
  # if a user forgets to call `chain.yield` inside an around callback.
  #
  # rubocop:disable Style/Semicolon
  composed =
    around_callbacks.reverse.reduce(initial_around_callback) do |composed, callback|
      proc { callback.call_in_context_with_value_and_arguments(entity, composed, *args, **kwargs, &block); original_value }
    end
  # rubocop:enable Style/Semicolon

  ##
  # Call sequence:
  #
  #   composed.call
  #   proc { instance_exec(composed, &first) }.call  # part before `chain.yield`
  #   proc { instance_exec(composed, &second) }.call # part before `chain.yield`
  #   proc { instance_exec(composed, &third) }.call  # part before `chain.yield`
  #   initial_around_callback
  #   proc { instance_exec(composed, &third) }.call  # part after `chain.yield`
  #   proc { instance_exec(composed, &second) }.call # part after `chain.yield`
  #   proc { instance_exec(composed, &first) }.call  # part after `chain.yield`
  #
  composed.call

  ##
  #
  #
  ::ConvenientService.raise Exceptions::AroundCallbackChainIsNotContinued.new(callback: Utils::Array.find_last(around_callbacks, &:called?)) if around_callbacks.any?(&:not_called?)

  ##
  #
  #
  ::ConvenientService.raise Exceptions::AroundCallbackChainIsNotContinued.new(callback: around_callbacks.last) if initial_around_callback.not_called?

  original_value
end