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_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 beInt
instead ofInt64
orInt32
if this field is integer, and so doesFloat
. If it isNil
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 totype
. Ifvalue
is provided,type
becomes useless becausevalue
replaces the auto generated class method. However, it is still recommended to declaretype
for understandability. Don't usevalue
andas
together because it makesname
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 ofCrinder::Base
, but it must have the class methodrender(object : T | Array(T), json : JSON::Builder)
whereT
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 bothif
andunless
are provided, this field is only showed whenif
is truthy andunless
is falsey.