hibiki_rails 0.11.0 → 0.12.0

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: e95a440701dfeb3c02a3f6535b59cc18e51d66569dd76f9d793153f671120edc
4
- data.tar.gz: 14869e53518e44f14089c7a1be15c166a51e9f61ff87867cd9fcbb848b20a3dc
3
+ metadata.gz: 514349fcd04fc813cc4833144b4355c56958e9055dda33aefbdc547517d73bfd
4
+ data.tar.gz: 1a9729ad926d72f9bdfb1b4411544893f59c3ebb40d2dd552165bed1299218e5
5
5
  SHA512:
6
- metadata.gz: 3937c28fbcd8ec47e540a2837b6c48d34b4152553037491a576a67b22679d58578c09c4b63ea316390a1a5d5d49e9c37b29fe277dd8150484a881899ee8f1b00
7
- data.tar.gz: 88ca77fa4732bf3d08f7b207c57ec457bc601afa7111dda3d4660b51d62a1b305b88c9a98b1801cfdc5a29f2723f8d29f5b617b545cd3c9c952e1b3c9aef8329
6
+ metadata.gz: eb8e0207e6b1281f7085e7258a9ed189f836b0427ea3172acf3711b118f80f1a0a8fa97474d5f4ff4deaf69ef0bec8d6083f3b09a3ee1178cb836335453f3d76
7
+ data.tar.gz: 1f4c370144b71c7baea0b3960d69d761b562f6590f281b079f1bf9a26173e7d83c415e262924030e02e4d5bd62876eab7b62c2a12b8a145298ea1d60ff2a5480
data/CHANGELOG.md CHANGED
@@ -4,6 +4,38 @@ The gem and the npm package are released in lockstep and share these version
4
4
  numbers — `app/assets/javascripts/hibiki.js` is a single copy served both ways,
5
5
  so importmap and bundler apps always resolve identical client code.
6
6
 
