module Crinder::Field

Direct including types

Defined in:

crinder/field.cr

Macro Summary

Macro Detail

macro field(decl, **nargs) #

Defines a field.

This also creates an alias {{name}} for object.{{name}}, which can be used in value, if or unless.

Example

See README or Crinder::Base.

Usage

field requries a name or a type declaration and a series of named arguments.

field name : type, **named_arguments
  • name: (required) the field name to be rendered
  • as: the name to be replaced in the rendered json
  • type: the type for auto casting. For example, if it is String, #to_s of the field will be called for rendering. This is JSON Type but not Crystal Type, so it must be one of JSON::Any::Type, and it should be Int instead of Int64 or Int32 if this field is integer, and so does Float. If it is Nil or not provided, no casting method will be performed.
  • value: a lambda, a class method or a constant to replace the value. By default, it is an auto generated class method name which casting the field to type. If value is provided, type becomes useless because value replaces the auto generated class method. However, it is still recommended to declare type for understandability. Don't use value and as together because it makes name meaningless.
  • with: a renderer for this field. This field will be filtered by value before passing to it. It is not necessary to be a subclass of Crinder::Base, but it must have the class method render(object : T | Array(T), json : JSON::Builder) where T is the original type of this field.
  • options: options passing to the with renderer.
  • if: a lambda, a class method or a constant to determine whether to show this field.
  • unless: opposite of if. If both if and unless are provided, this field is only showed when if is truthy and unless is falsey.

[View source]
macro remove(name) #

Undefines a field.


[View source]