Class: Metybur::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/metybur/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection_name, websocket) ⇒ Collection

Returns a new instance of Collection.



2
3
4
5
6
# File 'lib/metybur/collection.rb', line 2

def initialize(collection_name, websocket)
  @collection_name = collection_name
  @callbacks = {}
  self.websocket = websocket
end

Instance Method Details

#on(event, &block) ⇒ Object



15
16
17
18
# File 'lib/metybur/collection.rb', line 15

def on(event, &block)
  callbacks_for(event) << block
  self
end

#websocket=(websocket) ⇒ Object



8
9
10
11
12
13
# File 'lib/metybur/collection.rb', line 8

def websocket=(websocket)
  websocket.on(:message) do |event|
    attributes = JSON.parse(event.data, symbolize_names: true)
    handle_message(attributes) if attributes[:collection] == @collection_name
  end
end