Class: Ustack
- Inherits:
-
Object
- Object
- Ustack
- Defined in:
- lib/ustack.rb,
lib/ustack/version.rb
Overview
Micro middleware stack for general purpose.
Constant Summary collapse
- VERSION =
'0.0.2'
Instance Method Summary collapse
-
#initialize(&block) ⇒ Ustack
constructor
Initializes new middleware stack.
-
#insert_after(klass, new_klass, options = nil) ⇒ Object
Insert a middleware after another middleware.
-
#insert_before(klass, new_klass, options = nil) ⇒ Object
Insert a middleware before another middleware.
-
#run(env = {}) ⇒ Object
Runs the middlware stack.
-
#swap(old_klass, new_klass, options = nil) ⇒ Object
Swaps two middlewares.
-
#use(klass, options = nil) ⇒ Object
Tells the stack to use given middleware.
Constructor Details
#initialize(&block) ⇒ Ustack
Initializes new middleware stack.
47 48 49 |
# File 'lib/ustack.rb', line 47 def initialize(&block) instance_eval(&block) if block_given? end |
Instance Method Details
#insert_after(klass, new_klass, options = nil) ⇒ Object
Insert a middleware after another middleware.
117 118 119 |
# File 'lib/ustack.rb', line 117 def insert_after(klass, new_klass, =nil) @ustack.insert(index(klass) + 1, [new_klass, ]) end |
#insert_before(klass, new_klass, options = nil) ⇒ Object
Insert a middleware before another middleware.
100 101 102 |
# File 'lib/ustack.rb', line 100 def insert_before(klass, new_klass, =nil) @ustack.insert(index(klass), [new_klass, ]) end |
#run(env = {}) ⇒ Object
Runs the middlware stack.
150 151 152 153 154 155 156 |
# File 'lib/ustack.rb', line 150 def run(env={}) app = nil @ustack.reverse.each do |(klass, )| app = ? klass.new(app, ) : klass.new(app) end app.call(env) end |
#swap(old_klass, new_klass, options = nil) ⇒ Object
Swaps two middlewares.
83 84 85 |
# File 'lib/ustack.rb', line 83 def swap(old_klass, new_klass, =nil) @ustack[index(old_klass)] = [new_klass, ] end |
#use(klass, options = nil) ⇒ Object
Tells the stack to use given middleware.
64 65 66 67 |
# File 'lib/ustack.rb', line 64 def use(klass, =nil) @ustack ||= [] @ustack << [klass, ] end |