abstract class AnyHash(K, V)

Overview

Recursive wrapper for Hash(K, V).

Holds the given V values alone or recursively inside of a container type like Array, Set or another Hash.

NOTE Performs deep cast of container types:

Included Modules

Direct Known Subclasses

Defined in:

any_hash.cr
any_hash/json.cr
any_hash/version.cr

Constant Summary

CHAINED_HASH_METHODS = ["reject!", "select!", "compact!", "clear"] of ::String

List of methods delegated to the underlying Hash returning self.

VERSION = {{ (`shards version \"/srv/crystaldoc.info/github-Sija-any_hash.cr-v0.2.5/src/any_hash\"`).chomp.stringify }}

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(hash = nil) #

Initializes AnyHash with the given hash object.

NOTE AnyHash | Hash objects are passed by reference.


[View source]

Class Method Detail

def self.deep_cast_value(value) #

Deep casts value to the V type.

ameba:disable Metrics/CyclomaticComplexity


[View source]

Instance Method Detail

def ==(other : self) #

ditto


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

ditto


[View source]
def ==(arg) #

See Hash#==.


[View source]
def ===(arg) #

See Hash#==.


[View source]
def [](*keys) #

See #dig.


[View source]
def []=(key, value) #

See Hash#[]=.


[View source]
def []=(*args) #

See Hash#[]=.


[View source]
def []?(*keys) #

See #dig?.


[View source]
def clone #

Returns a new AnyHash, with a deep copy of the underlying Hash.

Use #dup if you want a shallow copy.


[View source]
def dig(keys : Enumerable) #

Extracts the nested value specified by the sequence of keys objects by calling #[] at each step, raises if any intermediate step is nil.


[View source]
def dig(*keys) #

ditto


[View source]
def dig?(keys : Enumerable) #

Extracts the nested value specified by the sequence of keys objects by calling #[]? at each step, returns nil if any intermediate step is nil.


[View source]
def dig?(*keys) #

ditto


[View source]
def dup #

Returns a new AnyHash, with a shallow copy of the underlying Hash.

Use #clone if you want a deep copy.


[View source]
def each(*args, **options) #

See Hash#each.


[View source]
def each(*args, **options, &) #

See Hash#each.


[View source]
def merge(*values, **options) #

Performs deep merge of self with given other values and returns copy of self.

See Hash#merge.


[View source]
def merge(*values, **options, &) #

ditto


[View source]
def merge!(*values, **options) #

Destructive version of #merge.


[View source]
def merge!(*values, **options, &) #

ditto


[View source]
def replace(other) #

Replaces underlying Hash with given other.

Returns self.


[View source]
def reverse_merge(other = nil, *values, **options) #

Merges the caller into other. For example,

options = options.reverse_merge(size: 25, velocity: 10)

is equivalent to

options = { size: 25, velocity: 10 }.merge(options)

This is particularly useful for initializing an options hash with default values.


[View source]
def reverse_merge(other = nil, *values, **options, &) #

ditto


[View source]
def reverse_merge!(other = nil, *values, **options) #

Destructive version of #reverse_merge.


[View source]
def reverse_merge!(other = nil, *values, **options, &) #

ditto


[View source]
def to_h(*args, **options) #

See Hash#to_h.


[View source]
def to_h(*args, **options, &) #

See Hash#to_h.


[View source]

Macro Detail

macro define_new(klass, key, value) #

Defines new klass klass inheriting from AnyHash with given key keys and recursive value types.

AnyHash.define_new klass: :StringHash,
  key: String,
  value: String | Symbol

StringHash::Key   # => String
StringHash::Value # => String | Symbol

StringHash.new({"foo" => {:bar, "baz"}})
# => #<StringHash:0x100d5e180 @__hash__={"foo" => [:bar, "baz"]}>

See AnyHash.


[View source]
macro method_missing(call) #

[View source]