cable_ready 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de7cd175a02323ba1dd795cea4e1cfc9a4938309
4
- data.tar.gz: 9e551c0f04f2e54ee2b7189a87565a3db3ad001d
3
+ metadata.gz: 13c675045811966ee490dd09ce772bda11f4280f
4
+ data.tar.gz: b7b7a10cfd1c765d6a9c8f2400c3c2cc33c8ad88
5
5
  SHA512:
6
- metadata.gz: ddbbafcfbaeaa0b2e994023f2c58f3c8da2e3f18cbfb5d32c300294aaffcbd691d8d345b1d8ee3a292c35e5319b3bc5728305d4252dc37010d646e621ff72b54
7
- data.tar.gz: 1f588e0054df0d8b33e70f3646e99c36dfaa9b0bd0650dfadb93158481c7a815fc21e062778760a7b9cd1a395b7e4af8990b983b8b41424fab470d177de57718
6
+ metadata.gz: 2b4e30d36f53018d4c3e95a6b111c7c20c77be12c121211d458a7d507b3c07f78e501df858baf64f653b173a4bde0eb25af49ea8e37892ecaf2f665663248963
7
+ data.tar.gz: 61c307f9f518d4fa7250fe0ca2df2ea5c10994f91af2de2dbfcd645e98b9ad30c7086ea710b1f0a9d6f8b9676f83d2177c275048fb46fad7b7869613b73a3b39
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cable_ready (1.0.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.1.4)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (~> 0.7)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ binding_of_caller (0.7.2)
16
+ debug_inspector (>= 0.0.1)
17
+ byebug (9.0.6)
18
+ coderay (1.1.1)
19
+ concurrent-ruby (1.0.5)
20
+ coveralls (0.8.21)
21
+ json (>= 1.8, < 3)
22
+ simplecov (~> 0.14.1)
23
+ term-ansicolor (~> 1.3)
24
+ thor (~> 0.19.4)
25
+ tins (~> 1.6)
26
+ debug_inspector (0.0.3)
27
+ docile (1.1.5)
28
+ i18n (0.8.6)
29
+ interception (0.5)
30
+ json (2.1.0)
31
+ method_source (0.8.2)
32
+ minitest (5.10.3)
33
+ mustermann (1.0.0)
34
+ os (1.0.0)
35
+ pry (0.10.4)
36
+ coderay (~> 1.1.0)
37
+ method_source (~> 0.8.1)
38
+ slop (~> 3.4)
39
+ pry-byebug (3.4.2)
40
+ byebug (~> 9.0)
41
+ pry (~> 0.10)
42
+ pry-rescue (1.4.5)
43
+ interception (>= 0.5)
44
+ pry
45
+ pry-stack_explorer (0.4.9.2)
46
+ binding_of_caller (>= 0.7)
47
+ pry (>= 0.9.11)
48
+ pry-test (0.6.4)
49
+ os
50
+ pry
51
+ pry-byebug
52
+ pry-rescue
53
+ pry-stack_explorer
54
+ rack (2.0.3)
55
+ rack-protection (2.0.0)
56
+ rack
57
+ rake (10.5.0)
58
+ simplecov (0.14.1)
59
+ docile (~> 1.1.0)
60
+ json (>= 1.8, < 3)
61
+ simplecov-html (~> 0.10.0)
62
+ simplecov-html (0.10.1)
63
+ sinatra (2.0.0)
64
+ mustermann (~> 1.0)
65
+ rack (~> 2.0)
66
+ rack-protection (= 2.0.0)
67
+ tilt (~> 2.0)
68
+ slop (3.6.0)
69
+ term-ansicolor (1.6.0)
70
+ tins (~> 1.0)
71
+ thor (0.19.4)
72
+ thread_safe (0.3.6)
73
+ tilt (2.0.7)
74
+ tins (1.14.0)
75
+ tzinfo (1.2.3)
76
+ thread_safe (~> 0.1)
77
+
78
+ PLATFORMS
79
+ ruby
80
+
81
+ DEPENDENCIES
82
+ cable_ready!
83
+ coveralls
84
+ pry-test
85
+ rake
86
+ sinatra
87
+
88
+ BUNDLED WITH
89
+ 1.15.4
data/README.md CHANGED
@@ -1,3 +1,124 @@
1
+ [![Lines of Code](http://img.shields.io/badge/lines_of_code-80-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
2
+ [![Code Status](http://img.shields.io/codeclimate/github/hopsoft/cable_ready.svg?style=flat)](https://codeclimate.com/github/hopsoft/cable_ready)
3
+ [![Dependency Status](http://img.shields.io/gemnasium/hopsoft/cable_ready.svg?style=flat)](https://gemnasium.com/hopsoft/cable_ready)
4
+
1
5
  # CableReady
2
6
 
3
- Coming soon...
7
+ CableReady provides a standard interface for invoking common client-side DOM operations
8
+ from the server via Ruby on Rails' [ActionCable](http://guides.rubyonrails.org/action_cable_overview.html).
9
+
10
+ _NOTE: It's possible to invoke multiple DOM operations with a single ActionCable broadcast._
11
+
12
+ ## Supported DOM Operations
13
+
14
+ ### [dispatchEvent](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent)
15
+
16
+ Dispatches a DOM event in the browser.
17
+
18
+ ```ruby
19
+ cable_ready_broadcast "MyChannel", dispatch_event: [{
20
+ event_name: "string", # required - the name of the DOM event to dispatch (can be custom)
21
+ element_id: "string", # [window] - the DOM element id of the desired event target
22
+ detail: "object" # [null] - assigned to event.detail
23
+ }]
24
+ ```
25
+
26
+ ### [innerHTML](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML)
27
+
28
+ Sets the innerHTML of a DOM element.
29
+
30
+ ```ruby
31
+ cable_ready_broadcast "MyChannel", inner_html: [{
32
+ element_id: "string", # required - the DOM element id of the element to be mutated
33
+ html: "string" # [null] - the HTML to assign
34
+ }]
35
+ ```
36
+
37
+ ### [insertAdjacentHTML](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML)
38
+
39
+ Inserts HTML into the DOM relative to an element.
40
+ Supports behavior akin to prepend & append.
41
+
42
+ ```ruby
43
+ cable_ready_broadcast "MyChannel", insert_adjacent_html: [{
44
+ element_id: "string", # required - the DOM element id of the referenced element
45
+ position: "string", # [beforeend] - the relative position to the DOM element (beforebegin, afterbegin, beforeend, afterend)
46
+ html: "string" # [null] - the HTML to assign
47
+ }]
48
+ ```
49
+
50
+ ### [remove](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove)
51
+
52
+ Removes an element from the DOM.
53
+
54
+ ```ruby
55
+ cable_ready_broadcast "MyChannel", remove: [{
56
+ element_id: "string" # required - the DOM element id of the element to be removed
57
+ }]
58
+ ```
59
+
60
+ ### [replace](https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild)
61
+
62
+ Replaces a DOM element with new HTML.
63
+
64
+ ```ruby
65
+ cable_ready_broadcast "MyChannel", replace: [{
66
+ element_id: "string", # required - the DOM element id of the element to be replaced
67
+ html: "string" # [null] - the HTML to use as replacement
68
+ }]
69
+ ```
70
+
71
+ ### [textContent](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
72
+
73
+ Sets the text content of a DOM element.
74
+
75
+ ```ruby
76
+ cable_ready_broadcast "MyChannel", text_content: [{
77
+ element_id: "string", # required - the DOM element id of the element to be mutated
78
+ text: "string" # [null] - the text to assign
79
+ }]
80
+ ```
81
+
82
+ ## Quick Start
83
+
84
+ > Please read the official [ActionCable docs](http://guides.rubyonrails.org/action_cable_overview.html) to learn more about ActionCable before proceeding.
85
+
86
+ ```ruby
87
+ # app/models/user.rb
88
+ class User < ApplicationRecord
89
+ include CableReady::Broadcaster
90
+
91
+ def broadcast_name_change
92
+ cable_ready_broadcast "UserChannel", text_content: [{ element_id: "user-name", text: name }]
93
+ end
94
+ end
95
+ ```
96
+
97
+ ```javascript
98
+ // app/assets/javascripts/application.js
99
+ /*
100
+ * ...
101
+ *= require cable_ready
102
+ * ...
103
+ */
104
+ ```
105
+
106
+ ```javascript
107
+ // app/assets/javascripts/channels/user.js
108
+ App.cable.subscriptions.create({ channel: "UserChannel" }, {
109
+ received: function (data) {
110
+ if (data.cableReady) {
111
+ CableReady.perform(data.operations);
112
+ }
113
+ }
114
+ });
115
+ ```
116
+
117
+ ## Testing & Experimenting
118
+
119
+ Learn more about CableReady by running the [CableReady Test](https://github.com/hopsoft/cable_ready_test) project.
120
+
121
+ ## Advanced Usage
122
+
123
+ If you prefer server rendering, consider using CableReady in concert with a gem like
124
+ [SelfRenderer](https://github.com/hopsoft/self_renderer) for a simple yet powerful workflow.
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+
3
+ task :default => [:test]
4
+
5
+ task :test do
6
+ exec "bundle exec pry-test --disable-pry"
7
+ end
8
+
@@ -0,0 +1,45 @@
1
+ module CableReady
2
+ module Broadcaster
3
+ extend ::ActiveSupport::Concern
4
+
5
+ # Example Payload:
6
+ #
7
+ # {
8
+ # dispatch_event: [{
9
+ # event_name: "string",
10
+ # element_id: "string",
11
+ # detail: "object"
12
+ # }, ...],
13
+ #
14
+ # inner_html: [{
15
+ # element_id: "string",
16
+ # html: "string"
17
+ # }, ...],
18
+ #
19
+ # insert_adjacent_html: [{
20
+ # element_id: "string",
21
+ # position: "string",
22
+ # html: "string"
23
+ # }, ...],
24
+ #
25
+ # remove: [{
26
+ # element_id: "string"
27
+ # }, ...],
28
+ #
29
+ # replace: [{
30
+ # element_id: "string",
31
+ # html: "string"
32
+ # }, ...],
33
+ #
34
+ # textContent: [{
35
+ # element_id: "string",
36
+ # text: "string"
37
+ # }, ...]
38
+ # }
39
+ def cable_ready_broadcast(channel, operations={})
40
+ operations ||= {}
41
+ operations = operations.deep_transform_keys { |key| key.to_s.camelize(:lower) }
42
+ ActionCable.server.broadcast channel, "cableReady" => true, "operations" => operations
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module CableReady
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/cable_ready.rb CHANGED
@@ -1,5 +1,10 @@
1
+ require "active_support/all"
1
2
  require "cable_ready/version"
3
+ require "cable_ready/broadcaster"
2
4
 
3
5
  module CableReady
4
- # Your code goes here...
6
+ module Rails
7
+ class Engine < ::Rails::Engine
8
+ end
9
+ end
5
10
  end
@@ -0,0 +1,66 @@
1
+ (function () {
2
+ "use strict";
3
+
4
+ function log (operation, config) {
5
+ if (window.CableReady.debug) {
6
+ console.log("CableReady", operation, config);
7
+ }
8
+ }
9
+
10
+ var CableReadyOperations = {
11
+ dispatchEvent: function (config) {
12
+ log("dispatchEvent", config);
13
+ var event = new Event(config.eventName);
14
+ var target = document.getElementById(config.elementId) || window;
15
+ var event = new Event(config.eventName);
16
+ event.detail = config.detail;
17
+ target.dispatchEvent(event);
18
+ },
19
+
20
+ innerHtml: function (config) {
21
+ log("innerHTML", config);
22
+ var element = document.getElementById(config.elementId);
23
+ element.innerHTML = config.html;
24
+ },
25
+
26
+ insertAdjacentHtml: function (config) {
27
+ log("insertAdjacentHTML", config);
28
+ var element = document.getElementById(config.elementId);
29
+ element.insertAdjacentHTML(config.position || "beforeend", config.html);
30
+ },
31
+
32
+ remove: function (config) {
33
+ log("remove", config);
34
+ var element = document.getElementById(config.elementId);
35
+ element.remove(element);
36
+ },
37
+
38
+ replace: function (config) {
39
+ log("replace", config);
40
+ var element = document.getElementById(config.elementId);
41
+ var clone = element.cloneNode(false);
42
+ clone.innerHTML = config.html;
43
+ element.parentNode.replaceChild(clone, element);
44
+ },
45
+
46
+ textContent: function (config) {
47
+ log("textContent", config);
48
+ var element = document.getElementById(config.elementId);
49
+ element.textContent = config.content;
50
+ }
51
+ };
52
+
53
+ window.CableReady = {
54
+ debug: false,
55
+ perform: function (operations) {
56
+ for (var name in operations) {
57
+ if (operations.hasOwnProperty(name)) {
58
+ var entries = operations[name];
59
+ for (var i = 0; i < entries.length; i++) {
60
+ CableReadyOperations[name](entries[i]);
61
+ }
62
+ }
63
+ }
64
+ }
65
+ };
66
+ })();
metadata CHANGED
@@ -1,43 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cable_ready
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-19 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.14'
20
- type: :development
19
+ version: '0'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.14'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
32
74
  - !ruby/object:Gem::Version
33
- version: '10.0'
75
+ version: '0'
34
76
  type: :development
35
77
  prerelease: false
36
78
  version_requirements: !ruby/object:Gem::Requirement
37
79
  requirements:
38
- - - "~>"
80
+ - - ">="
39
81
  - !ruby/object:Gem::Version
40
- version: '10.0'
82
+ version: '0'
41
83
  description:
42
84
  email:
43
85
  - natehop@gmail.com
@@ -45,16 +87,17 @@ executables: []
45
87
  extensions: []
46
88
  extra_rdoc_files: []
47
89
  files:
48
- - ".gitignore"
49
90
  - Gemfile
91
+ - Gemfile.lock
50
92
  - LICENSE.txt
51
93
  - README.md
52
94
  - Rakefile
53
95
  - bin/console
54
96
  - bin/setup
55
- - cable_ready.gemspec
56
97
  - lib/cable_ready.rb
98
+ - lib/cable_ready/broadcaster.rb
57
99
  - lib/cable_ready/version.rb
100
+ - vendor/assets/javascripts/cable_ready.js
58
101
  homepage: https://github.com/hopsoft/cable_ready
59
102
  licenses:
60
103
  - MIT
@@ -75,8 +118,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
118
  version: '0'
76
119
  requirements: []
77
120
  rubyforge_project:
78
- rubygems_version: 2.6.11
121
+ rubygems_version: 2.6.13
79
122
  signing_key:
80
123
  specification_version: 4
81
- summary: Coming soon...
124
+ summary: put something here
82
125
  test_files: []
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/cable_ready.gemspec DELETED
@@ -1,25 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'cable_ready/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "cable_ready"
8
- spec.version = CableReady::VERSION
9
- spec.authors = ["Nathan Hopkins"]
10
- spec.email = ["natehop@gmail.com"]
11
-
12
- spec.summary = "Coming soon..."
13
- spec.homepage = "https://github.com/hopsoft/cable_ready"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
- f.match(%r{^(test|spec|features)/})
18
- end
19
- spec.bindir = "exe"
20
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
- spec.require_paths = ["lib"]
22
-
23
- spec.add_development_dependency "bundler", "~> 1.14"
24
- spec.add_development_dependency "rake", "~> 10.0"
25
- end