Class: SlackBot::Events::Middleware::Chain
- Inherits:
-
Object
- Object
- SlackBot::Events::Middleware::Chain
- 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
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #add(klass, *args) ⇒ Object
- #clear ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #entries ⇒ Object
- #exists?(klass) ⇒ Boolean
-
#initialize(type:) ⇒ Chain
constructor
A new instance of Chain.
- #insert_after(oldklass, newklass, *args) ⇒ Object
- #insert_before(oldklass, newklass, *args) ⇒ Object
- #inspect_me ⇒ Object
- #invoke(type:, socket_event:) ⇒ Object
- #invoke_message(type:, socket_event:, parsed_data:, websocket:, listener:, schema: nil) ⇒ Object
- #prepend(klass, *args) ⇒ Object
- #remove(klass) ⇒ Object
- #retrieve ⇒ Object
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
#type ⇒ Object (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 |
#clear ⇒ Object
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
76 77 78 |
# File 'lib/slack_bot/events/middleware/chain.rb', line 76 def empty? entries.nil? || entries.empty? end |
#entries ⇒ Object
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
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_me ⇒ Object
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 (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
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 |
#retrieve ⇒ Object
80 81 82 |
# File 'lib/slack_bot/events/middleware/chain.rb', line 80 def retrieve map(&:instance!) end |