Module: LiveComponent::Base::ClassMethods
- Defined in:
- lib/live_component/base.rb
Instance Method Summary collapse
- #__lc_compile_if_necessary! ⇒ Object
- #__lc_controller ⇒ Object
- #__lc_init_args ⇒ Object
- #__lc_js_sidecar_files ⇒ Object
- #__lc_slot_mod ⇒ Object
-
#__vc_initialize_parameters ⇒ Object
For collections.
- #deserialize_props(props) ⇒ Object
- #prop_serializers ⇒ Object
- #serialize_props(props) ⇒ Object
- #serializes(prop_name, with: nil, **serializer_options, &block) ⇒ Object
Instance Method Details
#__lc_compile_if_necessary! ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/live_component/base.rb', line 24 def __lc_compile_if_necessary! return if @__lc_compiled if registered_slots.empty? @__lc_compiled = true return end registered_slots.each do |slot_name, slot_config| is_collection = slot_type(slot_name) == :collection singular_name = is_collection ? ActiveSupport::Inflector.singularize(slot_name) : slot_name __lc_slot_mod.class_eval " def with_\#{singular_name}(**props, &block)\n new_slot_data = {}\n new_slot_data[:props] = props unless props.empty?\n\n @__lc[:slots] ||= {}\n @__lc[:slots][\#{singular_name.inspect}] ||= []\n @__lc[:slots][\#{singular_name.inspect}] << new_slot_data\n\n singular_name = \#{singular_name.inspect}\n slot_def = registered_slots[\#{slot_name.inspect}]\n\n UseContext.provide_context(:__lc_context, { slot_name: singular_name, slot_def: slot_def }) do\n if block\n super(**props) do |**block_props|\n slot = block.call(**block_props)\n\n if slot.is_a?(ViewComponent::Slot)\n content = slot.instance_variable_get(:@__vc_content)\n new_slot_data[:content] = Utils.normalize_html_whitespace(content) if content\n end\n\n if (instance = slot.instance_variable_get(:@__vc_component_instance))\n if instance.respond_to?(:__lc_attributes)\n new_slot_data[:props][:__lc_attributes] ||= {}\n new_slot_data[:props][:__lc_attributes][\"data-id\"] = instance.__lc_attributes[\"data-id\"]\n end\n end\n\n slot\n end\n else\n super\n end\n end\n end\n RUBY\n end\n\n unless self < __lc_slot_mod\n prepend(__lc_slot_mod)\n end\n\n @__lc_compiled = true\nend\n", __FILE__, __LINE__ + 1 |
#__lc_controller ⇒ Object
91 92 93 94 95 |
# File 'lib/live_component/base.rb', line 91 def __lc_controller # If there are any sidecar js files, assume one of them defines a controller # named after the Ruby class. Otherwise, use the default LiveController. @__lc_controller ||= __lc_js_sidecar_files.empty? ? "live" : self.name.dasherize.downcase.gsub("::", "-") end |
#__lc_init_args ⇒ Object
20 21 22 |
# File 'lib/live_component/base.rb', line 20 def __lc_init_args @__lc_init_args ||= instance_method(:initialize).super_method.parameters end |
#__lc_js_sidecar_files ⇒ Object
16 17 18 |
# File 'lib/live_component/base.rb', line 16 def __lc_js_sidecar_files @__lc_js_sidecar_files ||= sidecar_files(JS_SIDECAR_EXTENSIONS) end |
#__lc_slot_mod ⇒ Object
82 83 84 |
# File 'lib/live_component/base.rb', line 82 def __lc_slot_mod @__lc_slot_mod ||= Module.new end |
#__vc_initialize_parameters ⇒ Object
For collections
87 88 89 |
# File 'lib/live_component/base.rb', line 87 def __vc_initialize_parameters @__vc_initialize_parameters ||= instance_method(:initialize).super_method.parameters end |
#deserialize_props(props) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/live_component/base.rb', line 136 def deserialize_props(props) {}.tap do |deserialized_props| props.each_pair do |k, v| k = k.to_sym serializer = prop_serializers[k] ||= LiveComponent.serializer deserialized_props[k] = serializer.deserialize(v) end end end |
#prop_serializers ⇒ Object
122 123 124 |
# File 'lib/live_component/base.rb', line 122 def prop_serializers @prop_serializers ||= {} end |
#serialize_props(props) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/live_component/base.rb', line 126 def serialize_props(props) {}.tap do |serialized_props| props.each_pair do |k, v| k = k.to_sym serializer = prop_serializers[k] ||= LiveComponent.serializer serialized_props[k] = serializer.serialize(v) end end end |
#serializes(prop_name, with: nil, **serializer_options, &block) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/live_component/base.rb', line 97 def serializes(prop_name, with: nil, **, &block) if block && with raise "Expected `#{__method__}' to be called with a block or the with: parameter, but both were provided" end if block builder = InlineSerializer::Builder.new block.call(builder, **) prop_serializers[prop_name] = builder.to_serializer return end unless with raise "Expected `#{__method__}' to be called with a block or the with: parameter" end unless LiveComponent.registered_prop_serializers.include?(with) raise "Could not find a serializer with the name '#{with}' - is it registered?" end prop_serializers[prop_name] = LiveComponent.registered_prop_serializers[with].make(**) nil end |