class Hamilton::Storage

Overview

Storage class for Hamilton::Context. Basically, it's a hidden Hash(Symbol, Hamilton::Any) with some methods being overridden (same behavior as in Hash) without the need for explicit convertion values to Hamilton::Any.

Defined in:

storage.cr

Constructors

Instance Method Summary

Instance methods inherited from class Reference

==(other : Hamilton::Any) ==

Instance methods inherited from class Object

===(other : Hamilton::Any) ===

Constructor Detail

def self.new #

Create an empty Hamilton::Storage.


Instance Method Detail

def [](key : Symbol) #

Get value for the key. If key does not exist, raises a KeyError exception.


def []=(key : Symbol, value) #

Assignes value to a key. Raises an ArgumentError if something goes wrong (wrong type of value).


def []?(key : Symbol) #

Get value for the key, or nil if it doesn't exist.


def clear : self #

Empties the storage and returns it.


def delete(key : Symbol) #

Deletes the key-value pair and returns the value, otherwise returns nil.


def delete(key : Symbol, & : Symbol -> ) #

Deletes the key-value pair and returns the value, else yields key with given block.


def each(&block : Tuple(Symbol, Hamilton::Any) -> ) : Nil #

def empty? #

Returns true when hash contains no key-value pairs.


def fetch(key : Symbol, default) #

Returns the value for the key, or when not found the value given by default. Raises an ArgumentError if something goes wrong (wrong type of default).


def fetch(key : Symbol, &) #

Returns the value for the key, or when not found the value returned by the given block invoked with key. Raises an ArgumentError if something goes wrong (wrong type of block's result).


def has_key?(key : Symbol) #

Check if there is a key in the storage.


def keys #

Get all keys in the storage.


def put(key : Symbol, value, &) #

Tries to set value to a key. Raises an ArgumentError if something goes wrong (wrong type of value).

If a value already exists for key, that (old) value is returned. Otherwise the given block is invoked with key and its value is returned.


def put_if_absent(key : Symbol, value) #

Tries to set value to a key if it's absent yet. Raises an ArgumentError if something goes wrong (wrong type of value).

If a value already exists for key, that (old) value is returned. Otherwise value is returned.


def put_if_absent(key : Symbol, & : Symbol -> ) #

Tries to set the result of the given block to a key if it's absent yet. Raises an ArgumentError if something goes wrong (wrong type of block's result).

If a value already exists for key, that (old) value is returned. Otherwise the given block is invoked with key and its value is returned.


def size #

Get size of the storage.


def to_a #

Returns an Array of Tuple(Symbol, Hamilton::Any) with keys and values belonging to this Storage.


def values #

Get all values in the storage.