Class: CableReady::Channel
- Inherits:
-
Object
- Object
- CableReady::Channel
- Defined in:
- lib/cable_ready/channel.rb
Instance Attribute Summary collapse
-
#enqueued_operations ⇒ Object
readonly
Returns the value of attribute enqueued_operations.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
Instance Method Summary collapse
- #add_operation_method(name) ⇒ Object
- #broadcast(clear: true) ⇒ Object
- #broadcast_to(model, clear: true) ⇒ Object
-
#initialize(identifier) ⇒ Channel
constructor
A new instance of Channel.
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_operations ⇒ Object (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 |
#identifier ⇒ Object (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, ->( = {}) { enqueued_operations[name.to_s] << .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 |