Class: ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Stateful

Inherits:
Object
  • Object
show all
Defined in:
lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb,
lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful/exceptions.rb

Defined Under Namespace

Modules: Exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ void

Parameters:

  • kwargs (Hash{Symbol => Object})


28
29
30
31
32
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 28

def initialize(**kwargs)
  @name = kwargs.fetch(:name) { "Stack" }
  @stack = kwargs.fetch(:stack) { [] }
  @index = -1
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 22

def name
  @name
end

#stackObject (readonly)

Returns the value of attribute stack.



16
17
18
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 16

def stack
  @stack
end

Instance Method Details

#==(other) ⇒ Boolean?

Parameters:

  • other (Object)

    Can be any type.

Returns:

  • (Boolean, nil)


197
198
199
200
201
202
203
204
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 197

def ==(other)
  return unless other.instance_of?(self.class)

  return false if name != other.name
  return false if stack != other.stack

  true
end

#call(env) ⇒ Object

Returns Can be any type.

Parameters:

  • env (Hash{Symbol => Object})

Returns:

  • (Object)

    Can be any type.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 69

def call(env)
  return env if stack.empty?

  self.index += 1

  if index == stack.size - 1
    stack[index].call(env)
  else
    stack[index].new(self).call(env)
  end
ensure
  self.index = -1
end

#clearBoolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 52

def clear
  stack.clear

  self
end

#delete(index_or_middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack Also known as: remove



180
181
182
183
184
185
186
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 180

def delete(index_or_middleware)
  index = cast_index(index_or_middleware)

  stack.delete_at(index)

  self
end

#dupConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack



216
217
218
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 216

def dup
  self.class.new(name: name.dup, stack: stack.dup)
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 37

def empty?
  stack.empty?
end

#has?(some_middleware) ⇒ Boolean

Parameters:

  • some_middleware (#call<Hash>)

Returns:

  • (Boolean)


45
46
47
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 45

def has?(some_middleware)
  stack.any? { |middleware| middleware == some_middleware }
end

#insert(index_or_middleware, other_middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack Also known as: insert_before



114
115
116
117
118
119
120
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 114

def insert(index_or_middleware, other_middleware)
  index = cast_index(index_or_middleware)

  stack.insert(index, other_middleware)

  self
end

#insert_after(index_or_middleware, other_middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack



133
134
135
136
137
138
139
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 133

def insert_after(index_or_middleware, other_middleware)
  index = cast_index(index_or_middleware)

  stack.insert(index + 1, other_middleware)

  self
end

#insert_after_each(other_middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack



155
156
157
158
159
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 155

def insert_after_each(other_middleware)
  @stack = stack.reduce([]) { |stack, middleware| stack.push(middleware, other_middleware) }

  self
end

#insert_before_each(other_middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack



145
146
147
148
149
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 145

def insert_before_each(other_middleware)
  @stack = stack.reduce([]) { |stack, middleware| stack.push(other_middleware, middleware) }

  self
end

#replace(index_or_middleware, other_middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack



167
168
169
170
171
172
173
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 167

def replace(index_or_middleware, other_middleware)
  index = cast_index(index_or_middleware)

  stack[index] = other_middleware

  self
end

#to_aArray

Returns:

  • (Array)


209
210
211
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 209

def to_a
  stack
end

#unshift(middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack Also known as: prepend



87
88
89
90
91
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 87

def unshift(middleware)
  stack.unshift(middleware)

  self
end

#use(middleware) ⇒ ConvenientService::Support::Middleware::StackBuilder::Entities::Builders::Rack



102
103
104
105
106
# File 'lib/convenient_service/support/middleware/stack_builder/entities/builders/stateful.rb', line 102

def use(middleware)
  stack << middleware

  self
end