Class: LiveComponent::State

Inherits:
Object
  • Object
show all
Defined in:
lib/live_component/state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



51
52
53
# File 'lib/live_component/state.rb', line 51

def children
  @children
end

#contentObject

Returns the value of attribute content.



52
53
54
# File 'lib/live_component/state.rb', line 52

def content
  @content
end

#klassObject (readonly)

Returns the value of attribute klass.



51
52
53
# File 'lib/live_component/state.rb', line 51

def klass
  @klass
end

#propsObject (readonly)

Returns the value of attribute props.



51
52
53
# File 'lib/live_component/state.rb', line 51

def props
  @props
end

#rootObject (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

#slotsObject (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_hObject



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_jsonObject



87
88
89
# File 'lib/live_component/state.rb', line 87

def to_json
  to_h.to_json
end