Class: Lively::Application

Inherits:
Protocol::HTTP::Middleware
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, resolver: self.class.resolver) ⇒ Application

Initialize a new Lively application.



48
49
50
51
52
# File 'lib/lively/application.rb', line 48

def initialize(delegate, resolver: self.class.resolver)
  super(delegate)
  
  @resolver = resolver
end

Instance Attribute Details

#delegateObject (readonly)

Returns the value of attribute delegate.



58
59
60
# File 'lib/lively/application.rb', line 58

def delegate
  @delegate
end

#resolverObject (readonly)

Returns the value of attribute resolver.



55
56
57
# File 'lib/lively/application.rb', line 55

def resolver
  @resolver
end

#The delegate middleware for request handling.(delegatemiddleware) ⇒ Object



58
# File 'lib/lively/application.rb', line 58

attr :delegate

#The resolver for live components.(resolver) ⇒ Object (readonly)



55
# File 'lib/lively/application.rb', line 55

attr :resolver

Class Method Details

.[](tag) ⇒ Object

Create a new application class configured for a specific Live view tag.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lively/application.rb', line 25

def self.[](tag)
  klass = Class.new(self)
  
  klass.define_singleton_method(:resolver) do
    Live::Resolver.allow(tag)
  end
  
  klass.define_method(:body) do
    tag.new
  end
  
  return klass
end

.resolverObject

Get the default resolver for this application.



41
42
43
# File 'lib/lively/application.rb', line 41

def self.resolver
  Live::Resolver.allow(HelloWorld)
end

Instance Method Details

#bodyObject

Create the body content for this application.



75
76
77
# File 'lib/lively/application.rb', line 75

def body(...)
  HelloWorld.new(...)
end

#call(request) ⇒ Object

Process an incoming HTTP request.



97
98
99
100
101
102
103
# File 'lib/lively/application.rb', line 97

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.



90
91
92
# File 'lib/lively/application.rb', line 90

def handle(request, ...)
  return Protocol::HTTP::Response[200, [], [self.index(...).call]]
end

#indexObject

Create the index page for this application.



82
83
84
# File 'lib/lively/application.rb', line 82

def index(...)
  Pages::Index.new(title: self.title, body: self.body(...))
end

#live(connection) ⇒ Object

Handle a WebSocket connection for live updates.



62
63
64
# File 'lib/lively/application.rb', line 62

def live(connection)
  Live::Page.new(@resolver).run(connection)
end

#titleObject

Get the title for this application.



68
69
70
# File 'lib/lively/application.rb', line 68

def title
  self.class.name
end