CyberTrain API

The methods an application calls: in controllers, models, migrations, config/routes.rb, config/app.rb and tests. New to CyberTrain? Start with the tutorial; the README covers the CLI, deployment and the differences from Rails.

These pages are generated by YARD from the framework’s source, cybertrain/, and only from what is marked as part of the public API; everything else there is internal. Some of the API is not in the framework at all: spin run gen writes it into each application’s gen/ directory, and it is documented here through the classes examples/blog generates (Article, ArticleRelation, Gen::UrlHelpers).

Where things are

You write Reference
class ArticlesController < ApplicationController Cybertrain::Controller: before_action, rescue_from, render, redirect_to, head
params[:id], params.require(:article).permit(...) Cybertrain::Params
session[...], flash[...] Cybertrain::Session, Cybertrain::Flash
request.header(...), response.set_header(...), cookies Cybertrain::Request, Cybertrain::Response, Cybertrain::Cookies
Article.where(...).order(...).to_a, Article.find(id) Article and ArticleRelation (generated), Cybertrain::Relation
article.save, article.update(...), article.errors Cybertrain::Model, Cybertrain::Errors
validates, before_save { \|r\| ... } in app/models/ Cybertrain::Model.validates, Cybertrain::Model.before_save and the other callbacks
article.comments, comment.article Article#comments, Comment#article (from foreign keys)
config/routes.rb Cybertrain::Routes.draw, Cybertrain::Gen::Mapper
article_path(@article) Gen::UrlHelpers (generated), Cybertrain.url_root
db/migrate/*.rb Cybertrain::Migration::Base, Cybertrain::Schema::TableDef
config/app.rb Cybertrain.configure, Cybertrain::Config
test/*.rb Cybertrain::Test, Cybertrain::Test::Client, and the assertions below
app/views/**/*.html.erb Template language: helpers (link_to, form_with, …), the form builder and what a template may call
HTML from Ruby Cybertrain::Html, Cybertrain::SafeString

Exceptions you will meet

Exception Raised by Unhandled, the request gets
Cybertrain::RecordNotFound find, Cybertrain::Model#reload 500; the generated ApplicationController rescues it to a 404
Cybertrain::RecordInvalid Cybertrain::Model#save! 500
Cybertrain::Params::ParameterMissing Cybertrain::Params#require 400
ArgumentError order with anything but column [ASC\|DESC], render with the wrong options 500
Cybertrain::DB::Error SQLite refusing a query (a NOT NULL or foreign key constraint, a missing column) 500

Cybertrain::Controller.rescue_from matches the exception’s class exactly. In production a bare error response is replaced by public/<status>.html when that file exists; in development a 500 shows the exception’s class, message and template line.

Assertions

Top-level methods for test/*.rb (see Cybertrain::Test):

Assertion Passes when
assert(condition, message = ...) the condition is truthy
refute(condition, message = ...) it is false or nil
assert_equal(expected, actual) expected == actual
assert_nil(actual) it is nil
assert_includes(haystack, needle) a String contains it, or an Array includes it
assert_raises("ClassName") { ... } the block raises a matching StandardError; returns its message
flunk(message) never
assert_response(response, :ok) the status matches (an Integer, or :success, :redirect, :not_found, …)
assert_redirected_to(response, "/path") it is a redirect to exactly that Location

assert_response and assert_redirected_to come with require "cybertrain/test/client". Each is documented on the Top Level Namespace page.

Conventions on these pages

  • Signatures and behaviour are those of the current main branch; an application pins its framework version in spin.lock.
  • Many methods’ descriptions end with notes on the Spinel constraint behind a design choice. They explain the framework’s code; you can skip them.