struct Crinja::Value

Overview

Value represents an object inside the Crinja runtime.

It wraps a Crystal value in #raw and defines methods to access properties of the wrapped value while being agnostic about the actual type of the wrapped raw value.

Included Modules

Defined in:

runtime/value.cr:2
runtime/value.cr:102
runtime/value.cr:601

Constant Summary

UNDEFINED = new(Undefined.new)

Constructors

Instance Method Summary

Constructor Detail

def self.new(value : self) : self #

def self.new(raw : Raw) #

def self.new(value) : self #

Instance Method Detail

def <=>(other : Value) #

Compares this value to other.

TODO Enable proper comparison.


def ==(other : Value) #

Returns true if both self and other's raw object are equal.


def ==(other) #

Returns true if the raw object is equal to other.


def [](index : Int) : Value #

Assumes the underlying value is an Indexable or String returns the element at the given index. Raises if the underlying value is not an Indexable or String.


def [](key : String) : Value #

Assumes the underlying value has an hash-like accessor and returns the element with the given key. Raises if the underlying value is not hash-like.


def []?(index : Int) : Value | Nil #

Assumes the underlying value is an Indexable or String and returns the element at the given index, or nil if out of bounds. Raises if the underlying value is not an Indexable or String.


def []?(key : String) : Value | Nil #

Assumes the underlying value has an hash-like accessor returns the element with the given key, or nil if the key is not present. Raises if the underlying value is not hash-like.


def as_a : Array(Value) #

Checks that the underlying value is Array, and returns its value. Raises otherwise.


def as_callable #

Checks that the underlaying value is a Callable | Callable::Proc and retuns its value. Raises otherwise.


def as_h : Dictionary #

Checks that the underlying value is Hash, and returns its value. Raises otherwise.


def as_indexable #

Checks that the underlaying value is a Indexable and retuns its value. Raises otherwise.


def as_iterable #

Checks that the underlaying value is a Iterable and retuns its value. Raises otherwise.


def as_nil : Nil #

Checks that the underlying value is Nil, and returns nil. Raises otherwise.


def as_number : Number #

Checks that the underlying value is a Crinja::Number, and returns its value. Raises otherwise.


def as_s #

Checks that the underlying value is String, and returns the value as String. Raises otherwise.

If the value is SafeString it is unwrapped as String.


def as_s! #

Checks that the underlying value is String, and returns the value as String. Raises otherwise.

If the value is SafeString it is unwrapped as String.

DEPRECATED Use #as_s instead


def as_s? #

Checks that the underlying value is String | SafeString | Nil, and returns the value as String. Raises otherwise.

If the value is SafeString it is unwrapped as String.


def as_s_or_safe #

Checks that the underlying value is String | SafeString, and returns its value. Raises otherwise.


def as_time #

Checks that the underlaying value is a Time object and retuns its value. Raises otherwise.


def as_undefined #

def callable? #

Returns true if this value is a Callable


def each #

Returns an iterator for the underlying value if it is an Iterable, String or Undefined which iterates through the items as Value.


def each(&) #

Assumes the underlying value is an Iterable and yields each of the elements or key/values, always as Value.


def first #

Assumes the underlying value is an Iterable, Hash or String and returns the first item in the list or the first character of the string.


def indexable? #

Returns true if the value is a list (Array).


def iterable? #

Returns true if the value is iteraable.


def last #

Assumes the underlying value is a String or responds to #last and returns the last item in the list or the last character of the string.


def mapping? #

Returns true if the object is a mapping (Hash or Crinja::Object).


def none? #

Returns true if the value is nil.


def number? #

Returns true if this value is a Number


def pretty_print(pp : Crinja::PrettyPrint) #

def raw : Raw #

def raw_as(type : T.class) forall T #

def raw_each : ::Iterator #

Returns an iterator for the underlying value if it is an Iterable, String or Undefined which iterates through the items as Value.


def raw_each(&) #

Assumes the underlying value is an Iterable and yields each of the elements or key/values, always as Value.


def sameas?(other) #

Returns true if


def sequence? #

Returns true if the value is a sequence.

TODO Improve implementation based on crinja_item


def size : Int #

Assumes the underlying value responds to #size and returns its size.


def string? #

Returns true if the value is a string.


def time? #

Returns true if the value is a time object.


def to_a #
Description copied from module Enumerable(Crinja::Value)

Returns an Array with all the elements in the collection.

(1..5).to_a # => [1, 2, 3, 4, 5]

def to_f #

def to_i #

def to_json(builder : JSON::Builder) #

def to_string #

Transform the value into a string representation.


def truthy? #

Returns true unless this value is false, 0, nil or #undefined?


def undefined? #

Returns true if this value is a Undefined