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