Chem::Metadata
Inherits Iterable / Enumerable / Reference / Object
A Metadata is a hash-like container that holds metadata about an
object. It maps a property name (as string) to a primitive value
(integer, float, string, or bool) or an array of them. Most of the
functionality mirrors that of a Hash, where some specific methods
like Hash#dig or default values are excluded.
Values are stored as Any instances, which a thin wrapper for a
dynamically-typed primitive value and it offers convenient #as_*
cast methods.
Examples
metadata = Chem::Metadata.new
# four data types are supported
metadata["str"] = "Foo"
metadata["int"] = 123
metadata["float"] = 1.234
metadata["bool"] = true
# metadata["x"] = /[a-z]/ # fails to compile
# (base type) arrays are also supported
metadata["array_of_int"] = [1, 2, 3]
metadata["array_of_string"] = %w(1 2 3)
metadata["nested_array"] = [[1, 2], [3]]
# metadata["mixed_array"] = [1, "2", true] # does not compile
Values are stored as Any instances. Use #as_* cast methods get the
actual value.
metadata["str"] # => Chem::Metadata::Any("Foo")
metadata["str"].as_s.upcase # => "FOO"
metadata["str"].as_i? # => nil
metadata["str"].as_i # raises TypeCastError
metadata["str"].as_a # raises TypeCastError
Arrays are returned as Array(Any). Use Array#map(&.as_*) or
#as_a(type) to get a typed array.
metadata["array_of_int"].as_a # => [Chem::Metadata::Any(1), ...]
metadata["array_of_int"].as_a.map(&.as_i) # => [1, 2, 3]
metadata["array_of_int"].as_a(Int32) # => [1, 2, 3]
metadata["nested_array"].as_2a(Int32) # => [[1, 2], [3]]
Instance methods
Deletes the key-value pair and returns the value. Returns nil if
key does not exist.
Deletes the key-value pair and returns the value. Yields key and returns the value returned by the given block if key does not exist.
Yields each key to the given block.
The enumeration follows the order the keys were inserted.
Yields each value to the given block.
The enumeration follows the order the keys were inserted.
Returns the value if the given key exists, else default.
Returns the value if the given key exists, else the value returned by the given block invoked with key.
Returns true if any key is associated with value, else
false.
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Returns a key with the given value. Raises KeyError if no key is
associated with value.
Returns a key with the given value. Yields value to the given block and returns the returned value if no key is associated with value.
Returns a key with the given value. Yields value to the given block and returns the returned value if no key is associated with value.
Returns a key with the given value. Yields value to the given block and returns the returned value if no key is associated with value.
Returns a key with the given value. Yields value to the given block and returns the returned value if no key is associated with value.
Returns a key with the given value, or nil if no key is
associated with value.
Adds the contents of other to this metadata. Existing entries will be replaced with those in otherยจ.
Adds the contents of other to this metadata. If a key exists in
both hashes, the given block is called with the key, the value in
self and the value in other, and the returned value will be set.
Appends a short String representation of this object which includes its class name and its object address.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).to_s # => #<Person:0x10a199f20>
Yields the value for the given key and updates it with the value
returned by the given block. Raises KeyError if key does not
exist.
It returns the value used as input for the given block (i.e., the old value).
Returns the values for the given keys. Raises KeyError if a key
does not exist.
Values are returned in the same order of the keys.
Returns the values for the given keys. Raises KeyError if a key
does not exist.
Values are returned in the same order of the keys.