struct Crinja::Value
- Crinja::Value
- Struct
- Value
- Object
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
- Comparable(Crinja::Value)
- Enumerable(Crinja::Value)
- Iterable(Crinja::Value)
Defined in:
runtime/value.cr:2runtime/value.cr:102
runtime/value.cr:601
Constant Summary
-
UNDEFINED =
new(Undefined.new)
Constructors
Instance Method Summary
-
#<=>(other : Value)
Compares this value to other.
-
#==(other : Value)
Returns
trueif bothselfand other's raw object are equal. -
#==(other)
Returns
trueif the raw object is equal to other. -
#[](index : Int) : Value
Assumes the underlying value is an
IndexableorStringreturns the element at the given index. -
#[](key : String) : Value
Assumes the underlying value has an hash-like accessor and returns the element with the given key.
-
#[]?(index : Int) : Value?
Assumes the underlying value is an
IndexableorStringand returns the element at the given index, ornilif out of bounds. -
#[]?(key : String) : Value?
Assumes the underlying value has an hash-like accessor returns the element with the given key, or
nilif the key is not present. -
#as_a : Array(Value)
Checks that the underlying value is
Array, and returns its value. -
#as_callable
Checks that the underlaying value is a
Callable | Callable::Procand retuns its value. -
#as_h : Dictionary
Checks that the underlying value is
Hash, and returns its value. -
#as_indexable
Checks that the underlaying value is a
Indexableand retuns its value. -
#as_iterable
Checks that the underlaying value is a
Iterableand retuns its value. -
#as_nil : Nil
Checks that the underlying value is
Nil, and returnsnil. -
#as_number : Number
Checks that the underlying value is a
Crinja::Number, and returns its value. -
#as_s
Checks that the underlying value is
String, and returns the value asString. -
#as_s!
Checks that the underlying value is
String, and returns the value asString.DEPRECATED Use
#as_sinstead -
#as_s?
Checks that the underlying value is
String | SafeString | Nil, and returns the value asString. -
#as_s_or_safe
Checks that the underlying value is
String | SafeString, and returns its value. -
#as_time
Checks that the underlaying value is a
Timeobject and retuns its value. - #as_undefined
-
#callable?
Returns
trueif this value is aCallable - #each
-
#each(&)
Assumes the underlying value is an
Iterableand yields each of the elements or key/values, always asValue. -
#first
Assumes the underlying value is an
Iterable,HashorStringand returns the first item in the list or the first character of the string. -
#indexable?
Returns
trueif the value is a list (Array). -
#iterable?
Returns
trueif the value is iteraable. -
#last
Assumes the underlying value is a
Stringor responds to#lastand returns the last item in the list or the last character of the string. -
#mapping?
Returns
trueif the object is a mapping (Hash or Crinja::Object). -
#none?
Returns
trueif the value is nil. -
#number?
Returns
trueif this value is aNumber - #pretty_print(pp : Crinja::PrettyPrint)
- #raw : Raw
- #raw_as(type : T.class) forall T
- #raw_each : ::Iterator
-
#raw_each(&)
Assumes the underlying value is an
Iterableand yields each of the elements or key/values, always asValue. -
#sameas?(other)
Returns true if
-
#sequence?
Returns
trueif the value is a sequence. -
#size : Int
Assumes the underlying value responds to
#sizeand returns its size. -
#string?
Returns
trueif the value is a string. -
#time?
Returns
trueif the value is a time object. -
#to_a
Returns an
Arraywith all the elements in the collection. - #to_f
- #to_i
- #to_json(builder : JSON::Builder)
-
#to_string
Transform the value into a string representation.
-
#truthy?
Returns
trueunless this value isfalse,0,nilor#undefined? -
#undefined?
Returns
trueif this value is aUndefined
Constructor Detail
Instance Method Detail
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.
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.
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.
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.
Checks that the underlying value is Array, and returns its value. Raises otherwise.
Checks that the underlaying value is a Callable | Callable::Proc and retuns its value. Raises otherwise.
Checks that the underlying value is Hash, and returns its value. Raises otherwise.
Checks that the underlaying value is a Indexable and retuns its value. Raises otherwise.
Checks that the underlaying value is a Iterable and retuns its value. Raises otherwise.
Checks that the underlying value is a Crinja::Number, and returns its value. Raises otherwise.
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.
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
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.
Checks that the underlying value is String | SafeString, and returns its value. Raises otherwise.
Checks that the underlaying value is a Time object and retuns its value. Raises otherwise.
Returns an iterator for the underlying value if it is an Iterable, String or Undefined
which iterates through the items as Value.
Assumes the underlying value is an Iterable and yields each
of the elements or key/values, always as Value.
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.
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.
Returns an iterator for the underlying value if it is an Iterable, String or Undefined
which iterates through the items as Value.
Assumes the underlying value is an Iterable and yields each
of the elements or key/values, always as Value.
Returns true if the value is a sequence.
TODO Improve implementation based on crinja_item
Returns an Array with all the elements in the collection.
(1..5).to_a # => [1, 2, 3, 4, 5]