lively 0.20.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ffe05b0ea4fbdde40a40b59e85277e9a87bd2ca0cd57350f720a7c4dd1f8445
4
- data.tar.gz: d53aedcf48bfd4eab8f1017b214ab2ee3b2cb424ca676c7f495cc7d68dcd5e11
3
+ metadata.gz: 8430df315a92af764a65128e479adf71cd0971e27edfc259ba9ec5c7bbc21cbc
4
+ data.tar.gz: c48c124558fd175a016cd60a38e5f9773821d0c9fe260ed5a60218ee21e4f08d
5
5
  SHA512:
6
- metadata.gz: 827c7ea471e0e9075422332e679b7ff05070d040f9e49dc2ce3e69dd23c852b70bd25fed8a6d0c53485391d2e0b906883fae64f6f97a7ad3bc9c30d845b9cc82
7
- data.tar.gz: a2bd006da9cce6a5303c3e7aa78bf43d4e895c3fff1f7086b7d071872be1742349d7b6549d5e9b853b7e1e72141f0f4ee039de523f2f2de8bc72198a619eda6d
6
+ metadata.gz: 529d3090d929d951da18f7f58df1cb6df958f1489b54bdcbf370003b24a103dcee9ec72071be2b50c0ca327c66486cb44ed3954064cd38180774166c50e319b8
7
+ data.tar.gz: 23a4576c7fd367afbe32c761f6e039c74b5bef211d35f1be48690a30c68c2546d4c2530101fbe7798a526b497b864ec98e52d6b6cea29bb30fa3c6bcbb433b43
checksums.yaml.gz.sig CHANGED
Binary file
@@ -117,29 +117,22 @@ class Application < Lively::Application
117
117
  end
118
118
 
119
119
  def configure_routes(router)
120
- super
121
-
122
- router.get("/") do
123
- render(DisplayView.new(**state))
120
+ router.get("/") do |request|
121
+ render_view(request, DisplayView)
124
122
  end
125
123
 
126
- router.get("/control") do |_request, parameters|
127
- render(ControlView.new(**state, mode: parameters["mode"]))
124
+ router.get("/control") do |request, parameters|
125
+ render_view(request, ControlView, mode: parameters["mode"])
128
126
  end
129
127
  end
130
-
131
- private
132
-
133
- def render(body)
134
- page = Lively::Pages::Index.new(title: "My App", body: body)
135
- Protocol::HTTP::Response[200, [], [page.call]]
136
- end
137
128
  end
138
129
  ```
139
130
 
131
+ `allowed_views` defines the view classes the live resolver may construct. Routes select which view to display at each path, including `/`. `make_view` constructs a view with shared state, `make_page` wraps it in the default page, and `render_view` composes both operations. A custom route can construct any {ruby Lively::Page} around `make_view` and call it with the request. Lively installs its `/live` WebSocket route independently, so overriding `configure_routes` does not remove it.
132
+
140
133
  Routes match exact paths and may accept one or more HTTP methods. Query parameters are decoded using `protocol-url` and passed to the handler as its second argument. Routes without an explicit method accept every method.
141
134
 
142
- Requests which do not match a route are passed to the application's delegate. An application using client-side history routing can instead override `#handle` to return its index page for unmatched paths.
135
+ Requests which do not match a route are passed to the application's delegate. An application using client-side history routing can instead override `#handle` to render an application page for unmatched paths.
143
136
 
144
137
  ## Live Reloading
145
138
 
@@ -31,7 +31,7 @@ module Lively
31
31
  # Create a new application class configured for a specific Live view tag,
32
32
  # optionally with shared state that is passed to all views.
33
33
  #
34
- # @parameter tag [Class] The Live view class to use as the application body.
34
+ # @parameter tag [Class] The Live view class to render at the root route.
35
35
  # @parameter state [Hash] Shared state to pass to all views as keyword arguments.
36
36
  # @returns [Class] A new application class configured for the specified tag.
37
37
  def self.[](*tags, **state)
@@ -87,16 +87,29 @@ module Lively
87
87
  self.class.name
88
88
  end
89
89
 
90
- # Create the body content for this application.
90
+ # Construct a view with shared application state.
91
+ # @parameter view_class [Class] The view class to construct.
92
+ # @parameter arguments [Hash] Additional keyword arguments for the view.
91
93
  # @returns [Live::View] A new view instance.
