module Crinder::Field
Direct including types
Defined in:
crinder/field.crMacro Summary
-
field(decl, **nargs)
Defines a field.
-
remove(name)
Undefines a field.
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_sof 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 beIntinstead ofInt64orInt32if this field is integer, and so doesFloat. If it isNilor 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
namewhich casting the field totype. Ifvalueis provided,typebecomes useless becausevaluereplaces the auto generated class method. However, it is still recommended to declaretypefor understandability. Don't usevalueandastogether because it makesnamemeaningless. - with: a renderer for this field. This field will be filtered by
valuebefore passing to it. It is not necessary to be a subclass ofCrinder::Base, but it must have the class methodrender(object : T | Array(T), json : JSON::Builder)whereTis the original type of this field. - options: options passing to the
withrenderer. - if: a lambda, a class method or a constant to determine whether to show this field.
- unless: opposite of
if. If bothifandunlessare provided, this field is only showed whenifis truthy andunlessis falsey.