Class: Ruflet::Rails::ViewRouter

Inherits:
Object
  • Object
show all
Includes:
UI::SharedControlForwarders
Defined in:
lib/ruflet/rails.rb

Overview

ViewRouter dispatches incoming page connections to the correct RufletView subclass based on the current route. It includes SharedControlForwarders so that widget helpers (text, column, safe_area, …) can be called directly inside its own rendering helpers — the same pattern showcase uses.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, routes: nil, default: nil) ⇒ ViewRouter

Returns a new instance of ViewRouter.



65
66
67
68
69
# File 'lib/ruflet/rails.rb', line 65

def initialize(page, routes: nil, default: nil)
  @page = page
  @routes = normalize_routes(routes || self.class.discovered_routes)
  @default = default || @routes["/"]
end

Class Method Details

.discovered_routesObject



93
94
95
96
97
98
99
# File 'lib/ruflet/rails.rb', line 93

def self.discovered_routes
  Ruflet::Rails.view_classes.each_with_object({}) do |view_class, routes|
    next unless view_class.respond_to?(:route)

    routes[view_class.route] = view_class
  end
end

Instance Method Details

#renderObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruflet/rails.rb', line 76

def render
  if root_route_without_default?
    render_route_index
    return
  end

  target = route_target(@page.route)

  if target.respond_to?(:render)
    target.render(@page)
  elsif target.respond_to?(:call)
    target.call(@page)
  else
    render_empty_state
  end
end

#startObject



71
72
73
74
# File 'lib/ruflet/rails.rb', line 71

def start
  @page.on_route_change = ->(_event) { render }
  render
end