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, available_operations) ⇒ Channel

Returns a new instance of Channel.



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

def initialize(identifier, available_operations)
  @identifier = identifier
  @available_operations = available_operations
  reset
  available_operations.each do |available_operation, implementation|
    define_singleton_method available_operation, &implementation
  end
end

Instance Attribute Details

#available_operationsObject (readonly)

Returns the value of attribute available_operations.



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

def available_operations
  @available_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

#operationsObject (readonly)

Returns the value of attribute operations.



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

def operations
  @operations
end

Instance Method Details

#broadcast(clear) ⇒ Object



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

def broadcast(clear)
  operations.select! { |_, list| list.present? }
  operations.deep_transform_keys! { |key| key.to_s.camelize(:lower) }
  ActionCable.server.broadcast identifier, "cableReady" => true, "operations" => operations
  reset if clear
end

#broadcast_to(model, clear) ⇒ Object



23
24
25
26
27
28
# File 'lib/cable_ready/channel.rb', line 23

def broadcast_to(model, clear)
  operations.select! { |_, list| list.present? }
  operations.deep_transform_keys! { |key| key.to_s.camelize(:lower) }
  identifier.broadcast_to model, "cableReady" => true, "operations" => operations
  reset if clear
end