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.



193
194
195
196
197
# File 'lib/ruflet/rails.rb', line 193

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



221
222
223
224
225
226
227
# File 'lib/ruflet/rails.rb', line 221

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



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/ruflet/rails.rb', line 204

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



199
200
201
202
# File 'lib/ruflet/rails.rb', line 199

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