dexiecable 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b4a75838a91bf8b9ef988b1b2ef14f90e2148df75d80b15127487bb3b9efb8e
4
- data.tar.gz: 99ca5b8a8cf6a4b12eca7d54753b0de8c35dc699c22bb172a62b7ca826fa9f6a
3
+ metadata.gz: 85944f789c9d0a2a10d718fbab783e4c9b89e00bb2ad0fe2cb5804648660447c
4
+ data.tar.gz: a17bc5ccd07702c90ea508b9261391980fbfa68e4bf1a5d1b9dc4f85f6a8c417
5
5
  SHA512:
6
- metadata.gz: d77d14a92756f3a05c4c038f93f542953036d3df2e010952659a4833a48997fef2fcf4d145360f03d684a40b8fc25715a3aaf8f5624ff0f6bc3934568ff5806c
7
- data.tar.gz: ae7f67b4bf0ab26883afd3ba7c17fc944b6f7fc66f0f862c0c6cec6ff9d494ed749b2436e47d80898dc86d11ed1c44235e679e68b4ff3e9c1b662552e2536b59
6
+ metadata.gz: 62b8fbad4ae8392ec0da800697247833a62cdd1dd8165f95ed05c6b81e4b97a4984e75eb3179481139e9bf8f2378641eecb50e2e7ba5503c94f445a9e30e7128
7
+ data.tar.gz: 17330cb1f871afb2b9599596e2d8f1cd882fe323c686f9464490b779e1f2fdfbb4c9b6a9bacca0bd2eb5751ff4001a93cea4f0cbb9dbacae4ab1a1d6489ac2a0
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Run [Dexie.js](https://dexie.org) IndexedDB operations from your Rails ActionCable channels.
4
4
 
5
- DexieCable augments ActionCable channels with a query DSL that mirrors the Dexie.js API, letting you push database mutations from the server to the client in real time. It also gives you a `syncs_to_dexie` ActiveRecord macro for automatic change syncing.
5
+ DexieCable augments ActionCable channels with a query DSL that mirrors the Dexie.js API, letting you push database mutations from the server to the client in real time. It also gives you a [`syncs_to_dexie`](#syncs_to_dexie--automatic-model-syncing) ActiveRecord macro for automatic change syncing.
6
6
 
7
7
  You can run any Dexie table update directly inside a channel:
8
8
 
@@ -29,11 +29,11 @@ class NotificationsController < ApplicationController
29
29
  end
30
30
  ```
31
31
 
32
- An even more convenient way is to use the `syncs_to_dexie` macro:
32
+ An even more convenient way is to use the `syncs_to_dexie` macro (more info [below](#syncs_to_dexie--automatic-model-syncing))
33
33
 
34
34
  ```ruby
35
35
  class Notification < ApplicationRecord
36
- syncs_to_dexie via: UserChannel, subject: :user
36
+ syncs_to_dexie via: UserChannel, to: :user
37
37
  end
38
38
  ```
39
39
 
@@ -89,7 +89,7 @@ This gives you:
89
89
 
90
90
  | Method | Description |
91
91
  |---|---|
92
- | `self.[](subject)` | Returns a `ScopedChannel` bound to a subject. `UserChannel[current_user]` |
92
+ | `self.[](to)` | Returns a `ScopedChannel` bound to a recipient. `UserChannel[current_user]` |
93
93
  | `table(name)` | Starts a query chain. `table("messages")` |
94
94
 
95
95
  ### Chaining Dexie operations
@@ -125,34 +125,36 @@ The full query chain is serialized as JSON and sent over ActionCable. The JS cli
125
125
 
126
126
  ### `syncs_to_dexie` — automatic model syncing
127
127
 
128
- Add to any ActiveRecord model. `subject` is what gets passed to `broadcast_to` as the stream target:
128
+ Add to any ActiveRecord model. Just provide the channel class and the broadcast target.
129
129
 
130
130
  ```ruby
131
131
  class Message < ApplicationRecord
132
- # Calls send(:receiver), then broadcasts: broadcast_to(receiver, ...)
133
- syncs_to_dexie via: UserChannel, subject: :receiver
132
+ # Calls send(:receiver), then broadcasts: UserChannel.broadcast_to(receiver, ...)
133
+ syncs_to_dexie via: UserChannel, to: :receiver
134
134
 
135
- # String used directly: broadcast_to("global_feed", ...)
136
- syncs_to_dexie via: UserChannel, subject: "global_feed"
135
+ # String used directly: UserChannel.broadcast_to("global_feed", ...)
136
+ syncs_to_dexie via: UserChannel, to: "global_feed"
137
137
 
138
138
  # Procs are also supported. If an array is returned, multiple broadcasts are made
139
- # conversation.users.each { |u| broadcast_to(u, ...) }
140
- syncs_to_dexie via: UserChannel, subject: -> { conversation.users }
139
+ # conversation.users.each { |u| UserChannel.broadcast_to(u, ...) }
140
+ syncs_to_dexie via: UserChannel, to: -> { conversation.users }
141
141
  end
142
142
  ```
143
143
 
144
+ Internally, `syncs_to_dexie` sets up the following ActiveRecord callbacks:
145
+
144
146
  | Event | Action |
145
147
  |---|---|
146
- | `after_commit on: :create` | `channel.table("messages").add(record.as_json_for_dexie)` |
147
- | `after_commit on: :update` | `channel.table("messages").put(record.as_json_for_dexie)` |
148
- | `after_commit on: :destroy` | `channel.table("messages").delete(record.id)` |
148
+ | `after_commit on: :create` | `channel.table(table).add(record.as_json_for_dexie)` |
149
+ | `after_commit on: :update` | `channel.table(table).put(record.as_json_for_dexie)` |
150
+ | `after_commit on: :destroy` | `channel.table(table).delete(record.id)` |
149
151
 
150
152
  #### Options
151
153
 
152
154
  | Option | Default | Description |
153
155
  |---|---|---|
154
156
  | `via:` | *(required)* | A DexieCable channel class |
155
- | `subject:` | *(none)* | The stream target passed to `broadcast_to`. Symbol → calls `send`. String → used as-is. Proc → evaluated in record context. Returns a single subject or collection. |
157
+ | `to:` | *(none)* | The stream target passed to `broadcast_to`. Symbol → calls `send`. String → used as-is. Proc → evaluated in record context. Returns a single recipient or collection. |
156
158
  | `table:` | model's `table_name` | Override the Dexie table name. A Proc is evaluated in the record's context. |
157
159
  | `only:` | `[:create, :update, :destroy]` | Limit which events trigger a sync |
158
160
  | `if:` | *(none)* | Symbol (method name) or Proc — only sync when it returns truthy |
@@ -162,7 +164,7 @@ You can combine multiple `syncs_to_dexie` declarations, each with different cond
162
164
 
163
165
  ```ruby
164
166
  class Message < ApplicationRecord
165
- syncs_to_dexie via: UserChannel, subject: -> { sender },
167
+ syncs_to_dexie via: UserChannel, to: -> { sender },
166
168
  if: :published?
167
169
 
168
170
  syncs_to_dexie via: AdminChannel,
@@ -176,7 +178,7 @@ Override `as_json_for_dexie` in your model:
176
178
 
177
179
  ```ruby
178
180
  class Message < ApplicationRecord
179
- syncs_to_dexie via: UserChannel, subject: :sender
181
+ syncs_to_dexie via: UserChannel, to: :sender
180
182
 
181
183
  def as_json_for_dexie
182
184
  super.merge(room_name: room.name)
@@ -9,22 +9,22 @@ module DexieCable
9
9
  # DexieCable channel.
10
10
  #
11
11
  # class Message < ApplicationRecord
12
- # syncs_to_dexie via: UserChannel, subject: :sender
13
- # syncs_to_dexie via: UserChannel, subject: "global_feed"
14
- # syncs_to_dexie via: UserChannel, subject: -> { conversation.users }
12
+ # syncs_to_dexie via: UserChannel, to: :sender
13
+ # syncs_to_dexie via: UserChannel, to: "global_feed"
14
+ # syncs_to_dexie via: UserChannel, to: -> { conversation.users }
15
15
  # syncs_to_dexie via: PublicChannel
16
16
  # end
17
17
  #
18
- # @param via [Class] A DexieCable channel class. When +subject+
19
- # is given, each subject is mapped through
20
- # +via[subject]+ to produce scoped channels.
21
- # Without +subject+, +via+ is used directly as an
18
+ # @param via [Class] A DexieCable channel class. When +to+
19
+ # is given, each recipient is mapped through
20
+ # +via[to]+ to produce scoped channels.
21
+ # Without +to+, +via+ is used directly as an
22
22
  # unscoped channel.
23
- # @param subject [Proc, Symbol, String] A Proc evaluated in record
23
+ # @param to [Proc, Symbol, String] A Proc evaluated in record
24
24
  # context, a Symbol to call via +send+, or a String
25
25
  # used directly as the stream name for +broadcast_to+.
26
- # Must return a single subject or collection of
27
- # subjects.
26
+ # Must return a single recipient or collection of
27
+ # recipients.
28
28
  # @param table [String, Symbol, Proc] Override the Dexie table name
29
29
  # (defaults to the model's table_name). A Proc is
30
30
  # evaluated in the record's context.
@@ -34,18 +34,18 @@ module DexieCable
34
34
  # returns truthy (evaluated in the record's context).
35
35
  # @param unless [Symbol, Proc] Skip sync if the given method or proc
36
36
  # returns truthy (evaluated in the record's context).
37
- def syncs_to_dexie(via:, subject: nil, table: nil, only: nil, **options)
37
+ def syncs_to_dexie(via:, to: nil, table: nil, only: nil, **options)
38
38
  events = Array(only || %i[create update destroy])
39
39
  conditions = options.slice(:if, :unless)
40
40
 
41
41
  @dexie_sync_configs ||= []
42
- @dexie_sync_configs << { via: via, subject: subject, table: table, only: events, **conditions }
42
+ @dexie_sync_configs << { via: via, to: to, table: table, only: events, **conditions }
43
43
 
44
44
  if events.include?(:destroy)
45
45
  before_destroy :dexie_sync_before_destroy
46
46
 
47
47
  after_commit on: :destroy, **conditions do
48
- Array(resolve_channel(via, subject)).each do |channel|
48
+ Array(resolve_channel(via, to)).each do |channel|
49
49
  next unless channel
50
50
  channel.table(resolve_table(table)).delete(dexie_destroy_id)
51
51
  end
@@ -54,7 +54,7 @@ module DexieCable
54
54
 
55
55
  if events.include?(:create)
56
56
  after_commit on: :create, **conditions do
57
- Array(resolve_channel(via, subject)).each do |channel|
57
+ Array(resolve_channel(via, to)).each do |channel|
58
58
  next unless channel
59
59
  channel.table(resolve_table(table)).add(as_json_for_dexie)
60
60
  end
@@ -63,7 +63,7 @@ module DexieCable
63
63
 
64
64
  if events.include?(:update)
65
65
  after_commit on: :update, **conditions do
66
- Array(resolve_channel(via, subject)).each do |channel|
66
+ Array(resolve_channel(via, to)).each do |channel|
67
67
  next unless channel
68
68
 
69
69
  changes = as_json_for_dexie.slice(*saved_changes.keys)
@@ -76,20 +76,20 @@ module DexieCable
76
76
 
77
77
  private
78
78
 
79
- def resolve_channel(via, subject = nil)
80
- if subject
81
- subjects = resolve_subject(subject)
82
- Array(subjects).map { |s| via[s] }
79
+ def resolve_channel(via, to = nil)
80
+ if to
81
+ recipients = resolve_recipient(to)
82
+ Array(recipients).map { |r| via[r] }
83
83
  else
84
84
  via
85
85
  end
86
86
  end
87
87
 
88
- def resolve_subject(subject)
89
- case subject
90
- when Proc then instance_exec(&subject)
91
- when Symbol then send(subject)
92
- else subject
88
+ def resolve_recipient(to)
89
+ case to
90
+ when Proc then instance_exec(&to)
91
+ when Symbol then send(to)
92
+ else to
93
93
  end
94
94
  end
95
95
 
@@ -6,12 +6,12 @@ module DexieCable
6
6
  included do
7
7
  public :transmit
8
8
 
9
- # Open a scoped channel for broadcasting to a specific subject.
9
+ # Open a scoped channel for broadcasting to a specific recipient.
10
10
  #
11
11
  # UserChannel[current_user].table("notifications").add(notification)
12
12
  #
13
- def self.[](subject)
14
- ScopedChannel.new(self, subject)
13
+ def self.[](recipient)
14
+ ScopedChannel.new(self, recipient)
15
15
  end
16
16
 
17
17
  # Build a query against a Dexie table, transmitted to all subscribers
@@ -2,9 +2,9 @@
2
2
 
3
3
  module DexieCable
4
4
  class ScopedChannel
5
- def initialize(klass, subject)
6
- @klass = klass
7
- @subject = subject
5
+ def initialize(klass, recipient)
6
+ @klass = klass
7
+ @recipient = recipient
8
8
  end
9
9
 
10
10
  def table(name)
@@ -12,7 +12,7 @@ module DexieCable
12
12
  end
13
13
 
14
14
  def transmit(data)
15
- @klass.broadcast_to @subject, data
15
+ @klass.broadcast_to @recipient, data
16
16
  end
17
17
  end
18
18
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DexieCable
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dexiecable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Buhrmester