Class: CableReady::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/cable_ready/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ Channel

Returns a new instance of Channel.



7
8
9
10
11
12
13
14
15
# File 'lib/cable_ready/channel.rb', line 7

def initialize(identifier)
  @identifier = identifier
  reset
  CableReady.config.operation_names.each { |name| add_operation_method name }

  config_observer = self
  CableReady.config.add_observer config_observer, :add_operation_method
  ObjectSpace.define_finalizer self, -> { CableReady.config.delete_observer config_observer }
end

Instance Attribute Details

#enqueued_operationsObject (readonly)

Returns the value of attribute enqueued_operations.



5
6
7
# File 'lib/cable_ready/channel.rb', line 5

def enqueued_operations
  @enqueued_operations
end

#identifierObject (readonly)

Returns the value of attribute identifier.



5
6
7
# File 'lib/cable_ready/channel.rb', line 5

def identifier
  @identifier
end

Instance Method Details

#add_operation_method(name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/cable_ready/channel.rb', line 27

def add_operation_method(name)
  return if respond_to?(name)
  singleton_class.public_send :define_method, name, ->(options = {}) {
    enqueued_operations[name.to_s] << options.stringify_keys
    self # supports operation chaining
  }
end

#broadcast(clear: true) ⇒ Object



17
18
19
20
# File 'lib/cable_ready/channel.rb', line 17

def broadcast(clear: true)
  ActionCable.server.broadcast identifier, {"cableReady" => true, "operations" => broadcastable_operations}
  reset if clear
end

#broadcast_to(model, clear: true) ⇒ Object



22
23
24
25
# File 'lib/cable_ready/channel.rb', line 22

def broadcast_to(model, clear: true)
  identifier.broadcast_to model, {"cableReady" => true, "operations" => broadcastable_operations}
  reset if clear
end