dexiecable 0.1.1 → 0.1.3

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: 353c08fde431ac528532ebc11a99911154654988345d6c7fdddb479269a65194
4
- data.tar.gz: ec8e1ab7b1d4f0ea74cc8da8346337cee2b9f82192de966ccc653aedd5f96a38
3
+ metadata.gz: 4ad9a4f94d6491f661821d79ac6a8fa3c36527927f8fb0916eee26c87294ebdf
4
+ data.tar.gz: d50524564c23e50423e186edd1ed5883d9a4e1575fc687d9d80036fcac6201ec
5
5
  SHA512:
6
- metadata.gz: c1fabe1bc7d3aa4f16855ffc9f3d1dff472c0488b8b2959745b02225bd5503c3b9e43ea2cc253bc306c742633b5e85da4400b760072d5776f2e925d66cac0529
7
- data.tar.gz: 0d3c51e4d95f3848c244a0793cd77c2de817646ebef18c79705a42d2cfb4f5e729bbbbf8de466b1cc9f04e79d3f392a1c096f8968746a0c49077b00dee44d502
6
+ metadata.gz: 017f3a4b81fe35bbc8fc17ad5b22cd8a0400ce3e4a14aa20942a517f8030c1cbf08c67b1e590b090726f20dd67c5b71869319fa568b07e01ebdc320867a829be
7
+ data.tar.gz: 9ccd364a0a6a6b1960bcd5c9af17475b5e972dbfc2ee839bb86d9cb3bf69fedfeb3ccbc20b471e9d6dfe42f9e458f59f32cdd158d1f2c5f6f40f1b022e41f9d8
data/README.md CHANGED
@@ -100,18 +100,20 @@ Any Dexie.js write operation triggers an immediate broadcast:
100
100
  # Single insert
101
101
  UserChannel[current_user].table("messages").add(id: 1, text: "hello")
102
102
 
103
- # Filtered bulk insert
104
- UserChannel[current_user]
105
- .table("messages")
106
- .where(:room_id).equals(room.id)
107
- .bulkAdd(messages)
103
+ # Bulk insert
104
+ UserChannel[current_user].table("messages").bulkAdd(messages)
108
105
 
109
- # Update
106
+ # Update (using modify)
110
107
  UserChannel[current_user]
111
108
  .table("messages")
112
109
  .where(:id).equals(msg.id)
113
110
  .modify(read: true)
114
111
 
112
+ # Update (using update)
113
+ UserChannel[current_user]
114
+ .table("messages")
115
+ .update(msg.id, text: "updated text")
116
+
115
117
  # Delete
116
118
  UserChannel[current_user]
117
119
  .table("messages")
@@ -127,9 +129,11 @@ Add to any ActiveRecord model:
127
129
 
128
130
  ```ruby
129
131
  class Message < ApplicationRecord
132
+ # Sync to a single user.
130
133
  syncs_to_dexie via: -> { UserChannel[sender] }
131
- syncs_to_dexie via: -> { UserChannel[receiver] }
132
- syncs_to_dexie via: PublicChannel # broadcast to all connected clients
134
+
135
+ # Sync to all participants of a conversation.
136
+ syncs_to_dexie via: -> { conversation.participants.map { |u| UserChannel[u] } }
133
137
  end
134
138
  ```
135
139
 
@@ -143,9 +147,23 @@ end
143
147
 
144
148
  | Option | Default | Description |
145
149
  |---|---|---|
146
- | `via:` | *(required)* | Proc (evaluated in record context), channel class, or channel instance |
150
+ | `via:` | *(required)* | Proc (evaluated in record context), a channel, or an array of channels |
147
151
  | `table:` | model's `table_name` | Override the Dexie table name. A Proc is evaluated in the record's context. |
148
152
  | `only:` | `[:create, :update, :destroy]` | Limit which events trigger a sync |
153
+ | `if:` | *(none)* | Symbol (method name) or Proc — only sync when it returns truthy |
154
+ | `unless:` | *(none)* | Symbol (method name) or Proc — skip sync when it returns truthy |
155
+
156
+ You can combine multiple `syncs_to_dexie` declarations, each with different conditions:
157
+
158
+ ```ruby
159
+ class Message < ApplicationRecord
160
+ syncs_to_dexie via: -> { UserChannel[sender] },
161
+ if: :published?
162
+
163
+ syncs_to_dexie via: -> { AdminChannel },
164
+ unless: -> { draft? }
165
+ end
166
+ ```
149
167
 
150
168
  #### Customizing the synced payload
151
169
 
@@ -14,45 +14,53 @@ module DexieCable
14
14
  # syncs_to_dexie via: PublicChannel
15
15
  # end
16
16
  #
17
- # @param via [Proc, DexieCable] Proc evaluated in record context,
18
- # must return a channel (responds to +table+). A channel
19
- # class/instance can also be passed directly. Skipped if nil.
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
20
  # @param table [String, Symbol, Proc] Override the Dexie table name
21
21
  # (defaults to the model's table_name). A Proc is
22
22
  # evaluated in the record's context.
23
23
  # @param only [Array<Symbol>] Limit which lifecycle events sync.
24
24
  # Default: [:create, :update, :destroy].
25
- def syncs_to_dexie(via:, table: nil, only: nil)
26
- events = Array(only || %i[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)
30
+ events = Array(only || %i[create update destroy])
31
+ conditions = options.slice(:if, :unless)
27
32
 
28
33
  @dexie_sync_configs ||= []
29
- @dexie_sync_configs << { via: via, table: table, only: events }
34
+ @dexie_sync_configs << { via: via, table: table, only: events, **conditions }
30
35
 
31
36
  if events.include?(:destroy)
32
37
  before_destroy :dexie_sync_before_destroy
33
38
 
34
- after_commit on: :destroy do
35
- channel = resolve_channel(via)
36
- next unless channel
37
- channel.table(resolve_table(table)).delete(dexie_destroy_id)
39
+ after_commit on: :destroy, **conditions do
40
+ Array(resolve_channel(via)).each do |channel|
41
+ next unless channel
42
+ channel.table(resolve_table(table)).delete(dexie_destroy_id)
43
+ end
38
44
  end
39
45
  end
40
46
 
41
47
  if events.include?(:create)
42
- after_commit on: :create do
43
- channel = resolve_channel(via)
44
- next unless channel
45
- channel.table(resolve_table(table)).add(as_json_for_dexie)
48
+ after_commit on: :create, **conditions do
49
+ Array(resolve_channel(via)).each do |channel|
50
+ next unless channel
51
+ channel.table(resolve_table(table)).add(as_json_for_dexie)
52
+ end
46
53
  end
47
54
  end
48
55
 
49
56
  if events.include?(:update)
50
- after_commit on: :update do
51
- channel = resolve_channel(via)
52
- next unless channel
57
+ after_commit on: :update, **conditions do
58
+ Array(resolve_channel(via)).each do |channel|
59
+ next unless channel
53
60
 
54
- changes = as_json_for_dexie.slice(*saved_changes.keys)
55
- channel.table(resolve_table(table)).update(id, changes)
61
+ changes = as_json_for_dexie.slice(*saved_changes.keys)
62
+ channel.table(resolve_table(table)).update(id, changes)
63
+ end
56
64
  end
57
65
  end
58
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DexieCable
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
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.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Buhrmester