Class: WaterDrop::Middleware
- Inherits:
-
Object
- Object
- WaterDrop::Middleware
- Defined in:
- lib/waterdrop/middleware.rb
Overview
Simple middleware layer for manipulating messages prior to their validation
Instance Method Summary collapse
-
#append(step) ⇒ Object
Register given middleware as the last one in the chain.
-
#initialize ⇒ Middleware
constructor
A new instance of Middleware.
-
#prepend(step) ⇒ Object
Register given middleware as the first one in the chain.
-
#run(message) ⇒ Hash
Runs middleware on a single message prior to validation.
-
#run_many(messages) ⇒ Array<Hash>
Transformed messages.
Constructor Details
#initialize ⇒ Middleware
Returns a new instance of Middleware.
6 7 8 9 |
# File 'lib/waterdrop/middleware.rb', line 6 def initialize @mutex = Mutex.new @steps = [] end |
Instance Method Details
#append(step) ⇒ Object
Register given middleware as the last one in the chain
44 45 46 47 48 |
# File 'lib/waterdrop/middleware.rb', line 44 def append(step) @mutex.synchronize do @steps.append step end end |
#prepend(step) ⇒ Object
Register given middleware as the first one in the chain
36 37 38 39 40 |
# File 'lib/waterdrop/middleware.rb', line 36 def prepend(step) @mutex.synchronize do @steps.prepend step end end |
#run(message) ⇒ Hash
Note:
You need to decide yourself whether you don’t use the message hash data anywhere else and you want to save on memory by modifying it in place or do you want to do a deep copy
Runs middleware on a single message prior to validation
18 19 20 21 22 23 24 |
# File 'lib/waterdrop/middleware.rb', line 18 def run() @steps.each do |step| = step.call() end end |
#run_many(messages) ⇒ Array<Hash>
Returns transformed messages.
28 29 30 31 32 |
# File 'lib/waterdrop/middleware.rb', line 28 def run_many() .map do || run() end end |