class StirFry::Component
Component is a request/response view handler. It can be a plain class with a template inlined in the same file but it can also have the request class methods such as self.get, self.post, self.delete ect. so that specific requests can have more rich handling and different views can be returned.
Example:
module Todos class Todo < StirFry::Component attr_reader :todo class << self # PUT /todo/:id/toggle def put(request:, response:, id:) todo = ::App.todos.find { |t| t["id"] == id.to_i } todo["done"] = !todo["done"] response.ok(new(todo: todo)) # Return a Todo view end # DELETE /todo/:id def delete(request:, response:, id:) ::App.todos.delete_if { |t| t["id"] == id.to_i } response.render(List.new(todos: ::App.todos)) # Return a list view instead end end def initialize(todo:) super() @todo = todo end end end
Attributes
application is root application where all the routes are defined and maybe where you might keep config or database references. This is only defined on the root component that is defined on the route.
request is the request that called this component. This is only defined on the root component that is defined on the route.
response is the response that is used for responding in the component. This is only defined on the root component that is defined on the route.
Public Class Methods
# File lib/stir_fry/component.rb, line 60 def self.call(application: nil, request: nil, response: nil, **args) new(application: application, request: request, response: response, **args).call end
call it the method called from routing. It is called with the request and response first, and the arguments to initialize the component will be appended to the end. It will set the request, and response on the component and call render on the component.
Source
# File lib/stir_fry/component.rb, line 54 def self.logger = StirFry.logger
Simple access to StirFry.logger
# File lib/stir_fry/component.rb, line 65 def initialize(application: nil, request: nil, response: nil, **_args) self.application = application self.request = request self.response = response end
create a new component with a request and response
Public Instance Methods
Source
# File lib/stir_fry/component.rb, line 73 def call response.html(render) end
call is sugar for response.html(render) and should be overriden to change how a component responds.
Source
# File lib/stir_fry/component.rb, line 78 def logger = StirFry.logger
Simple access to StirFry.logger