Class: MooTool::DeviceTree::Node

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mootool/models/device_tree.rb

Overview

Represents a single node in the device tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, data) ⇒ Node

Returns a new instance of Node.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mootool/models/device_tree.rb', line 24

def initialize(tree, data)
  @tree = tree
  vals = T.must(data.read(8)).unpack(NODE_FORMAT)
  property_count = T.cast(vals[0], Integer)
  child_count = T.cast(vals[1], Integer)

  @properties = T.let({}, T::Hash[String, Property])
  @children = T.let([], T::Array[Node])

  property_count.times do
    prop = Property.new(data)
    @properties[prop.name] = prop
  end
  child_count.times { @children << Node.new(tree, data) }

  @tree.add_handle(self, T.must(@properties[PHANDLE_PROP]).value) if @properties.key? PHANDLE_PROP
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



21
22
23
# File 'lib/mootool/models/device_tree.rb', line 21

def children
  @children
end

#propertiesObject (readonly)

Returns the value of attribute properties.



21
22
23
# File 'lib/mootool/models/device_tree.rb', line 21

def properties
  @properties
end

Instance Method Details

#to_hObject



42
43
44
45
46
47
48
49
# File 'lib/mootool/models/device_tree.rb', line 42

def to_h
  props = @properties.transform_values(&:value)
  if @children.any?
    props.merge({ children: @children.map(&:to_h) })
  else
    props
  end
end