live_component 0.1.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.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +2 -0
  3. data/Gemfile +1 -0
  4. data/LICENSE +21 -0
  5. data/Rakefile +41 -0
  6. data/app/channels/live_component_channel.rb +23 -0
  7. data/app/components/live_component/render_component.rb +43 -0
  8. data/app/controllers/live_component/render_controller.rb +9 -0
  9. data/app/helpers/live_component/application_helper.rb +40 -0
  10. data/app/views/live_component/render/show.html.erb +1 -0
  11. data/config/routes.rb +5 -0
  12. data/ext/view_component_patch.rb +22 -0
  13. data/lib/live_component/action.rb +31 -0
  14. data/lib/live_component/base.rb +267 -0
  15. data/lib/live_component/big_decimal_serializer.rb +17 -0
  16. data/lib/live_component/component.html.erb +4 -0
  17. data/lib/live_component/controller_methods.rb +9 -0
  18. data/lib/live_component/date_serializer.rb +15 -0
  19. data/lib/live_component/date_time_serializer.rb +11 -0
  20. data/lib/live_component/duration_serializer.rb +18 -0
  21. data/lib/live_component/engine.rb +21 -0
  22. data/lib/live_component/inline_serializer.rb +34 -0
  23. data/lib/live_component/middleware.rb +25 -0
  24. data/lib/live_component/model_serializer.rb +65 -0
  25. data/lib/live_component/module_serializer.rb +15 -0
  26. data/lib/live_component/object_serializer.rb +29 -0
  27. data/lib/live_component/range_serializer.rb +19 -0
  28. data/lib/live_component/react.rb +24 -0
  29. data/lib/live_component/record_proxy.rb +85 -0
  30. data/lib/live_component/safe_dispatcher.rb +64 -0
  31. data/lib/live_component/serializer.rb +202 -0
  32. data/lib/live_component/state.rb +91 -0
  33. data/lib/live_component/tag_builder.rb +20 -0
  34. data/lib/live_component/target.rb +26 -0
  35. data/lib/live_component/time_object_serializer.rb +13 -0
  36. data/lib/live_component/time_serializer.rb +11 -0
  37. data/lib/live_component/time_with_zone_serializer.rb +20 -0
  38. data/lib/live_component/utils.rb +139 -0
  39. data/lib/live_component/version.rb +5 -0
  40. data/lib/live_component.rb +55 -0
  41. data/lib/tasks/test.rake +1 -0
  42. data/live_component.gemspec +21 -0
  43. metadata +136 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a94ea37093288d3f0b8a8c8b843edbaecb90432df85e5f0ad8616e57e0ec6ee2
