Class: Zui::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, id:, props: {}) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
13
# File 'lib/zui/node.rb', line 7

def initialize(type:, id:, props: {})
  @type = type.to_s
  @id = id.to_s
  @props = props.transform_keys(&:to_s)
  @children = []
  @events = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/zui/node.rb', line 5

def children
  @children
end

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/zui/node.rb', line 5

def events
  @events
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/zui/node.rb', line 5

def id
  @id
end

#propsObject (readonly)

Returns the value of attribute props.



5
6
7
# File 'lib/zui/node.rb', line 5

def props
  @props
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/zui/node.rb', line 5

def type
  @type
end

Instance Method Details

#subscribe(event) ⇒ Object



23
24
25
# File 'lib/zui/node.rb', line 23

def subscribe(event)
  @events << event.to_s unless @events.include?(event.to_s)
end

#to_hObject



15
16
17
18
19
20
21
# File 'lib/zui/node.rb', line 15

def to_h
  result = { "type" => type, "id" => id }
  result["props"] = props unless props.empty?
  result["children"] = children.map(&:to_h) unless children.empty?
  result["events"] = events unless events.empty?
  result
end