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
true
if bothself
and other's raw object are equal. -
#==(other)
Returns
true
if the raw object is equal to other. -
#[](index : Int) : Value
Assumes the underlying value is an
Indexable
orString
returns 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
Indexable
orString
and returns the element at the given index, ornil
if out of bounds. -
#[]?(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. -
#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::Proc
and 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
Indexable
and retuns its value. -
#as_iterable
Checks that the underlaying value is a
Iterable
and 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_s
instead -
#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
Time
object and retuns its value. - #as_undefined
-
#callable?
Returns
true
if this value is aCallable
- #each
-
#each(&)
Assumes the underlying value is an
Iterable
and yields each of the elements or key/values, always asValue
. -
#first
Assumes the underlying value is an
Iterable
,Hash
orString
and returns the first item in the list or the first character of the string. -
#indexable?
Returns
true
if the value is a list (Array
). -
#iterable?
Returns
true
if the value is iteraable. -
#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. -
#mapping?
Returns
true
if the object is a mapping (Hash or Crinja::Object). -
#none?
Returns
true
if the value is nil. -
#number?
Returns
true
if 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
Iterable
and yields each of the elements or key/values, always asValue
. -
#sameas?(other)
Returns true if
-
#sequence?
Returns
true
if the value is a sequence. -
#size : Int
Assumes the underlying value responds to
#size
and returns its size. -
#string?
Returns
true
if the value is a string. -
#time?
Returns
true
if the value is a time object. -
#to_a
Returns an
Array
with 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
true
unless this value isfalse
,0
,nil
or#undefined?
-
#undefined?
Returns
true
if 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]