module Crinja::Object
Overview
This module can be included into custom types to make their instances available as values inside the Crinja runtime.
There are three types of properties that can be exposed to the Crinja runtime:
#crinja_attribute(name : Crinja::Value) : Crinja::Value
: Access an attribute (e.g. an instance property) of this type.#crinja_item(name : Crinja::Value) : Crinja::Value
: Access an item (e.g. an array member) of this type.#crinja_call(name : String) : Crinja::Callable | Callable::Proc | Nil
: Expose a callable as method of this type.
Through the static comilation it is not possible to access properties or methods of an object
directly from inside the Crinja runtime. These methods allow to define a name-based lookup and
return the corresponding values. If the looked-up name is not defined, the return value for crinja_call
should be nil
. should return Crinja::Undefined
.
crinja_attribute
and crinja_item
must return an Crinja::Undefined
if there is no attribute or item of that name.
In this case, Crinja::Resolver
may try other methods of accessing the attribute or item depending on the type
of lookup (see Notes on Subscription for Jinja2).
Implementing classes do not need to implement these methods. They will only be accessed if an
instance of Crinja::Object
responds to them. Otherwise it will be considered as if there are no
attributes, items or methods defined.
Example:
class User
include Crinja::Object
property name : String
property dob : Time
def initialize(@name, @dob)
end
def age
(Time.now - @dob).years
end
def crinja_attribute(attr : Crinja::Value) : Crinja::Value
value = case attr.to_string
when "name"
name
when "age"
age
else
Undefined.new(attr.to_s)
end
Crinja::Value.new(value)
end
def crinja_call(name : String) : Crinja::Callable | Crinja::Callable::Proc | Nil
if name == "days_old"
->(arguments : Crinja::Arguments) do
self.age.days
end
end
end
end