Ignore

Please note that your browser is not supported.

We recommend upgrading to the latest Firefox or Google Chrome.

Reflection

Most web applications share quite a bit of common code, or boilerplate. When building an application, a significant amount of time is spent connecting various aspects together and writing code to perform the same work over and over. Not only is this time consuming, it means there's a lot more you have to know before you can build something.

Pakyow includes a feature called Reflection that takes care of the boilerplate for you. As you define your interface with view templates, Reflection bootstraps custom backend behavior that powers your frontend. The reflection handles things like presenting dynamic data from a database, defining actions for creating and changing data, and setting up navigation between pages of your application. You can even use the reflection in production!

What this means is that you can build a working, production-ready application just by writing a little bit of HTML. With the boilerplate taken care of, you're free to focus on the features that make your application unique.

How it works

Reflection works by building an understanding of your application from the semantic intent in your view templates, such as data bindings. When Reflection encounters a binding type with one or more attributes, a matching data source is created for storing data in the database. The data source is exposed back to the frontend view template through endpoints, automatically presenting data from the database in your views.

Reflection also looks for any forms defined for the binding type, creating actions that connect each form to its reflected data source. Reflected actions handle everything from verifying and validating the submitted data, presenting errors back to the user, and saving data to the database.

There's a lot more to cover, but we'll get to the details in next few guides.

Inspecting the reflection

Reflection doesn't generate code, instead it attaches behavior dynamically to your project when it boots. You can see everything reflection has attached to your project with the following commands:

Reflected Actions & Endpoints:

pakyow info:endpoints

:messages_create          POST  /messages                      pakyow/reflection
:messages_replies_create  POST  /messages/:message_id/replies  pakyow/reflection
:messages_show            GET   /messages/:id                  pakyow/reflection
:root                     GET   /                              pakyow/reflection

Reflected Data Sources:

pakyow info:sources

:messages pakyow/reflection
  has_many :replies

  attribute :id,         :bignum
  attribute :content,    :string
  attribute :created_at, :datetime
  attribute :updated_at, :datetime

:replies pakyow/reflection
  belongs_to :message

  attribute :id,         :bignum
  attribute :message_id, :bignum
  attribute :content,    :string
  attribute :created_at, :datetime
  attribute :updated_at, :datetime

Reflected parts are tagged with pakyow/reflection.

Integrating reflection with your backend

Reflection will take your project pretty far, but most projects will probably hit a point where custom behavior is needed. For example, user authentication isn't something you can do with reflection since it relies on more complex behavior such as session management.

If and when you hit the point of needing custom code, you can continue to use Reflection just as you were up to that point. All of the reflected behavior integrates seamlessly with any custom behavior you add, helping where it can but backing off when you decide to take over.

Our goal is to keep you from writing boilerplate and focused on what makes your project unique.

Complete example

You can walk through a complete example of reflection in the 5-Minute App guide. The guide includes source code and a public instance of the reflected application that you can play with to get a feel for the behavior that reflection offers. It's a great place to start.

Next Up: Data Sources