dexiecable 0.1.3 → 0.1.4

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: 4ad9a4f94d6491f661821d79ac6a8fa3c36527927f8fb0916eee26c87294ebdf
4
- data.tar.gz: d50524564c23e50423e186edd1ed5883d9a4e1575fc687d9d80036fcac6201ec
3
+ metadata.gz: 8d99eb3ba3c4e24ad9e780707ae8f9a1d7d49dad075e38350851d47a215920ca
4
+ data.tar.gz: 4e9922d041d377b10377db9a2673980523690fe2790f48d4ce04c15b361941dc
5
5
  SHA512:
6
- metadata.gz: 017f3a4b81fe35bbc8fc17ad5b22cd8a0400ce3e4a14aa20942a517f8030c1cbf08c67b1e590b090726f20dd67c5b71869319fa568b07e01ebdc320867a829be
7
- data.tar.gz: 9ccd364a0a6a6b1960bcd5c9af17475b5e972dbfc2ee839bb86d9cb3bf69fedfeb3ccbc20b471e9d6dfe42f9e458f59f32cdd158d1f2c5f6f40f1b022e41f9d8
6
+ metadata.gz: 88434bc692480d1624a68d06fd1a8f6d1ada2ecaf59f023fa5168008a14bdcc5c9517e5dfa5e199a33e8053f3d0c5faf41eac8bab80756ad87b392f9f707247b
7
+ data.tar.gz: 3e44885987108b69e7160925fcb5908ac5dafa981557dac7ec46caf2e4faa83efdb777950a9011eb01d5692f6c3dd1241ecfef2d9fd7b74a2c8f2aa35ca29f60
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_dexia` 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_dexia` macro:
33
33
 
34
34
  ```ruby
35
35
  class Notification < ApplicationRecord
36
- syncs_to_dexie via: -> { UserChannel[user] }
36
+ syncs_to_dexia via: UserChannel, subject: :user
37
37
  end
38
38
  ```
39
39
 
@@ -47,7 +47,7 @@ Add to your `Gemfile`:
47
47
  gem "dexiecable"
48
48
  ```
49
49
 
50
- Then `bundle install`. The Railtie automatically extends `ActiveRecord::Base` with `syncs_to_dexie`.
50
+ Then `bundle install`. The Railtie automatically extends `ActiveRecord::Base` with `syncs_to_dexia`.
51
51
 
52
52
  ### npm package
53
53
 
@@ -123,17 +123,21 @@ UserChannel[current_user]
123
123
 
124
124
  The full query chain is serialized as JSON and sent over ActionCable. The JS client replays every method call against the local Dexie database in order.
125
125
 
126
- ### `syncs_to_dexie` — automatic model syncing
126
+ ### `syncs_to_dexia` — automatic model syncing
127
127
 
128
- Add to any ActiveRecord model:
128
+ Add to any ActiveRecord model. `subject` is what gets passed to `broadcast_to` as the stream target:
129
129
 
130
130
  ```ruby
131
131
  class Message < ApplicationRecord
132
- # Sync to a single user.
133
- syncs_to_dexie via: -> { UserChannel[sender] }
132
+ # Calls send(:receiver), then broadcasts: broadcast_to(receiver, ...)
133
+ syncs_to_dexia via: UserChannel, subject: :receiver
134
134
 
135
- # Sync to all participants of a conversation.
136
- syncs_to_dexie via: -> { conversation.participants.map { |u| UserChannel[u] } }
135
+ # String used directly: broadcast_to("global_feed", ...)
136
+ syncs_to_dexia via: UserChannel, subject: "global_feed"
137
+
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_dexia via: UserChannel, subject: -> { conversation.users }
137
141
  end
138
142
  ```
139
143
 
@@ -147,7 +151,8 @@ end
147
151
 
148
152
  | Option | Default | Description |
149
153
  |---|---|---|
150
- | `via:` | *(required)* | Proc (evaluated in record context), a channel, or an array of channels |
154
+ | `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. |
151
156
  | `table:` | model's `table_name` | Override the Dexie table name. A Proc is evaluated in the record's context. |
152
157
  | `only:` | `[:create, :update, :destroy]` | Limit which events trigger a sync |
153
158
  | `if:` | *(none)* | Symbol (method name) or Proc — only sync when it returns truthy |
@@ -157,10 +162,10 @@ You can combine multiple `syncs_to_dexie` declarations, each with different cond
157
162
 
158
163
  ```ruby
159
164
  class Message < ApplicationRecord
160
- syncs_to_dexie via: -> { UserChannel[sender] },
165
+ syncs_to_dexia via: UserChannel, subject: -> { sender },
161
166
  if: :published?
162
167
 
163
- syncs_to_dexie via: -> { AdminChannel },
168
+ syncs_to_dexia via: AdminChannel,
164
169
  unless: -> { draft? }
165
170
  end
166
171
  ```
@@ -171,7 +176,7 @@ Override `as_json_for_dexie` in your model:
171
176
 