7
+ ## 0.12.0 — 2026-09-06
8
+
9
+ ### Added
10
+
11
+ **`island` — the ERB block helper for the island root.** One call
12
+ generates the cid, stamps the root with `hibiki_island`, and derives the
13
+ channel's `turbo_stream_from` inside it:
14
+
15
+ ```erb
16
+ <%= island CounterChannel do |cid| %>
17
+ ...
18
+ <% end %>
19
+ ```
20
+
21
+ - `cid:` defaults to a fresh UUID; the block receives it either way.
22
+ - The stream source is derived from the channel class
23
+ (`turbo_stream_from channel.channel_name, cid`), so renaming the channel
24
+ cannot leave a hand-typed streamable behind. `transport: :transmit`
25
+ leaves it out: for a channel that transmits, or one that overrides
26
+ `stream_name` and writes its own line in the block. (`:broadcast` is the
27
+ default; the option is a named value rather than a boolean so it cannot
28
+ be mistaken for Turbo's own `data-turbo="false"`.)
29
+ - `params:` passes through; `tag_name:` picks the root element; any other
30
+ keyword lands on it, with a `data:` hash merged beneath the island's own
31
+ keys.
32
+ - Class-only (`constantize` a dynamic name at the call site) and ERB-only
33
+ (it needs ActionView's `capture`). Phlex components keep
34
+ `div(**hibiki_island(...))`, which remains the primitive.
35
+
36
+ Generators are unchanged: their output keeps the explicit three-line form.
37
+ No client change; the npm release is the lockstep bump.
38
+
7
39
  ## 0.11.0 — 2026-09-05
8
40
 
9
41
  ### Changed
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "cgi/escape"
4
4
  require "json"
5
+ require "securerandom"
5
6
 
6
7
  module Hibiki
7
8
  module Rails
@@ -24,7 +25,8 @@ module Hibiki
24
25
  # Both helpers return a `{ data: { ... } }` hash: splat it into Phlex
25
26
  # element methods or Rails tag helpers (`tag.div(**hibiki_island(...))`).
26
27
  # When the element needs other attributes on the same `data:` key, merge
27
- # the hashes yourself (Phlex's `mix` does this).
28
+ # the hashes yourself (Phlex's `mix` does this). In ERB, #island wraps
29
+ # the root, the cid, and the Turbo stream source into one block helper.
28
30
  #
29
31
  # The emitted attribute names are a private contract between these
30
32
  # helpers and the gem's JS — they version together; don't hand-write
@@ -53,7 +55,12 @@ module Hibiki
53
55
  # the emitted markup instead of being an invisible default.
54
56
  DEFAULT_INPUT_DEBOUNCE = 250
55
57
 
56
- private_constant :VALUE_NAME, :VALUE_TAG, :EVENT_NAME
58
+ # How an island's channel sends HTML back: Turbo broadcasts to a named
59
+ # stream (the root needs a stream source inside), or transmit down the
60
+ # subscription itself (it does not).
61
+ TRANSPORTS = %i[broadcast transmit].freeze
62
+
63
+ private_constant :VALUE_NAME, :VALUE_TAG, :EVENT_NAME, :TRANSPORTS
57
64
 
58
65
  # The shared name validator for both halves of a reactive value (the
59
66
  # view-side data-hibiki-value placeholder and the channel's
@@ -67,6 +74,23 @@ module Hibiki
67
74
  name
68
75
  end
69
76
 
77
+ # A tag name lands in raw markup, so it is allowlisted wherever a
78
+ # helper takes one.
79
+ def self.tag_name(name, of)
80
+ name = name.to_s
81
+ raise ArgumentError, "#{of} tag #{name.inspect} must match #{VALUE_TAG.inspect}" unless VALUE_TAG.match?(name)
82
+
83
+ name
84
+ end
85
+
86
+ # #island's transport option, allowlisted so a typo names itself.
87
+ def self.transport(value)
88
+ return value if TRANSPORTS.include?(value)
89
+
90
+ raise ArgumentError,
91
+ "island transport #{value.inspect} must be one of #{TRANSPORTS.map(&:inspect).join(', ')}"
92
+ end
93
+
70
94
  # The shared validator for both halves of an `event->action` token.
71
95
  def self.event_name(name)
72
96
  name = name.to_s
@@ -105,6 +129,33 @@ module Hibiki
105
129
  { data: }
106
130
  end
107
131
 
132
+ # The ERB spelling of the island root: a fresh cid, the #hibiki_island
133
+ # root, and the channel's own `turbo_stream_from channel_name, cid`
134
+ # inside it, in one block. The block receives the cid.
135
+ #
136
+ # <%= island CounterChannel do |cid| %>
137
+ # ...
138
+ # <% end %>
139
+ #
140
+ # `transport: :transmit` leaves the stream source out — for a channel
141
+ # that transmits, or one that overrides #stream_name and writes its own
142
+ # turbo_stream_from inside the block. Other keywords land on the root
143
+ # element; a `data:` hash is merged beneath the island's own keys.
144
+ # Class-only: a dynamic name is `"#{kind.camelize}Channel".constantize`
145
+ # at the call site, never taken from a request param. Phlex components
146
+ # keep `div(**hibiki_island(...))`.
147
+ def island(channel, cid: nil, params: nil, transport: :broadcast, tag_name: :div, **attributes,
148
+ &block)
149
+ island_guards!(channel, block)
150
+ tag_name = Helpers.tag_name(tag_name, "island")
151
+ transport = Helpers.transport(transport)
152
+ cid ||= SecureRandom.uuid
153
+ attributes[:data] = attributes[:data].to_h.merge(hibiki_island(channel, cid:, params:)[:data])
154
+ content = capture(cid, &block)
155
+ content = island_stream(channel, cid, content) if transport == :broadcast
156
+ tag.public_send(tag_name, content, **attributes)
157
+ end
158
+
108
159
  # Forward an event on this element as a channel action.
109
160
  #
110
161
  # `event:` names the event, or a list of them — the left side of the
@@ -169,11 +220,7 @@ module Hibiki
169
220
  # channels. Only the placeholder text is server-rendered: each site
170
221
  # keeps its own tag, classes, and attributes across updates.
171
222
  def reactive(name, placeholder = "", tag_name: :span)
172
- tag_name = tag_name.to_s
173
- unless VALUE_TAG.match?(tag_name)
174
- raise ArgumentError,
175
- "reactive value tag #{tag_name.inspect} must match #{VALUE_TAG.inspect}"
176
- end
223
+ tag_name = Helpers.tag_name(tag_name, "reactive value")
177
224
  html = %(<#{tag_name} data-hibiki-value="#{Helpers.value_name(name)}">) +
178
225
  %(#{CGI.escapeHTML(placeholder.to_s)}</#{tag_name}>)
179
226
  html.respond_to?(:html_safe) ? html.html_safe : html
@@ -185,6 +232,25 @@ module Hibiki
185
232
 
186
233
  private
187
234
 
235
+ # #island's preconditions: a block, an ActionView receiver, a channel
236
+ # class.
237
+ def island_guards!(channel, block)
238
+ raise ArgumentError, "island needs a block" unless block
239
+ unless respond_to?(:output_buffer) && respond_to?(:tag)
240
+ raise ArgumentError, "island is ERB-only; use div(**hibiki_island(...)) in a Phlex component"
241
+ end
242
+ return if channel.is_a?(Class) && channel.respond_to?(:channel_name)
243
+
244
+ raise ArgumentError,
245
+ "island takes a channel class, got #{channel.inspect}; " \
246
+ "constantize a dynamic name at the call site"
247
+ end
248
+
249
+ # The broadcast transport's stream source, ahead of the block's content.
250
+ def island_stream(channel, cid, content)
251
+ safe_join([turbo_stream_from(channel.channel_name, cid), content])
252
+ end
253
+
188
254
  # #on's per-control modifiers, kept out of the token grammar. Each is
189
255
  # omitted when it matches the client's own default, so the common call
190
256
  # still stamps exactly one attribute.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Hibiki
4
4
  module Rails
5
- VERSION = "0.11.0"
5
+ VERSION = "0.12.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hibiki_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - planetaska