Class: WebRubyUI::Router
- Inherits:
-
Object
- Object
- WebRubyUI::Router
- Defined in:
- lib/web_ruby_ui/router.rb
Overview
Client-side router matching URL paths to WebRubyUI Components.
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(path) ⇒ Object
- #navigate(path, target_element_id = 'app') ⇒ Object
- #route(path, component_class) ⇒ Object
Constructor Details
#initialize ⇒ Router
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
#routes ⇒ Object (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 |
#navigate(path, target_element_id = 'app') ⇒ Object
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 |