172
177
  ```ruby
173
178
  class Message < ApplicationRecord
174
- syncs_to_dexie via: -> { UserChannel[sender] }
179
+ syncs_to_dexia via: UserChannel, subject: :sender
175
180
 
176
181
  def as_json_for_dexie
177
182
  super.merge(room_name: room.name)
@@ -9,35 +9,43 @@ module DexieCable
9
9
  # DexieCable channel.
10
10
  #
11
11
  # class Message < ApplicationRecord
12
- # syncs_to_dexie via: -> { UserChannel[sender] }
13
- # syncs_to_dexie via: -> { UserChannel[receiver] }
14
- # syncs_to_dexie via: PublicChannel
12
+ # syncs_to_dexia via: UserChannel, subject: :sender
13
+ # syncs_to_dexia via: UserChannel, subject: "global_feed"
14
+ # syncs_to_dexia via: UserChannel, subject: -> { conversation.users }
15
+ # syncs_to_dexia via: PublicChannel
15
16
  # end
16
17
  #
17
- # @param via [Proc, DexieCable, Array<DexieCable>] Proc evaluated in
18
- # record context, must return a channel (or array of
19
- # channels) that responds to +table+. Skipped if nil.
20
- # @param table [String, Symbol, Proc] Override the Dexie table name
21
- # (defaults to the model's table_name). A Proc is
22
- # evaluated in the record's context.
23
- # @param only [Array<Symbol>] Limit which lifecycle events sync.
24
- # Default: [:create, :update, :destroy].
25
- # @param if [Symbol, Proc] Only sync if the given method or proc
26
- # returns truthy (evaluated in the record's context).
27
- # @param unless [Symbol, Proc] Skip sync if the given method or proc
28
- # returns truthy (evaluated in the record's context).
29
- def syncs_to_dexie(via:, table: nil, only: nil, **options)
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
22
+ # unscoped channel.
23
+ # @param subject [Proc, Symbol, String] A Proc evaluated in record
24
+ # context, a Symbol to call via +send+, or a String
25
+ # used directly as the stream name for +broadcast_to+.
26
+ # Must return a single subject or collection of
27
+ # subjects.
28
+ # @param table [String, Symbol, Proc] Override the Dexie table name
29
+ # (defaults to the model's table_name). A Proc is
30
+ # evaluated in the record's context.
31
+ # @param only [Array<Symbol>] Limit which lifecycle events sync.
32
+ # Default: [:create, :update, :destroy].
33
+ # @param if [Symbol, Proc] Only sync if the given method or proc
34
+ # returns truthy (evaluated in the record's context).
35
+ # @param unless [Symbol, Proc] Skip sync if the given method or proc
36
+ # returns truthy (evaluated in the record's context).
37
+ def syncs_to_dexia(via:, subject: nil, table: nil, only: nil, **options)
30
38
  events = Array(only || %i[create update destroy])
31
39
  conditions = options.slice(:if, :unless)
32
40
 
33
41
  @dexie_sync_configs ||= []
34
- @dexie_sync_configs << { via: via, table: table, only: events, **conditions }
42
+ @dexie_sync_configs << { via: via, subject: subject, table: table, only: events, **conditions }
35
43
 
36
44
  if events.include?(:destroy)
37
45
  before_destroy :dexie_sync_before_destroy
38
46
 
39
47
  after_commit on: :destroy, **conditions do
40
- Array(resolve_channel(via)).each do |channel|
48
+ Array(resolve_channel(via, subject)).each do |channel|
41
49
  next unless channel
42
50
  channel.table(resolve_table(table)).delete(dexie_destroy_id)
43
51
  end
@@ -46,7 +54,7 @@ module DexieCable
46
54
 
47
55
  if events.include?(:create)
48
56
  after_commit on: :create, **conditions do
49
- Array(resolve_channel(via)).each do |channel|
57
+ Array(resolve_channel(via, subject)).each do |channel|
50
58
  next unless channel
51
59
  channel.table(resolve_table(table)).add(as_json_for_dexie)
52
60
  end
@@ -55,7 +63,7 @@ module DexieCable
55
63
 
56
64
  if events.include?(:update)
57
65
  after_commit on: :update, **conditions do
58
- Array(resolve_channel(via)).each do |channel|
66
+ Array(resolve_channel(via, subject)).each do |channel|
59
67
  next unless channel
60
68
 
61
69
  changes = as_json_for_dexie.slice(*saved_changes.keys)
@@ -68,10 +76,20 @@ module DexieCable
68
76
 
69
77
  private
70
78
 
71
- def resolve_channel(via)
72
- case via
73
- when Proc then instance_exec(&via)
74
- else via
79
+ def resolve_channel(via, subject = nil)
80
+ if subject
81
+ subjects = resolve_subject(subject)
82
+ Array(subjects).map { |s| via[s] }
83
+ else
84
+ via
85
+ end
86
+ end
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
75
93
  end
76
94
  end
77
95
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DexieCable
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Buhrmester
@@ -39,7 +39,7 @@ dependencies:
39
39
  version: '7.0'
40
40
  description: DexieCable augments ActionCable channels with a query DSL that mirrors
41
41
  the Dexie.js API, letting you push database mutations from the server to the client
42
- in real time. Includes an ActiveRecord macro (syncs_to_dexie) for automatic change
42
+ in real time. Includes an ActiveRecord macro (syncs_to_dexia) for automatic change
43
43
  syncing.
44
44
  email:
45
45
  - stefan@buhrmi.de