Class: Ustack

Inherits:
Object
  • Object
show all
Defined in:
lib/ustack.rb,
lib/ustack/version.rb

Overview

Micro middleware stack for general purpose.

Examples:

class M1 < Struct.new(:app)
  def call(env)
    puts self
    puts app.call(env)
    puts self
    'zzz'
  end
end

class M2 < Struct.new(:app, :options)
  def call(env)
    puts self
    options[:return]
  end
end

app = Ustack.new do
  use M1
  use M2, return: 'xxx'
end

puts app.run
# => M1
# => M2
# => xxx
# => M1
# => zzz

Constant Summary collapse

VERSION =
'0.0.2'

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Ustack

Initializes new middleware stack.

Examples:

app1 = Ustack.new
app2 = Ustack.new do
  use M1
  use M2, my_option: 'xxx'
end


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.

Examples:

app = Ustack.new do
  use M1
end
app.insert_before M1, M2, my_option: 'xxx'

Parameters:

  • klass (Class)

    Existing middleware.

  • new_klass (Class)

    Middleware to be inserted.

  • options (Hash) (defaults to: nil)

    Hash of orbitrary options for new middlware.



117
118
119
# File 'lib/ustack.rb', line 117

def insert_after(klass, new_klass, options=nil)
  @ustack.insert(index(klass) + 1, [new_klass, options])
end

#insert_before(klass, new_klass, options = nil) ⇒ Object

Insert a middleware before another middleware.

Examples:

app = Ustack.new do
  use M1
end
app.insert_before M1, M0, my_option: 'xxx'

Parameters:

  • klass (Class)

    Existing middleware.

  • new_klass (Class)

    Middleware to be inserted.

  • options (Hash) (defaults to: nil)

    Hash of orbitrary options for new middlware.



100
101
102
# File 'lib/ustack.rb', line 100

def insert_before(klass, new_klass, options=nil)
  @ustack.insert(index(klass), [new_klass, options])
end

#run(env = {}) ⇒ Object

Runs the middlware stack.

Examples:

class M1 < Struct.new(:app)
  def call(env)
    app.call(env)
    'zzz'
  end
end

class M2 < Struct.new(:app)
  def call(env)
    puts env[:my_env_key]
  end
end

app = Ustack do
  use M1
  use m2
end

puts app.run(my_env_key: 'xxx')
# => 'xxx'
# => 'zzz'

Parameters:

  • env (Hash) (defaults to: {})

    Initial environment.

Returns:

  • (Object)

    Value returned by the most outer middleware.



150
151
152
153
154
155
156
# File 'lib/ustack.rb', line 150

def run(env={})
  app = nil
  @ustack.reverse.each do |(klass, options)|
    app = options ? klass.new(app, options) : klass.new(app)
  end
  app.call(env)
end

#swap(old_klass, new_klass, options = nil) ⇒ Object

Swaps two middlewares.

Examples:

app = Ustack.new do
  use M1
end
app.use M2
app.use M2, my_option: 'xxx'

Parameters:

  • old_klass (Class)

    Middleware class to be replaced.

  • new_klass (Class)

    New middleware class.

  • options (Hash) (defaults to: nil)

    Hash of orbitrary options for new middlware.



83
84
85
# File 'lib/ustack.rb', line 83

def swap(old_klass, new_klass, options=nil)
  @ustack[index(old_klass)] = [new_klass, options]
end

#use(klass, options = nil) ⇒ Object

Tells the stack to use given middleware.

Examples:

app = Ustack.new do
  use M1
end
app.use M2
app.use M2, my_option: 'xxx'

Parameters:

  • klass (Class)

    Middleware class to be used.

  • options (Hash) (defaults to: nil)

    Hash of orbitrary options for new middlware.



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

def use(klass, options=nil)
  @ustack ||= []
  @ustack << [klass, options]
end