Class: Meddleware::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/meddleware/stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Stack

Returns a new instance of Stack.



3
4
5
# File 'lib/meddleware/stack.rb', line 3

def initialize(&block)
  instance_eval(&block) if block_given?
end

Instance Method Details

#after(after_klass, *args, **kwargs, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/meddleware/stack.rb', line 22

def after(after_klass, *args, **kwargs, &block)
  entry = create_entry(args, kwargs, block)
  remove(entry[0])

  i = if after_klass.is_a? Array
    after_klass.map {|x| index(x) }.compact.max
  else
    index(after_klass)
  end
  i ||= count - 1 # last element

  stack.insert(i + 1, entry)
  self
end

#before(before_klass, *args, **kwargs, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/meddleware/stack.rb', line 37

def before(before_klass, *args, **kwargs, &block)
  entry = create_entry(args, kwargs, block)
  remove(entry[0])

  i = if before_klass.is_a? Array
    before_klass.map {|x| index(x) }.compact.min
  else
    index(before_klass)
  end
  i ||= 0 # first element

  stack.insert(i, entry)
  self
end

#call(*args, **kwargs) ⇒ Object



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
# File 'lib/meddleware/stack.rb', line 88

def call(*args, **kwargs)
  chain = build_chain
  default_args = args
  default_kwargs = kwargs

  traverse = proc do |*args, **kwargs|
    if args.empty? && kwargs.empty?
      args = default_args
      kwargs = default_kwargs
    else
      default_args = args
      default_kwargs = kwargs
    end

    if chain.empty?
      yield(*args, **kwargs) if block_given?
    else
      middleware = chain.shift

      if middleware.is_a?(Proc) && !middleware.lambda?
        middleware.call(*args, **kwargs)

        # implicit yield
        traverse.call(*args, **kwargs)
      else
        middleware.call(*args, **kwargs, &traverse)
      end
    end
  end

  traverse.call(*args, **kwargs)
end

#clearObject



80
81
82
# File 'lib/meddleware/stack.rb', line 80

def clear
  stack.clear
end

#countObject Also known as: size



75
76
77
# File 'lib/meddleware/stack.rb', line 75

def count
  stack.count
end

#empty?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/meddleware/stack.rb', line 84

def empty?
  stack.empty?
end

#include?(*klass) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/meddleware/stack.rb', line 52

def include?(*klass)
  klass.all? {|x| index(x) }
end

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



15
16
17
18
19
20
# File 'lib/meddleware/stack.rb', line 15

def prepend(*args, **kwargs, &block)
  entry = create_entry(args, kwargs, block)
  remove(entry[0])
  stack.insert(0, entry)
  self
end

#remove(*klass) ⇒ Object



56
57
58
59
# File 'lib/meddleware/stack.rb', line 56

def remove(*klass)
  stack.reject! { |entry| klass.include?(entry[0]) }
  self
end

#replace(old_klass, *args, **kwargs, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/meddleware/stack.rb', line 61

def replace(old_klass, *args, **kwargs, &block)
  entry = create_entry(args, kwargs, block)
  remove(entry[0])

  i = index(old_klass)

  unless i
    raise RuntimeError, "middleware not present: #{old_klass}"
  end

  stack[i] = entry
  self
end

#use(*args, **kwargs, &block) ⇒ Object Also known as: append



7
8
9
10
11
12
# File 'lib/meddleware/stack.rb', line 7

def use(*args, **kwargs, &block)
  entry = create_entry(args, kwargs, block)
  remove(entry[0])
  stack << entry
  self
end