Class: LiveComponent::State
- Inherits:
-
Object
- Object
- LiveComponent::State
- Defined in:
- lib/live_component/state.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#content ⇒ Object
Returns the value of attribute content.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
-
#root ⇒ Object
(also: #root?)
readonly
Returns the value of attribute root.
-
#slots ⇒ Object
readonly
Returns the value of attribute slots.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(root: false, klass: nil, props: {}, slots: {}, children: {}, content: nil) ⇒ State
constructor
A new instance of State.
- #root! ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(root: false, klass: nil, props: {}, slots: {}, children: {}, content: nil) ⇒ State
Returns a new instance of State.
56 57 58 59 60 61 62 63 |
# File 'lib/live_component/state.rb', line 56 def initialize(root: false, klass: nil, props: {}, slots: {}, children: {}, content: nil) @root = root @klass = klass @props = props @slots = slots @children = children @content = content end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
51 52 53 |
# File 'lib/live_component/state.rb', line 51 def children @children end |
#content ⇒ Object
Returns the value of attribute content.
52 53 54 |
# File 'lib/live_component/state.rb', line 52 def content @content end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
51 52 53 |
# File 'lib/live_component/state.rb', line 51 def klass @klass end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
51 52 53 |
# File 'lib/live_component/state.rb', line 51 def props @props end |
#root ⇒ Object (readonly) Also known as: root?
Returns the value of attribute root.
51 52 53 |
# File 'lib/live_component/state.rb', line 51 def root @root end |
#slots ⇒ Object (readonly)
Returns the value of attribute slots.
51 52 53 |
# File 'lib/live_component/state.rb', line 51 def slots @slots end |
Class Method Details
.build(definition, prop_overrides = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/live_component/state.rb', line 6 def build(definition, prop_overrides = {}) klass = definition["ruby_class"] || definition[:ruby_class] klass = LiveComponent::Utils.lookup_component_class(klass) if klass && !klass.is_a?(Class) props = definition["props"] || definition[:props] || {} props.symbolize_keys! props.except!(*prop_overrides.keys) props = klass.deserialize_props(props) if klass props.merge!(prop_overrides) slots = build_slots(definition["slots"] || definition[:slots] || {}) || {} children = build_children(definition["children"] || definition[:children] || {}) || {} State.new( klass: klass, props: props, slots: slots, children: children, content: definition["content"] || definition[:content] ) end |
Instance Method Details
#root! ⇒ Object
65 66 67 |
# File 'lib/live_component/state.rb', line 65 def root! @root = true end |
#to_h ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/live_component/state.rb', line 69 def to_h { ruby_class: klass ? klass.name : nil, props: klass ? klass.serialize_props(props) : LiveComponent.serializer.serialize(props), slots: slots.each_with_object({}) do |(k, v), h| h[k] = v.map(&:to_h) end, children: children.each_with_object({}) do |(k, v), h| h[k] = v.to_h end, content: content, } end |
#to_json ⇒ Object
87 88 89 |
# File 'lib/live_component/state.rb', line 87 def to_json to_h.to_json end |