Class: Plushie::Node

Inherits:
Data
  • Object
show all
Defined in:
lib/plushie/node.rb

Overview

A UI tree node. Produced by DSL methods and widget builders.

Nodes are immutable value objects representing a single widget in the view tree. The runtime diffs consecutive trees to produce patches sent to the renderer.

Examples:

Creating a node

Node.new(id: "greeting", type: "text", props: { content: "Hello" })

Pattern matching

case node
in Node[type: "button", id:]
  puts "Found button: #{id}"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, type:, props: {}, children: [], meta: {}) ⇒ Node

Create a new Node with string-coerced id and type, and frozen props/children/meta.

Parameters:

  • id (#to_s)

    unique widget identifier

  • type (#to_s)

    widget type name

  • props (Hash) (defaults to: {})

    widget properties (will be frozen)

  • children (Array<Node>) (defaults to: [])

    child nodes (will be frozen)

  • meta (Hash) (defaults to: {})

    runtime-only metadata, never sent to the renderer (will be frozen)

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/plushie/node.rb', line 33

def initialize(id:, type:, props: {}, children: [], meta: {})
  raise ArgumentError, "Node id cannot be nil" if id.nil?
  raise ArgumentError, "Node type cannot be nil" if type.nil?
  super(id: id.to_s, type: type.to_s, props: props.freeze, children: children.freeze, meta: meta.freeze)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



24
25
26
# File 'lib/plushie/node.rb', line 24

def children
  @children
end

#children [Array<Node>] frozen array of child nodes (empty for leaf widgets)([Array<Node>](empty) ⇒ Object (readonly)

A UI tree node. Produced by DSL methods and widget builders.

Nodes are immutable value objects representing a single widget in the view tree. The runtime diffs consecutive trees to produce patches sent to the renderer.

Examples:

Creating a node

Node.new(id: "greeting", type: "text", props: { content: "Hello" })

Pattern matching

case node
in Node[type: "button", id:]
  puts "Found button: #{id}"
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/plushie/node.rb', line 24

Node = Data.define(:id, :type, :props, :children, :meta) do
  # Create a new Node with string-coerced id and type, and frozen props/children/meta.
  #
  # @param id [#to_s] unique widget identifier
  # @param type [#to_s] widget type name
  # @param props [Hash] widget properties (will be frozen)
  # @param children [Array<Node>] child nodes (will be frozen)
  # @param meta [Hash] runtime-only metadata, never sent to the renderer (will be frozen)
  # @return [Node]
  def initialize(id:, type:, props: {}, children: [], meta: {})
    raise ArgumentError, "Node id cannot be nil" if id.nil?
    raise ArgumentError, "Node type cannot be nil" if type.nil?
    super(id: id.to_s, type: type.to_s, props: props.freeze, children: children.freeze, meta: meta.freeze)
  end

  # Return a new Node with the given fields replaced.
  # Unspecified fields retain their current values.
  #
  # @param changes [Hash] fields to replace (:id, :type, :props, :children, :meta)
  # @return [Node] new Node with the changes applied
  def with(**changes)
    self.class.new(**to_h.merge(changes))
  end
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



24
25
26
# File 'lib/plushie/node.rb', line 24

def id
  @id
end

#id [String] unique widget identifier (scoped within parent containers)([String](scoped within parent containers)) ⇒ Object (readonly)

A UI tree node. Produced by DSL methods and widget builders.

Nodes are immutable value objects representing a single widget in the view tree. The runtime diffs consecutive trees to produce patches sent to the renderer.

Examples:

Creating a node

Node.new(id: "greeting", type: "text", props: { content: "Hello" })

Pattern matching

case node
in Node[type: "button", id:]
  puts "Found button: #{id}"
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/plushie/node.rb', line 24

Node = Data.define(:id, :type, :props, :children, :meta) do
  # Create a new Node with string-coerced id and type, and frozen props/children/meta.
  #
  # @param id [#to_s] unique widget identifier
  # @param type [#to_s] widget type name
  # @param props [Hash] widget properties (will be frozen)
  # @param children [Array<Node>] child nodes (will be frozen)
  # @param meta [Hash] runtime-only metadata, never sent to the renderer (will be frozen)
  # @return [Node]
  def initialize(id:, type:, props: {}, children: [], meta: {})
    raise ArgumentError, "Node id cannot be nil" if id.nil?
    raise ArgumentError, "Node type cannot be nil" if type.nil?
    super(id: id.to_s, type: type.to_s, props: props.freeze, children: children.freeze, meta: meta.freeze)
  end

  # Return a new Node with the given fields replaced.
  # Unspecified fields retain their current values.
  #
  # @param changes [Hash] fields to replace (:id, :type, :props, :children, :meta)
  # @return [Node] new Node with the changes applied
  def with(**changes)
    self.class.new(**to_h.merge(changes))
  end
end

#metaObject (readonly)

Returns the value of attribute meta

Returns:

  • (Object)

    the current value of meta



24
25
26
# File 'lib/plushie/node.rb', line 24

def meta
  @meta
end

#meta [Hash] frozen runtime-only metadata, never sent to the renderer or included in diffs([Hash], neversenttotherenderer) ⇒ Object (readonly)

A UI tree node. Produced by DSL methods and widget builders.

Nodes are immutable value objects representing a single widget in the view tree. The runtime diffs consecutive trees to produce patches sent to the renderer.

Examples:

Creating a node

Node.new(id: "greeting", type: "text", props: { content: "Hello" })

Pattern matching

case node
in Node[type: "button", id:]
  puts "Found button: #{id}"
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/plushie/node.rb', line 24

Node = Data.define(:id, :type, :props, :children, :meta) do
  # Create a new Node with string-coerced id and type, and frozen props/children/meta.
  #
  # @param id [#to_s] unique widget identifier
  # @param type [#to_s] widget type name
  # @param props [Hash] widget properties (will be frozen)
  # @param children [Array<Node>] child nodes (will be frozen)
  # @param meta [Hash] runtime-only metadata, never sent to the renderer (will be frozen)
  # @return [Node]
  def initialize(id:, type:, props: {}, children: [], meta: {})
    raise ArgumentError, "Node id cannot be nil" if id.nil?
    raise ArgumentError, "Node type cannot be nil" if type.nil?
    super(id: id.to_s, type: type.to_s, props: props.freeze, children: children.freeze, meta: meta.freeze)
  end

  # Return a new Node with the given fields replaced.
  # Unspecified fields retain their current values.
  #
  # @param changes [Hash] fields to replace (:id, :type, :props, :children, :meta)
  # @return [Node] new Node with the changes applied
  def with(**changes)
    self.class.new(**to_h.merge(changes))
  end
end

#propsObject (readonly)

Returns the value of attribute props

Returns:

  • (Object)

    the current value of props



24
25
26
# File 'lib/plushie/node.rb', line 24

def props
  @props
end

#props [Hash] frozen hash of property values (symbol keys)([Hash](symbol keys)) ⇒ Object (readonly)

A UI tree node. Produced by DSL methods and widget builders.

Nodes are immutable value objects representing a single widget in the view tree. The runtime diffs consecutive trees to produce patches sent to the renderer.

Examples:

Creating a node

Node.new(id: "greeting", type: "text", props: { content: "Hello" })

Pattern matching

case node
in Node[type: "button", id:]
  puts "Found button: #{id}"
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/plushie/node.rb', line 24

Node = Data.define(:id, :type, :props, :children, :meta) do
  # Create a new Node with string-coerced id and type, and frozen props/children/meta.
  #
  # @param id [#to_s] unique widget identifier
  # @param type [#to_s] widget type name
  # @param props [Hash] widget properties (will be frozen)
  # @param children [Array<Node>] child nodes (will be frozen)
  # @param meta [Hash] runtime-only metadata, never sent to the renderer (will be frozen)
  # @return [Node]
  def initialize(id:, type:, props: {}, children: [], meta: {})
    raise ArgumentError, "Node id cannot be nil" if id.nil?
    raise ArgumentError, "Node type cannot be nil" if type.nil?
    super(id: id.to_s, type: type.to_s, props: props.freeze, children: children.freeze, meta: meta.freeze)
  end

  # Return a new Node with the given fields replaced.
  # Unspecified fields retain their current values.
  #
  # @param changes [Hash] fields to replace (:id, :type, :props, :children, :meta)
  # @return [Node] new Node with the changes applied
  def with(**changes)
    self.class.new(**to_h.merge(changes))
  end
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



24
25
26
# File 'lib/plushie/node.rb', line 24

def type
  @type
end

#type [String] widget type name ("button", "column", "text_input", etc.)([String]("button", "column", "text_input", etc.)) ⇒ Object (readonly)

A UI tree node. Produced by DSL methods and widget builders.

Nodes are immutable value objects representing a single widget in the view tree. The runtime diffs consecutive trees to produce patches sent to the renderer.

Examples:

Creating a node

Node.new(id: "greeting", type: "text", props: { content: "Hello" })

Pattern matching

case node
in Node[type: "button", id:]
  puts "Found button: #{id}"
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/plushie/node.rb', line 24

Node = Data.define(:id, :type, :props, :children, :meta) do
  # Create a new Node with string-coerced id and type, and frozen props/children/meta.
  #
  # @param id [#to_s] unique widget identifier
  # @param type [#to_s] widget type name
  # @param props [Hash] widget properties (will be frozen)
  # @param children [Array<Node>] child nodes (will be frozen)
  # @param meta [Hash] runtime-only metadata, never sent to the renderer (will be frozen)
  # @return [Node]
  def initialize(id:, type:, props: {}, children: [], meta: {})
    raise ArgumentError, "Node id cannot be nil" if id.nil?
    raise ArgumentError, "Node type cannot be nil" if type.nil?
    super(id: id.to_s, type: type.to_s, props: props.freeze, children: children.freeze, meta: meta.freeze)
  end

  # Return a new Node with the given fields replaced.
  # Unspecified fields retain their current values.
  #
  # @param changes [Hash] fields to replace (:id, :type, :props, :children, :meta)
  # @return [Node] new Node with the changes applied
  def with(**changes)
    self.class.new(**to_h.merge(changes))
  end
end

Instance Method Details

#with(**changes) ⇒ Node

Return a new Node with the given fields replaced. Unspecified fields retain their current values.

Parameters:

  • changes (Hash)

    fields to replace (:id, :type, :props, :children, :meta)

Returns:

  • (Node)

    new Node with the changes applied



44
45
46
# File 'lib/plushie/node.rb', line 44

def with(**changes)
  self.class.new(**to_h.merge(changes))
end