Class: IndifferentHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/triton/indifferent_hash.rb

Overview

This class has been permamently borrowed from Sinatra, that repo is MIT licensed and has these copyright notices:

Copyright (c) 2007, 2008, 2009 Blake Mizerany
Copyright (c) 2010-2017 Konstantin Haase
Copyright (c) 2015-2017 Zachary Scott

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ IndifferentHash

Returns a new instance of IndifferentHash.



14
15
16
# File 'lib/triton/indifferent_hash.rb', line 14

def initialize(*args)
  super(*args.map(&method(:convert_value)))
end

Class Method Details

.[](*args) ⇒ Object



10
11
12
# File 'lib/triton/indifferent_hash.rb', line 10

def self.[](*args)
  new.merge!(Hash[*args])
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
# File 'lib/triton/indifferent_hash.rb', line 38

def [](key)
  super(convert_key(key))
end

#[]=(key, value) ⇒ Object Also known as: store



42
43
44
# File 'lib/triton/indifferent_hash.rb', line 42

def []=(key, value)
  super(convert_key(key), convert_value(value))
end

#assoc(key) ⇒ Object



26
27
28
# File 'lib/triton/indifferent_hash.rb', line 26

def assoc(key)
  super(convert_key(key))
end

#default(*args) ⇒ Object



18
19
20
# File 'lib/triton/indifferent_hash.rb', line 18

def default(*args)
  super(*args.map(&method(:convert_key)))
end

#default=(value) ⇒ Object



22
23
24
# File 'lib/triton/indifferent_hash.rb', line 22

def default=(value)
  super(convert_value(value))
end

#delete(key) ⇒ Object



66
67
68
# File 'lib/triton/indifferent_hash.rb', line 66

def delete(key)
  super(convert_key(key))
end

#dig(key, *other_keys) ⇒ Object



70
71
72
# File 'lib/triton/indifferent_hash.rb', line 70

def dig(key, *other_keys)
  super(convert_key(key), *other_keys)
end

#fetch(key, *args) ⇒ Object



34
35
36
# File 'lib/triton/indifferent_hash.rb', line 34

def fetch(key, *args)
  super(convert_key(key), *args.map(&method(:convert_value)))
end

#fetch_values(*keys) ⇒ Object



74
75
76
# File 'lib/triton/indifferent_hash.rb', line 74

def fetch_values(*keys)
  super(*keys.map(&method(:convert_key)))
end

#key(value) ⇒ Object



48
49
50
# File 'lib/triton/indifferent_hash.rb', line 48

def key(value)
  super(convert_value(value))
end

#key?(key) ⇒ Boolean Also known as: has_key?, include?, member?

Returns:

  • (Boolean)


52
53
54
# File 'lib/triton/indifferent_hash.rb', line 52

def key?(key)
  super(convert_key(key))
end

#merge(other_hash, &block) ⇒ Object



96
97
98
# File 'lib/triton/indifferent_hash.rb', line 96

def merge(other_hash, &block)
  dup.merge!(other_hash, &block)
end

#merge!(other_hash) ⇒ Object Also known as: update



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/triton/indifferent_hash.rb', line 82

def merge!(other_hash)
  return super if other_hash.is_a?(self.class)

  other_hash.each_pair do |key, value|
    key = convert_key(key)
    value = yield(key, self[key], value) if block_given? && key?(key)
    self[key] = convert_value(value)
  end

  self
end

#rassoc(value) ⇒ Object



30
31
32
# File 'lib/triton/indifferent_hash.rb', line 30

def rassoc(value)
  super(convert_value(value))
end

#replace(other_hash) ⇒ Object



100
101
102
# File 'lib/triton/indifferent_hash.rb', line 100

def replace(other_hash)
  super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
end

#value?(value) ⇒ Boolean Also known as: has_value?

Returns:

  • (Boolean)


60
61
62
# File 'lib/triton/indifferent_hash.rb', line 60

def value?(value)
  super(convert_value(value))
end

#values_at(*keys) ⇒ Object



78
79
80
# File 'lib/triton/indifferent_hash.rb', line 78

def values_at(*keys)
  super(*keys.map(&method(:convert_key)))
end