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(name) ⇒ Channel

Example Operations Payload:

{

# DOM Events ..................................................................................................

dispatch_event: [{
  name:     "string",
  detail:   "object",
  selector: "string",
}, ...],

# Element Mutations ...........................................................................................

morph: [{
  selector:      "string",
  focusSelector: "string",
  html:          "string"
}, ...],

inner_html: [{
  selector:      "string",
  focusSelector: "string",
  html:          "string"
}, ...],

text_content: [{
  selector: "string",
  text:     "string"
}, ...]

insert_adjacent_html: [{
  selector:      "string",
  focusSelector: "string",
  position:      "string",
  html:          "string"
}, ...],

insert_adjacent_text: [{
  selector: "string",
  position: "string",
  text:     "string"
}, ...],

remove: [{
  selector:      "string",
  focusSelector: "string,
}, ...],

replace: [{
  selector:      "string",
  focusSelector: "string",
  html:          "string"
}, ...],

set_value: [{
  selector: "string",
  value:    "string"
}, ...],

# Attribute Mutations .........................................................................................

set_attribute: [{
  selector: "string",
  name:     "string",
  value:    "string"
}, ...],

remove_attribute: [{
  selector: "string",
  name:     "string"
}, ...],

# CSS Class Mutations .........................................................................................

add_css_class: [{
  selector: "string",
  name:     "string"
}, ...],

remove_css_class: [{
  selector: "string",
  name:     "string"
}, ...],

# Dataset Mutations ...........................................................................................

set_dataset_property: [{
  selector: "string",
  name:     "string",
  value:    "string"
}, ...],

}



100
101
102
103
104
# File 'lib/cable_ready/channel.rb', line 100

def initialize(name)
  @name = name
  @operations = stub
  @compress_html = true
end

Instance Attribute Details

#compress_htmlObject

Returns the value of attribute compress_html.



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

def compress_html
  @compress_html
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
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

#add_css_class(options = {}) ⇒ Object



165
166
167
# File 'lib/cable_ready/channel.rb', line 165

def add_css_class(options={})
  operations[:add_css_class] << options
end

#broadcastObject



110
111
112
113
114
115
# File 'lib/cable_ready/channel.rb', line 110

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

#clearObject



106
107
108
# File 'lib/cable_ready/channel.rb', line 106

def clear
  @operations = stub
end

#dispatch_event(options = {}) ⇒ Object



117
118
119
# File 'lib/cable_ready/channel.rb', line 117

def dispatch_event(options={})
  operations[:dispatch_event] << options
end

#inner_html(options = {}) ⇒ Object



126
127
128
129
# File 'lib/cable_ready/channel.rb', line 126

def inner_html(options={})
  options[:html] = html_compressor.compress(options[:html].to_s) if compress_html
  operations[:inner_html] << options
end

#insert_adjacent_html(options = {}) ⇒ Object



135
136
137
138
# File 'lib/cable_ready/channel.rb', line 135

def insert_adjacent_html(options={})
  options[:html] = html_compressor.compress(options[:html].to_s) if compress_html
  operations[:insert_adjacent_html] << options
end

#insert_adjacent_text(options = {}) ⇒ Object



140
141
142
# File 'lib/cable_ready/channel.rb', line 140

def insert_adjacent_text(options={})
  operations[:insert_adjacent_text] << options
end

#morph(options = {}) ⇒ Object



121
122
123
124
# File 'lib/cable_ready/channel.rb', line 121

def morph(options={})
  options[:html] = html_compressor.compress(options[:html].to_s) if compress_html
  operations[:morph] << options
end

#remove(options = {}) ⇒ Object



144
145
146
# File 'lib/cable_ready/channel.rb', line 144

def remove(options={})
  operations[:remove] << options
end

#remove_attribute(options = {}) ⇒ Object



161
162
163
# File 'lib/cable_ready/channel.rb', line 161

def remove_attribute(options={})
  operations[:remove_attribute] << options
end

#remove_css_class(options = {}) ⇒ Object



169
170
171
# File 'lib/cable_ready/channel.rb', line 169

def remove_css_class(options={})
  operations[:remove_css_class] << options
end

#replace(options = {}) ⇒ Object



148
149
150
151
# File 'lib/cable_ready/channel.rb', line 148

def replace(options={})
  options[:html] = html_compressor.compress(options[:html].to_s) if compress_html
  operations[:replace] << options
end

#set_attribute(options = {}) ⇒ Object



157
158
159
# File 'lib/cable_ready/channel.rb', line 157

def set_attribute(options={})
  operations[:set_attribute] << options
end

#set_dataset_property(options = {}) ⇒ Object



173
174
175
# File 'lib/cable_ready/channel.rb', line 173

def set_dataset_property(options={})
  operations[:set_dataset_property] << options
end

#set_value(options = {}) ⇒ Object



153
154
155
# File 'lib/cable_ready/channel.rb', line 153

def set_value(options={})
  operations[:set_value] << options
end

#text_content(options = {}) ⇒ Object



131
132
133
# File 'lib/cable_ready/channel.rb', line 131

def text_content(options={})
  operations[:text_content] << options
end