Class: SlackBot::Events::Middleware::Chain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/slack_bot/events/middleware/chain.rb

Constant Summary collapse

DEFAULT_ENTRIES =
{
  message: {
    base: [Middleware::EventTracer, Middleware::MessageHandler],
    immutable: [Middleware::MessageHandler]
  },
  open: { base: [Middleware::EventTracer] },
  close: { base: [Middleware::EventTracer] },
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:) ⇒ Chain

Returns a new instance of Chain.



30
31
32
# File 'lib/slack_bot/events/middleware/chain.rb', line 30

def initialize(type:)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/slack_bot/events/middleware/chain.rb', line 19

def type
  @type
end

Class Method Details

.default_entry(type) ⇒ Object



34
35
36
# File 'lib/slack_bot/events/middleware/chain.rb', line 34

def self.default_entry(type)
  DEFAULT_ENTRIES[type][:base].map { Entry.new(_1) }
end

Instance Method Details

#add(klass, *args) ⇒ Object



48
49
50
51
# File 'lib/slack_bot/events/middleware/chain.rb', line 48

def add(klass, *args)
  remove(klass)
  entries << Entry.new(klass, args)
end

#clearObject



88
89
90
# File 'lib/slack_bot/events/middleware/chain.rb', line 88

def clear
  @entries = nil
end

#each(&block) ⇒ Object



84
85
86
# File 'lib/slack_bot/events/middleware/chain.rb', line 84

def each(&block)
  entries.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/slack_bot/events/middleware/chain.rb', line 76

def empty?
  entries.nil? || entries.empty?
end

#entriesObject



38
39
40
# File 'lib/slack_bot/events/middleware/chain.rb', line 38

def entries
  @entries ||= self.class.default_entry(type)
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/slack_bot/events/middleware/chain.rb', line 72

def exists?(klass)
  any? { |entry| entry.klass == klass }
end

#insert_after(oldklass, newklass, *args) ⇒ Object



65
66
67
68
69
70
# File 'lib/slack_bot/events/middleware/chain.rb', line 65

def insert_after(oldklass, newklass, *args)
  i = entries.index { |entry| entry.klass == newklass }
  new_entry = i.nil? ? Entry.new(klass, args) : entries.delete_at(i)
  i = entries.index { |entry| entry.klass == oldklass } || entries.count - 1
  entries.insert(i + 1, new_entry)
end

#insert_before(oldklass, newklass, *args) ⇒ Object



58
59
60
61
62
63
# File 'lib/slack_bot/events/middleware/chain.rb', line 58

def insert_before(oldklass, newklass, *args)
  i = entries.index { |entry| entry.klass == newklass }
  new_entry = i.nil? ? Entry.new(klass, args) : entries.delete_at(i)
  i = entries.index { |entry| entry.klass == oldklass } || 0
  entries.insert(i, new_entry)
end

#inspect_meObject



128
129
130
131
132
133
134
135
# File 'lib/slack_bot/events/middleware/chain.rb', line 128

def inspect_me
  entries.map do |e|
    [
      e.klass,
      e.args
    ]
  end
end

#invoke(type:, socket_event:) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/slack_bot/events/middleware/chain.rb', line 114

def invoke(type:, socket_event:)
  return yield if empty?

  chain = retrieve
  traverse_chain = proc do
    if chain.empty?
      yield
    else
      chain.shift.call(type: type, socket_event: socket_event, &traverse_chain)
    end
  end
  traverse_chain.call
end

#invoke_message(type:, socket_event:, parsed_data:, websocket:, listener:, schema: nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/slack_bot/events/middleware/chain.rb', line 92

def invoke_message(type:, socket_event:, parsed_data:, websocket:, listener:, schema: nil)
  return yield if empty?

  chain = retrieve
  traverse_chain = proc do
    if chain.empty?
      yield
    else
      params = {
        parsed_data: parsed_data,
        schema: schema,
        socket_event: socket_event,
        listener: listener,
        type: type,
        websocket: websocket,
      }
      chain.shift.call(**params, &traverse_chain)
    end
  end
  traverse_chain.call
end

#prepend(klass, *args) ⇒ Object



53
54
55
56
# File 'lib/slack_bot/events/middleware/chain.rb', line 53

def prepend(klass, *args)
  remove(klass)
  entries.insert(0, Entry.new(klass, args))
end

#remove(klass) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
# File 'lib/slack_bot/events/middleware/chain.rb', line 42

def remove(klass)
  raise ArgumentError, "Unable to remove default Middleware #{klass}" if Array(DEFAULT_ENTRIES[@type][:immutable]).include?(klass)

  entries.delete_if { |entry| entry.klass == klass }
end

#retrieveObject



80
81
82
# File 'lib/slack_bot/events/middleware/chain.rb', line 80

def retrieve
  map(&:instance!)
end