Class: Mayu::VDOM::Descriptor

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Includes:
Interfaces::Descriptor
Defined in:
lib/mayu/vdom/descriptor.rb

Defined Under Namespace

Classes: FactoryImpl

Constant Summary collapse

Factory =
T.let(FactoryImpl.new, FactoryImpl)

Constants included from Interfaces::Descriptor

Interfaces::Descriptor::COMMENT, Interfaces::Descriptor::TEXT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interfaces::Descriptor

#children, #comment?, #element?, #has_children?, #hash, #itself, #key, #props, #slot, #text, #text?, #then, #type

Class Method Details

.[](type, *children, **props) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/mayu/vdom/descriptor.rb', line 88

def self.[](type, *children, **props)
  type = T.let(SpecialElements.for_type(type), Component::ElementType)

  children = Children.new(children, parent_type: type)
  props = props.merge(children:)
  key = props.delete(:key)
  slot = props.delete(:slot)&.to_s

  new(type:, key:, slot:, props:)
end

Instance Method Details

#component?Boolean

Returns:

  • (Boolean)


117
# File 'lib/mayu/vdom/descriptor.rb', line 117

def component? = Component.component_class?(@type)

#component_classObject



120
121
122
123
124
125
126
# File 'lib/mayu/vdom/descriptor.rb', line 120

def component_class
  if Component.component_class?(@type)
    T.cast(@type, T.class_of(Component::Base))
  else
    raise "#{@type.inspect} is not a component class"
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


114
# File 'lib/mayu/vdom/descriptor.rb', line 114

def eql?(other) = self.class === other && same?(other)

#marshal_dumpObject



100
101
102
# File 'lib/mayu/vdom/descriptor.rb', line 100

def marshal_dump
  [ComponentMarshaler.new(type), Marshalling.dump_props(props), key, slot]
end

#marshal_load(a) ⇒ Object



105
106
107
108
# File 'lib/mayu/vdom/descriptor.rb', line 105

def marshal_load(a)
  @type, @props, @key, @slot = a
  freeze
end

#same?(other) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mayu/vdom/descriptor.rb', line 136

def same?(other)
  if key == other.key && type == other.type
    if type == :input
      # Inputs are considered to be different if their type changes.
      # Is this a good behavior? I think maybe it comes from from Preact.
      props[:type] == other.props[:type]
    else
      true
    end
  else
    false
  end
end

#to_sObject



129
130
131
132
133
# File 'lib/mayu/vdom/descriptor.rb', line 129

def to_s
  return text if text?
  return "" if comment?
  "#<Descriptor type=#{type.inspect}>"
end