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 #

[View source]
def self.new(raw : Raw) #

[View source]
def self.new(value) : self #

[View source]

Instance Method Detail

def <=>(other : Value) #

Compares this value to other.

TODO Enable proper comparison.


[View source]
def ==(other : Value) #

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


[View source]
def ==(other) #

Returns true if the raw object is equal to other.


[View source]
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.


[View source]
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.


[View source]
def []?(index : Int) : Value? #

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.


[View source]
def []?(key : String) : Value? #

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.


[View source]
def as_a : Array(Value) #

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


[View source]
def as_callable #

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


[View source]
def as_h : Dictionary #

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


[View source]
def as_indexable #

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


[View source]
def as_iterable #

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


[View source]
def as_nil : Nil #

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


[View source]
def as_number : Number #

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


[View source]
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.


[View source]
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


[View source]
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.


[View source]
def as_s_or_safe #

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


[View source]
def as_time #

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


[View source]
def as_undefined #

[View source]
def callable? #

Returns true if this value is a Callable


[View source]
def each #

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


[View source]
def each(&) #

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


[View source]
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.


[View source]
def indexable? #

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


[View source]
def iterable? #

Returns true if the value is iteraable.


[View source]
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.


[View source]
def mapping? #

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


[View source]
def none? #

Returns true if the value is nil.


[View source]
def number? #

Returns true if this value is a Number


[View source]
def pretty_print(pp : Crinja::PrettyPrint) #

[View source]
def raw : Raw #

[View source]
def raw_as(type : T.class) forall T #

[View source]
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.


[View source]
def raw_each(&) #

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


[View source]
def sameas?(other) #

Returns true if


[View source]
def sequence? #

Returns true if the value is a sequence.

TODO Improve implementation based on crinja_item


[View source]
def size : Int #

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


[View source]
def string? #

Returns true if the value is a string.


[View source]
def time? #

Returns true if the value is a time object.


[View source]
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]

[View source]
def to_f #

[View source]
def to_i #

[View source]
def to_json(builder : JSON::Builder) #

[View source]
def to_string #

Transform the value into a string representation.


[View source]
def truthy? #

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


[View source]
def undefined? #

Returns true if this value is a Undefined


[View source]