Class: Jets::Middleware::Stack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jets/middleware/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ Stack

Returns a new instance of Stack.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
# File 'lib/jets/middleware/stack.rb', line 6

def initialize(*args)
  @middlewares = []
  yield(self) if block_given?
end

Instance Attribute Details

#middlewaresObject

Returns the value of attribute middlewares.



5
6
7
# File 'lib/jets/middleware/stack.rb', line 5

def middlewares
  @middlewares
end

Instance Method Details

#[](i) ⇒ Object



23
24
25
# File 'lib/jets/middleware/stack.rb', line 23

def [](i)
  middlewares[i]
end

#build(app = Proc.new) ⇒ Object



61
62
63
# File 'lib/jets/middleware/stack.rb', line 61

def build(app = Proc.new)
  middlewares.freeze.reverse.inject(app) { |a, e| e.build(a) }
end

#delete(target) ⇒ Object



53
54
55
# File 'lib/jets/middleware/stack.rb', line 53

def delete(target)
  middlewares.delete_if { |m| m.klass == target }
end

#eachObject



11
12
13
# File 'lib/jets/middleware/stack.rb', line 11

def each
  @middlewares.each { |x| yield x }
end

#initialize_copy(other) ⇒ Object



31
32
33
# File 'lib/jets/middleware/stack.rb', line 31

def initialize_copy(other)
  self.middlewares = other.middlewares.dup
end

#insert(index, klass, *args, &block) ⇒ Object Also known as: insert_before



35
36
37
38
# File 'lib/jets/middleware/stack.rb', line 35

def insert(index, klass, *args, &block)
  index = assert_index(index, :before)
  middlewares.insert(index, build_middleware(klass, args, block))
end

#insert_after(index, *args, &block) ⇒ Object



42
43
44
45
# File 'lib/jets/middleware/stack.rb', line 42

def insert_after(index, *args, &block)
  index = assert_index(index, :after)
  insert(index + 1, *args, &block)
end

#lastObject



19
20
21
# File 'lib/jets/middleware/stack.rb', line 19

def last
  middlewares.last
end

#sizeObject



15
16
17
# File 'lib/jets/middleware/stack.rb', line 15

def size
  middlewares.size
end

#swap(target, *args, &block) ⇒ Object



47
48
49
50
51
# File 'lib/jets/middleware/stack.rb', line 47

def swap(target, *args, &block)
  index = assert_index(target, :before)
  insert(index, *args, &block)
  middlewares.delete_at(index + 1)
end

#unshift(klass, *args, &block) ⇒ Object



27
28
29
# File 'lib/jets/middleware/stack.rb', line 27

def unshift(klass, *args, &block)
  middlewares.unshift(build_middleware(klass, args, block))
end

#use(klass, *args, &block) ⇒ Object



57
58
59
# File 'lib/jets/middleware/stack.rb', line 57

def use(klass, *args, &block)
  middlewares.push(build_middleware(klass, args, block))
end