Class: Lively::Application
- Inherits:
-
Protocol::HTTP::Middleware
- Object
- Protocol::HTTP::Middleware
- Lively::Application
- Defined in:
- lib/lively/application.rb
Overview
Represents the main Lively application middleware.
This class serves as the entry point for Lively applications, handling both standard HTTP requests for the initial page load and WebSocket connections for live updates. It integrates with the Live framework to provide real-time interactive web applications.
Use Application.[] to create a simple application class for a single view, optionally with shared state. For more complex applications, subclass and override #allowed_views, #state, and #body.
Constant Summary collapse
- VIEWS =
[HelloWorld].freeze
- STATE =
{}.freeze
Instance Attribute Summary collapse
-
#delegate ⇒ Object
readonly
Returns the value of attribute delegate.
- #The delegate middleware for request handling.(delegatemiddleware) ⇒ Object
Class Method Summary collapse
-
.[](*tags, **state) ⇒ Object
Create a new application class configured for a specific Live view tag, optionally with shared state that is passed to all views.
Instance Method Summary collapse
-
#allowed_views ⇒ Object
The view classes that this application allows.
-
#body ⇒ Object
Create the body content for this application.
-
#call(request) ⇒ Object
Process an incoming HTTP request.
-
#handle(request) ⇒ Object
Handle a standard HTTP request.
-
#index ⇒ Object
Create the index page for this application.
-
#initialize(delegate) ⇒ Application
constructor
Initialize a new Lively application.
-
#live(connection) ⇒ Object
Handle a WebSocket connection for live updates.
-
#resolver ⇒ Object
The resolver for live components.
-
#state ⇒ Object
The shared state for this application, passed to all views via the resolver.
-
#title ⇒ Object
Get the title for this application.
Constructor Details
#initialize(delegate) ⇒ Application
Initialize a new Lively application.
47 48 49 |
# File 'lib/lively/application.rb', line 47 def initialize(delegate) super(delegate) end |
Instance Attribute Details
#delegate ⇒ Object (readonly)
Returns the value of attribute delegate.
52 53 54 |
# File 'lib/lively/application.rb', line 52 def delegate @delegate end |
#The delegate middleware for request handling.(delegatemiddleware) ⇒ Object
52 |
# File 'lib/lively/application.rb', line 52 attr :delegate |
Class Method Details
.[](*tags, **state) ⇒ Object
Create a new application class configured for a specific Live view tag, optionally with shared state that is passed to all views.
36 37 38 39 40 41 42 43 |
# File 'lib/lively/application.rb', line 36 def self.[](*, **state) klass = Class.new(self) klass.const_set(:VIEWS, ) klass.const_set(:STATE, state) return klass end |
Instance Method Details
#allowed_views ⇒ Object
The view classes that this application allows. Override this in subclasses to specify which views can be resolved.
64 65 66 |
# File 'lib/lively/application.rb', line 64 def allowed_views self.class::VIEWS end |
#body ⇒ Object
Create the body content for this application.
91 92 93 |
# File 'lib/lively/application.rb', line 91 def body self.allowed_views.first.new(**self.state) end |
#call(request) ⇒ Object
Process an incoming HTTP request.
111 112 113 114 115 116 117 |
# File 'lib/lively/application.rb', line 111 def call(request) if request.path == "/live" return Async::WebSocket::Adapters::HTTP.open(request, &self.method(:live)) || Protocol::HTTP::Response[400] else return handle(request) end end |
#handle(request) ⇒ Object
Handle a standard HTTP request.
104 105 106 |
# File 'lib/lively/application.rb', line 104 def handle(request) return Protocol::HTTP::Response[200, [], [self.index.call]] end |
#index ⇒ Object
Create the index page for this application.
97 98 99 |
# File 'lib/lively/application.rb', line 97 def index Pages::Index.new(title: self.title, body: self.body) end |
#live(connection) ⇒ Object
Handle a WebSocket connection for live updates.
79 80 81 |
# File 'lib/lively/application.rb', line 79 def live(connection) Live::Page.new(self.resolver).run(connection) end |
#resolver ⇒ Object
The resolver for live components. Built from #allowed_views and #state.
71 72 73 74 75 |
# File 'lib/lively/application.rb', line 71 def resolver @resolver ||= Resolver.new(self.state).tap do |resolver| resolver.allow(*self.allowed_views) end end |
#state ⇒ Object
The shared state for this application, passed to all views via the resolver. Override this in subclasses to provide custom state.
57 58 59 |
# File 'lib/lively/application.rb', line 57 def state self.class::STATE end |
#title ⇒ Object
Get the title for this application.
85 86 87 |
# File 'lib/lively/application.rb', line 85 def title self.class.name end |