Class: WebRubyUI::VNode
- Inherits:
-
Object
- Object
- WebRubyUI::VNode
- Defined in:
- lib/web_ruby_ui/vnode.rb
Overview
Represents a virtual DOM node in pure Ruby memory.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#text_content ⇒ Object
readonly
Returns the value of attribute text_content.
Instance Method Summary collapse
- #diff(other_vnode) ⇒ Object
-
#initialize(tag:, attributes: {}, children: [], text_content: nil) ⇒ VNode
constructor
A new instance of VNode.
- #to_h ⇒ Object
Constructor Details
#initialize(tag:, attributes: {}, children: [], text_content: nil) ⇒ VNode
Returns a new instance of VNode.
8 9 10 11 12 13 |
# File 'lib/web_ruby_ui/vnode.rb', line 8 def initialize(tag:, attributes: {}, children: [], text_content: nil) @tag = tag.to_s.downcase @attributes = attributes || {} @children = children || [] @text_content = text_content end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/web_ruby_ui/vnode.rb', line 6 def attributes @attributes end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
6 7 8 |
# File 'lib/web_ruby_ui/vnode.rb', line 6 def children @children end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
6 7 8 |
# File 'lib/web_ruby_ui/vnode.rb', line 6 def tag @tag end |
#text_content ⇒ Object (readonly)
Returns the value of attribute text_content.
6 7 8 |
# File 'lib/web_ruby_ui/vnode.rb', line 6 def text_content @text_content end |
Instance Method Details
#diff(other_vnode) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/web_ruby_ui/vnode.rb', line 24 def diff(other_vnode) patches = [] return [{ type: :remove }] if other_vnode.nil? return [{ type: :replace, new_node: other_vnode }] if tag != other_vnode.tag patches << { type: :update_text, new_text: other_vnode.text_content } if text_content != other_vnode.text_content # Attr diffing attr_diff = diff_attributes(attributes, other_vnode.attributes) patches << { type: :update_attrs, attrs: attr_diff } if attr_diff.any? patches end |
#to_h ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/web_ruby_ui/vnode.rb', line 15 def to_h { tag: @tag, attributes: @attributes, children: @children.map { |c| c.is_a?(VNode) ? c.to_h : c }, text_content: @text_content } end |