Class: CableReady::Channels

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cable_ready/channels.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChannels

Returns a new instance of Channels.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cable_ready/channels.rb', line 14

def initialize
  @channels = {}
  @operations = {}
  %i[
    add_css_class
    console_log
    dispatch_event
    inner_html
    insert_adjacent_html
    insert_adjacent_text
    morph
    notification
    outer_html
    remove
    remove_attribute
    remove_css_class
    set_attribute
    set_cookie
    set_dataset_property
    set_property
    set_style
    set_styles
    set_value
    text_content
  ].each do |operation|
    add_operation operation
  end
end

Instance Attribute Details

#operationsObject

Returns the value of attribute operations.



8
9
10
# File 'lib/cable_ready/channels.rb', line 8

def operations
  @operations
end

Class Method Details

.configure {|CableReady::Channels.instance| ... } ⇒ Object

Yields:



10
11
12
# File 'lib/cable_ready/channels.rb', line 10

def self.configure
  yield CableReady::Channels.instance if block_given?
end

Instance Method Details

#[](identifier) ⇒ Object



47
48
49
# File 'lib/cable_ready/channels.rb', line 47

def [](identifier)
  @channels[identifier] ||= CableReady::Channel.new(identifier, operations)
end

#add_operation(operation, &implementation) ⇒ Object



43
44
45
# File 'lib/cable_ready/channels.rb', line 43

def add_operation(operation, &implementation)
  @operations[operation] = implementation || ->(options = {}) { add_operation(operation, options) }
end

#broadcast(*identifiers, clear: true) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/cable_ready/channels.rb', line 51

def broadcast(*identifiers, clear: true)
  @channels.values
    .reject { |channel| identifiers.any? && identifiers.exclude?(channel.identifier) }
    .select { |channel| channel.identifier.is_a?(String) }
    .tap do |channels|
      channels.each { |channel| @channels[channel.identifier].broadcast(clear) }
      channels.each { |channel| @channels.except!(channel.identifier) if clear }
    end
end

#broadcast_to(model, *identifiers, clear: true) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/cable_ready/channels.rb', line 61

def broadcast_to(model, *identifiers, clear: true)
  @channels.values
    .reject { |channel| identifiers.any? && identifiers.exclude?(channel.identifier) }
    .reject { |channel| channel.identifier.is_a?(String) }
    .tap do |channels|
      channels.each { |channel| @channels[channel.identifier].broadcast_to(model, clear) }
      channels.each { |channel| @channels.except!(channel.identifier) if clear }
    end
end