92
- def body
93
- self.allowed_views.first.new(**self.state)
94
+ def make_view(view_class, **arguments)
95
+ view_class.new(**self.state, **arguments)
94
96
  end
95
97
 
96
- # Create the index page for this application.
97
- # @returns [Pages::Index] A new index page instance.
98
- def index
99
- Pages::Index.new(title: self.title, body: self.body)
98
+ # Construct the default page for a view.
99
+ # Override this to customize the document surrounding live views.
100
+ # @parameter view [Live::View] The root view for the page.
101
+ # @returns [Page] A new page instance.
102
+ def make_page(view)
103
+ Pages::Index.new(title: self.title, body: view)
104
+ end
105
+
106
+ # Construct a view and its default page, then render an HTTP response.
107
+ # @parameter request [Protocol::HTTP::Request] The incoming request.
108
+ # @parameter view_class [Class] The view class to render.
109
+ # @parameter arguments [Hash] Additional keyword arguments for the view.
110
+ # @returns [Protocol::HTTP::Response] A successful HTML response.
111
+ def render_view(request, view_class, **arguments)
112
+ make_page(make_view(view_class, **arguments)).call(request)
100
113
  end
101
114
 
102
115
  # Handle a standard HTTP request which did not match a configured route.
@@ -106,16 +119,12 @@ module Lively
106
119
  return delegate.call(request)
107
120
  end
108
121
 
109
- # Add the standard application routes to the given router.
110
- # Override this method and call `super` to add application-specific routes.
122
+ # Add application routes to the given router.
123
+ # Override this method to map paths to views or other handlers.
111
124
  # @parameter router [Router] The router to configure.
112
125
  def configure_routes(router)
113
- router.get("/") do
114
- Protocol::HTTP::Response[200, [], [self.index.call]]
115
- end
116
-
117
- router.route("/live") do |request|
118
- Async::WebSocket::Adapters::HTTP.open(request, &self.method(:live)) || Protocol::HTTP::Response[400]
126
+ router.get("/") do |request|
127
+ self.render_view(request, self.allowed_views.first)
119
128
  end
120
129
  end
121
130
 
@@ -124,6 +133,7 @@ module Lively
124
133
  # @returns [Router] The configured router.
125
134
  def router
126
135
  @router ||= Router.new.tap do |router|
136
+ configure_system_routes(router)
127
137
  configure_routes(router)
128
138
  end
129
139
  end
@@ -134,5 +144,15 @@ module Lively
134
144
  def call(request)
135
145
  return router.call(request) || handle(request)
136
146
  end
147
+
148
+ private
149
+
150
+ # Add framework-owned routes to the given router.
151
+ # @parameter router [Router] The router to configure.
152
+ def configure_system_routes(router)
153
+ router.route("/live") do |request|
154
+ Async::WebSocket::Adapters::HTTP.open(request, &self.method(:live)) || Protocol::HTTP::Response[400]
155
+ end
156
+ end
137
157
  end
138
158
  end
data/lib/lively/page.rb CHANGED
@@ -4,6 +4,7 @@
4
4
  # Copyright, 2026, by Samuel Williams.
5
5
 
6
6
  require "json"
7
+ require "protocol/http/response"
7
8
  require "xrb/markup"
8
9
  require "xrb/tag"
9
10
  require "xrb/template"
@@ -94,10 +95,17 @@ module Lively
94
95
  XRB::MarkupString.raw(json)
95
96
  end
96
97
 
97
- # Render this page to a string.
98
+ # Render this page to an HTML string.
98
99
  # @returns [String]
99
- def call
100
+ def to_html
100
101
  @template.to_string(self)
101
102
  end
103
+
104
+ # Render this page as an HTTP response.
105
+ # @parameter request [Protocol::HTTP::Request] The incoming request.
106
+ # @returns [Protocol::HTTP::Response] A successful HTML response.
107
+ def call(request)
108
+ Protocol::HTTP::Response[200, {"content-type" => "text/html; charset=utf-8"}, [to_html]]
109
+ end
102
110
  end
103
111
  end
@@ -5,5 +5,5 @@
5
5
 
6
6
  # @namespace
7
7
  module Lively
8
- VERSION = "0.20.0"
8
+ VERSION = "0.21.0"
9
9
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lively
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.0
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file