Class: WebRubyUI::Router

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

Overview

Client-side router matching URL paths to WebRubyUI Components.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



8
9
10
# File 'lib/web_ruby_ui/router.rb', line 8

def initialize
  @routes = {}
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



6
7
8
# File 'lib/web_ruby_ui/router.rb', line 6

def routes
  @routes
end

Instance Method Details

#match(path) ⇒ Object



16
17
18
19
# File 'lib/web_ruby_ui/router.rb', line 16

def match(path)
  clean_path = path.to_s.empty? ? '/' : path.to_s
  @routes[clean_path] || @routes['/']
end


21
22
23
24
25
26
27
28
# File 'lib/web_ruby_ui/router.rb', line 21

def navigate(path, target_element_id = 'app')
  component_class = match(path)
  return nil unless component_class

  component = component_class.new(path: path)
  component.mount(target_element_id)
  component
end

#route(path, component_class) ⇒ Object



12
13
14
# File 'lib/web_ruby_ui/router.rb', line 12

def route(path, component_class)
  @routes[path.to_s] = component_class
end