4
+ data.tar.gz: 062fdc7ae9925cfdeb34f2ed58b51fa553544eb75d88c2001244e4df1702eea7
5
+ SHA512:
6
+ metadata.gz: 5973e55e49860be0d89d62e707b8b23e07b5fc3632ab8d5525b2be29a119fa74ace9e5981381ffa6a81a390bb2832c31c7c48f73b1cc1232b9e3d49a09b0609e
7
+ data.tar.gz: dabe8e9473069e34225cfacae0ffe1d663d77f0fc7c4a8a0d9989e327561090dd2943a2b0f8869ffd004b9337d9fe874dd32851d969ff75603be9922d9ba6888
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ # 1.0.0
2
+ * Birthday!
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ eval_gemfile "test/dummy/Gemfile"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Cameron Dutro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be included in the set of available tasks.
3
+
4
+ ENV["RAILS_ENV"] ||= "test"
5
+
6
+ require "rubygems/package_task"
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require_relative "test/dummy/config/application"
10
+
11
+ # Load tasks but skip the test unit tasks to avoid the "rails new" help output
12
+ Rails.application.load_tasks
13
+
14
+ # Override the Rails test:system task to use our custom implementation
15
+ Rake::Task["test:system"].clear if Rake::Task.task_defined?("test:system")
16
+ Rake::Task["test"].clear if Rake::Task.task_defined?("test")
17
+
18
+ task default: :test
19
+
20
+ namespace :test do
21
+ task :setup do
22
+ $:.push(File.join(__dir__, "test"))
23
+
24
+ require "test_helper"
25
+ require "minitest/autorun"
26
+ end
27
+
28
+ desc "Run unit tests"
29
+ task unit: :setup do
30
+ # Run all system tests
31
+ Dir.glob("test/unit/**/*_test.rb").each { |file| require_relative file }
32
+ end
33
+
34
+ desc "Run system tests"
35
+ task system: :setup do
36
+ # Run all system tests
37
+ Dir.glob("test/system/**/*_test.rb").each { |file| require_relative file }
38
+ end
39
+ end
40
+
41
+ task test: ["test:unit", "test:system"]
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ class LiveComponentChannel < ActionCable::Channel::Base
6
+ def subscribed
7
+ stream_from "live_component"
8
+ end
9
+
10
+ def receive(data)
11
+ request_id = data["request_id"]
12
+ payload = JSON.parse(data["payload"])
13
+
14
+ result = LiveComponent::RenderController.renderer.render(
15
+ :show, assigns: { state: payload["state"], reflexes: payload["reflexes"] }, layout: false
16
+ )
17
+
18
+ ActionCable.server.broadcast(
19
+ "live_component",
20
+ { payload: result, request_id: request_id }
21
+ )
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ class RenderComponent < ViewComponent::Base
5
+ def initialize(state, reflexes, prop_overrides = {})
6
+ @state = LiveComponent::State.build(state, prop_overrides)
7
+ @reflexes = reflexes
8
+ end
9
+
10
+ def render_in(view_context, &block)
11
+ component = @state.klass.new(**@state.props.symbolize_keys)
12
+
13
+ @reflexes.each do |reflex|
14
+ method_name = reflex["method_name"] || reflex[:method_name]
15
+
16
+ props = (reflex["props"] || reflex[:props] || {}).each_with_object({}) do |(k, v), memo|
17
+ memo[k.to_sym] = LiveComponent.serializer.deserialize(v)
18
+ end
19
+
20
+ SafeDispatcher.send_safely(component, method_name, **props)
21
+ end
22
+
23
+ component.render_in(view_context) do
24
+ apply_slots(component, @state.slots)
25
+ block.call(component) if block
26
+ @state.content
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def apply_slots(component_instance, slots)
33
+ slots.each do |slot_method, slot_defs|
34
+ slot_defs.each do |slot_def|
35
+ component_instance.send("with_#{slot_method}", **slot_def.props) do |slot_instance|
36
+ apply_slots(slot_instance, slot_def.slots)
37
+ slot_def.content
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ class RenderController < ActionController::Base
5
+ def show
6
+ render layout: false, formats: [:html]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ module ApplicationHelper
5
+ def live
6
+ @__lc_tag_builder ||= LiveComponent::TagBuilder.new(self)
7
+ end
8
+
9
+ def form_with(rerender: nil, html: {}, **options, &block)
10
+ if (params = Utils.html_params_for_rerender(rerender))
11
+ html.merge!(params)
12
+ end
13
+
14
+ options = ::LiveComponent::Utils.translate_all_attrs(options)
15
+
16
+ super(**options, html: html, &block)
17
+ end
18
+
19
+ def button_to(*args, rerender: nil, form: {}, **options, &block)
20
+ if (params = Utils.html_params_for_rerender(rerender))
21
+ form.merge!(params)
22
+ end
23
+
24
+ options = ::LiveComponent::Utils.translate_all_attrs(options)
25
+
26
+ super(*args, **options, form: form, &block)
27
+ end
28
+
29
+ def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
30
+ if block_given?
31
+ options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
32
+ end
33
+
34
+ options ||= {}
35
+ options = ::LiveComponent::Utils.translate_all_attrs(options)
36
+
37
+ super
38
+ end
39
+ end
40
+ end
@@ -0,0 +1 @@
1
+ <%= render(LiveComponent::RenderComponent.new(@state, @reflexes)) %>
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ LiveComponent::Engine.routes.draw do
4
+ post "/render", to: "render#show", as: "render"
5
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "view_component"
4
+
5
+ module LiveComponent
6
+ module ViewComponentPatch
7
+ def self.included(base)
8
+ base.singleton_class.prepend(ClassMethodOverrides)
9
+ end
10
+
11
+ module ClassMethodOverrides
12
+ def new(*args, **kwargs, &block)
13
+ return super unless kwargs[:actions] || kwargs[:targets]
14
+
15
+ kwargs = ::LiveComponent::Utils.translate_attrs(Action, kwargs) if kwargs[:actions]
16
+ kwargs = ::LiveComponent::Utils.translate_attrs(Target, kwargs) if kwargs[:targets]
17
+
18
+ super
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ class Action
5
+ def initialize(controller_name, event_name)
6
+ @controller_name = controller_name
7
+ @event_name = event_name
8
+ end
9
+
10
+ def call(method_name)
11
+ @method_name = method_name
12
+ self
13
+ end
14
+
15
+ def self.attr_name
16
+ :actions
17
+ end
18
+
19
+ def attr_name
20
+ self.class.attr_name
21
+ end
22
+
23
+ def to_attributes
24
+ { data: {} }.tap do |attrs|
25
+ if @method_name
26
+ attrs[:data][:action] = "#{@event_name}->#{@controller_name}##{@method_name}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,267 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "use_context"
4
+
5
+ module LiveComponent
6
+ module Base
7
+ JS_SIDECAR_EXTENSIONS = %w(js ts jsx tsx).freeze
8
+
9
+ def self.included(base)
10
+ base.prepend(Overrides)
11
+ base.include(InstanceMethods)
12
+ base.extend(ClassMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+ def __lc_js_sidecar_files
17
+ @__lc_js_sidecar_files ||= sidecar_files(JS_SIDECAR_EXTENSIONS)
18
+ end
19
+
20
+ def __lc_init_args
21
+ @__lc_init_args ||= instance_method(:initialize).super_method.parameters
22
+ end
23
+
24
+ def __lc_compile_if_necessary!
25
+ return if @__lc_compiled
26
+
27
+ if registered_slots.empty?
28
+ @__lc_compiled = true
29
+ return
30
+ end
31
+
32
+ registered_slots.each do |slot_name, slot_config|
33
+ is_collection = slot_type(slot_name) == :collection
34
+ singular_name = is_collection ? ActiveSupport::Inflector.singularize(slot_name) : slot_name
35
+
36
+ __lc_slot_mod.class_eval <<~RUBY, __FILE__, __LINE__ + 1
37
+ def with_#{singular_name}(**props, &block)
38
+ new_slot_data = {}
39
+ new_slot_data[:props] = props unless props.empty?
40
+
41
+ @__lc[:slots] ||= {}
42
+ @__lc[:slots][#{singular_name.inspect}] ||= []
43
+ @__lc[:slots][#{singular_name.inspect}] << new_slot_data
44
+
45
+ singular_name = #{singular_name.inspect}
46
+ slot_def = registered_slots[#{slot_name.inspect}]
47
+
48
+ UseContext.provide_context(:__lc_context, { slot_name: singular_name, slot_def: slot_def }) do
49
+ if block
50
+ super(**props) do |**block_props|
51
+ slot = block.call(**block_props)
52
+
53
+ if slot.is_a?(ViewComponent::Slot)
54
+ content = slot.instance_variable_get(:@__vc_content)
55
+ new_slot_data[:content] = Utils.normalize_html_whitespace(content) if content
56
+ end
57
+
58
+ if (instance = slot.instance_variable_get(:@__vc_component_instance))
59
+ if instance.respond_to?(:__lc_attributes)
60
+ new_slot_data[:props][:__lc_attributes] ||= {}
61
+ new_slot_data[:props][:__lc_attributes]["data-id"] = instance.__lc_attributes["data-id"]
62
+ end
63
+ end
64
+
65
+ slot
66
+ end
67
+ else
68
+ super
69
+ end
70
+ end
71
+ end
72
+ RUBY
73
+ end
74
+
75
+ unless self < __lc_slot_mod
76
+ prepend(__lc_slot_mod)
77
+ end
78
+
79
+ @__lc_compiled = true
80
+ end
81
+
82
+ def __lc_slot_mod
83
+ @__lc_slot_mod ||= Module.new
84
+ end
85
+
86
+ # For collections
87
+ def __vc_initialize_parameters
88
+ @__vc_initialize_parameters ||= instance_method(:initialize).super_method.parameters
89
+ end
90
+
91
+ def __lc_controller
92
+ # If there are any sidecar js files, assume one of them defines a controller
93
+ # named after the Ruby class. Otherwise, use the default LiveController.
94
+ @__lc_controller ||= __lc_js_sidecar_files.empty? ? "live" : self.name.dasherize.downcase.gsub("::", "-")
95
+ end
96
+
97
+ def serializes(prop_name, with: nil, **serializer_options, &block)
98
+ if block && with
99
+ raise "Expected `#{__method__}' to be called with a block or the with: parameter, but both were provided"
100
+ end
101
+
102
+ if block
103
+ builder = InlineSerializer::Builder.new
104
+ block.call(builder, **serializer_options)
105
+ prop_serializers[prop_name] = builder.to_serializer
106
+ return
107
+ end
108
+
109
+ unless with
110
+ raise "Expected `#{__method__}' to be called with a block or the with: parameter"
111
+ end
112
+
113
+ unless LiveComponent.registered_prop_serializers.include?(with)
114
+ raise "Could not find a serializer with the name '#{with}' - is it registered?"
115
+ end
116
+
117
+ prop_serializers[prop_name] = LiveComponent.registered_prop_serializers[with].make(**serializer_options)
118
+
119
+ nil
120
+ end
121
+
122
+ def prop_serializers
123
+ @prop_serializers ||= {}
124
+ end
125
+
126
+ def serialize_props(props)
127
+ {}.tap do |serialized_props|
128
+ props.each_pair do |k, v|
129
+ k = k.to_sym
130
+ serializer = prop_serializers[k] ||= LiveComponent.serializer
131
+ serialized_props[k] = serializer.serialize(v)
132
+ end
133
+ end
134
+ end
135
+
136
+ def deserialize_props(props)
137
+ {}.tap do |deserialized_props|
138
+ props.each_pair do |k, v|
139
+ k = k.to_sym
140
+ serializer = prop_serializers[k] ||= LiveComponent.serializer
141
+ deserialized_props[k] = serializer.deserialize(v)
142
+ end
143
+ end
144
+ end
145
+ end
146
+
147
+ module InstanceMethods
148
+ attr_reader :__lc_attributes
149
+
150
+ def __lc_id
151
+ @__lc_id ||= @__lc_attributes["data-id"] || SecureRandom.uuid
152
+ end
153
+
154
+ def fn(method_name)
155
+ "fn:#{__lc_id}##{method_name}"
156
+ end
157
+
158
+ def on(event_name)
159
+ Action.new(__lc_controller, event_name)
160
+ end
161
+
162
+ def target(target_name)
163
+ Target.new(__lc_controller, target_name)
164
+ end
165
+
166
+ private
167
+
168
+ def __lc_tag_name
169
+ @__lc_tag_name ||= if self.class.__lc_js_sidecar_files.empty?
170
+ "live-component"
171
+ else
172
+ self.class.name.gsub("::", "-").downcase.yield_self do |name|
173
+ if name.split("-").size == 1
174
+ "lc-#{name}" # custom element names have to be more than one word
175
+ else
176
+ name
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ def __lc_controller
183
+ self.class.__lc_controller
184
+ end
185
+ end
186
+
187
+ module Overrides
188
+ def initialize(__lc_attributes: {}, **props, &block)
189
+ @__lc = { ruby_class: self.class.name }
190
+ @__lc_attributes = __lc_attributes
191
+
192
+ UseContext.use_context(:__lc_context, :slot_name) do |slot_name|
193
+ @__lc_slot_name = slot_name if slot_name
194
+ end
195
+
196
+ self.class.__lc_compile_if_necessary!
197
+
198
+ super(**props, &block)
199
+ end
200
+
201
+ def render_in(view_context, &block)
202
+ props = {
203
+ __lc_attributes: @__lc_attributes.merge(
204
+ "data-id" => __lc_id
205
+ )
206
+ }
207
+
208
+ current_state = State.new(
209
+ klass: self.class,
210
+ props: props,
211
+ slots: @__lc[:slots] || {},
212
+ children: @__lc[:children] || {},
213
+ )
214
+
215
+ result = UseContext.use_context(:__lc_context, :state) do |parent_state|
216
+ if !parent_state
217
+ current_state.root!
218
+ end
219
+
220
+ if parent_state
221
+ if @__lc_slot_name
222
+ parent_state.slots[@__lc_slot_name] ||= []
223
+ parent_state.slots[@__lc_slot_name] << current_state
224
+ else
225
+ parent_state.children[__lc_id] = current_state
226
+ end
227
+ end
228
+
229
+ UseContext.provide_context(:__lc_context, { state: current_state }) do
230
+ super
231
+ end
232
+ end
233
+
234
+ self.class.__lc_init_args.each do |(type, name)|
235
+ if type == :key || type == :keyreq
236
+ props[name] = instance_variable_get(:"@#{name}")
237
+ elsif type == :keyrest
238
+ props.merge!(instance_variable_get(:"@#{name}"))
239
+ end
240
+ end
241
+
242
+ if @__vc_content
243
+ @__lc[:content] = Utils.normalize_html_whitespace(@__vc_content)
244
+ current_state.content = @__lc[:content]
245
+ end
246
+
247
+ attributes = {
248
+ "data-id" => __lc_id,
249
+ "data-controller" => __lc_controller,
250
+ "data-livecomponent" => "true",
251
+ "data-component" => self.class.name,
252
+ **@__lc_attributes,
253
+ }
254
+
255
+ if current_state.root?
256
+ attributes["data-state"] = current_state.to_json
257
+ end
258
+
259
+ if @__lc_slot_name
260
+ attributes["data-slot-name"] = @__lc_slot_name
261
+ end
262
+
263
+ content_tag(__lc_tag_name, result, **attributes)
264
+ end
265
+ end
266
+ end
267
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+
5
+ module LiveComponent
6
+ class BigDecimalSerializer < ObjectSerializer
7
+ private
8
+
9
+ def object_to_hash(big_decimal)
10
+ { "value" => big_decimal.to_s }
11
+ end
12
+
13
+ def hash_to_object(hash)
14
+ BigDecimal(hash["value"])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ <%= render(component_klass.new(**@component_def.args)) do |component| %>
2
+ <% apply_slots(component, @component_def.slots) %>
3
+ <%= @component_def.content %>
4
+ <% end %>
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ module ControllerMethods
5
+ def live
6
+ helpers.live
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ class DateSerializer < ObjectSerializer
5
+ private
6
+
7
+ def object_to_hash(date)
8
+ { "value" => date.iso8601 }
9
+ end
10
+
11
+ def hash_to_object(hash)
12
+ Date.iso8601(hash["value"])
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ class DateTimeSerializer < TimeObjectSerializer
5
+ private
6
+
7
+ def hash_to_object(hash)
8
+ DateTime.iso8601(hash["value"])
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ class DurationSerializer < ObjectSerializer
5
+ private
6
+
7
+ def object_to_hash(duration)
8
+ { "value" => duration.value, "parts" => LiveComponent.serializer.serialize(duration.parts.to_a) }
9
+ end
10
+
11
+ def hash_to_object(hash)
12
+ value = hash["value"]
13
+ parts = LiveComponent.serializer.deserialize(hash["parts"].to_h)
14
+ # `parts` is originally a hash, but will have been flattened to an array by serializer.deserialize
15
+ ActiveSupport::Duration.new(value, parts.to_h)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require "live_component"
2
+
3
+ module LiveComponent
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace LiveComponent
6
+
7
+ initializer "live-component.include_helpers" do
8
+ ActiveSupport.on_load(:action_controller) do
9
+ helper LiveComponent::ApplicationHelper
10
+ include LiveComponent::ControllerMethods
11
+ end
12
+ end
13
+
14
+ initializer "live-component.patch_vc" do
15
+ ActiveSupport.on_load(:view_component) do
16
+ include LiveComponent::ViewComponentPatch
17
+ include LiveComponent::ApplicationHelper
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LiveComponent
4
+ class InlineSerializer
5
+ class Builder
6
+ def serialize(&block)
7
+ @serializer_proc = block
8
+ self
9
+ end
10
+
11
+ def deserialize(&block)
12
+ @deserializer_proc = block
13
+ self
14
+ end
15
+
16
+ def to_serializer
17
+ InlineSerializer.new(@serializer_proc, @deserializer_proc)
18
+ end
19
+ end
20
+
21
+ def initialize(serializer_proc, deserializer_proc)
22
+ @serializer_proc = serializer_proc
23
+ @deserializer_proc = deserializer_proc
24
+ end
25
+
26
+ def serialize(object)
27
+ @serializer_proc ? @serializer_proc.call(object) : object
28
+ end
29
+
30
+ def deserialize(hash)
31
+ @deserializer_proc ? @deserializer_proc.call(hash) : hash
32
+ end
33
+ end
34
+ end