Class: WebRubyUI::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/web_ruby_ui/component.rb

Overview

Base Component class providing reactive state tracking and HTML builder DSL.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props = {}) ⇒ Component

Returns a new instance of Component.



33
34
35
36
37
38
39
40
41
# File 'lib/web_ruby_ui/component.rb', line 33

def initialize(props = {})
  @props = props
  @state_values = {}
  self.class.state_definitions.each do |name, default|
    @state_values[name] = default
  end
  @current_vnode = nil
  @children_stack = []
end

Instance Attribute Details

#mount_element_idObject (readonly)

Returns the value of attribute mount_element_id.



31
32
33
# File 'lib/web_ruby_ui/component.rb', line 31

def mount_element_id
  @mount_element_id
end

Class Method Details

.state(name, default: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/web_ruby_ui/component.rb', line 10

def state(name, default: nil)
  state_definitions[name] = default

  define_method(name) do
    @state_values[name]
  end

  define_method("#{name}=") do |new_val|
    old_val = @state_values[name]
    return unless old_val != new_val

    @state_values[name] = new_val
    re_render!
  end
end

.state_definitionsObject



26
27
28
# File 'lib/web_ruby_ui/component.rb', line 26

def state_definitions
  @state_definitions ||= {}
end

Instance Method Details

#mount(element_id) ⇒ Object



43
44
45
46
# File 'lib/web_ruby_ui/component.rb', line 43

def mount(element_id)
  @mount_element_id = element_id
  re_render!
end

#re_render!Object



52
53
54
55
56
57
58
# File 'lib/web_ruby_ui/component.rb', line 52

def re_render!
  new_vnode = build_vnode_tree
  @current_vnode = new_vnode

  DOM.render_to_element(@mount_element_id, new_vnode) if @mount_element_id
  new_vnode
end

#renderObject

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/web_ruby_ui/component.rb', line 48

def render
  raise NotImplementedError, "#{self.class.name}#render must be implemented"
end