Class: InsensitiveHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/insensitive_hash/version.rb,
lib/insensitive_hash/insensitive_hash.rb

Overview

Insensitive Hash.

Author:

Defined Under Namespace

Classes: KeyClashError

Constant Summary collapse

VERSION =
'0.4.0'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#insensitive

Constructor Details

#initialize(default = nil, &block) ⇒ InsensitiveHash

Returns a new instance of InsensitiveHash.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/insensitive_hash/insensitive_hash.rb', line 9

def initialize(default = nil, &block)
  if block_given?
    raise ArgumentError, 'wrong number of arguments' unless default.nil?

    super(&block)
  else
    super
  end

  @key_map = {}
  @safe    = false
end

Class Method Details

.[](*init) ⇒ Object

See Also:



44
45
46
47
48
49
# File 'lib/insensitive_hash/insensitive_hash.rb', line 44

def self.[](*init)
  h = Hash[*init]
  new.tap do |ih|
    ih.merge_recursive! h
  end
end

Instance Method Details

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

See Also:



60
61
62
63
64
65
# File 'lib/insensitive_hash/insensitive_hash.rb', line 60

def []=(key, value)
  delete key
  ekey = encode key
  @key_map[ekey] = key
  super key, value
end

#clearObject

See Also:



105
106
107
108
# File 'lib/insensitive_hash/insensitive_hash.rb', line 105

def clear
  @key_map.clear
  super
end

#cloneObject

See Also:



147
148
149
# File 'lib/insensitive_hash/insensitive_hash.rb', line 147

def clone
  super.tap { |copy| copy.instance_variable_set :@key_map, @key_map.dup }
end

#delete(key, &block) ⇒ Object

See Also:



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

def delete(key, &block)
  super lookup_key(key, true), &block
end

#dupObject

See Also:



142
143
144
# File 'lib/insensitive_hash/insensitive_hash.rb', line 142

def dup
  super.tap { |copy| copy.instance_variable_set :@key_map, @key_map.dup }
end

#fetch(*args, &block) ⇒ Object

See Also:



136
137
138
139
# File 'lib/insensitive_hash/insensitive_hash.rb', line 136

def fetch(*args, &block)
  args[0] = lookup_key(args[0]) if args.first
  super(*args, &block)
end

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

See Also:



79
80
81
82
83
84
# File 'lib/insensitive_hash/insensitive_hash.rb', line 79

def merge(other_hash)
  self.class.new.tap do |ih|
    ih.replace self
    ih.merge! other_hash
  end
end

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

See Also:



69
70
71
72
73
74
75
# File 'lib/insensitive_hash/insensitive_hash.rb', line 69

def merge!(other_hash)
  detect_clash other_hash
  other_hash.each do |key, value|
    store key, value
  end
  self
end

#merge_recursive!(other_hash) ⇒ self Also known as: update_recursive!

Merge another hash recursively.

Parameters:

Returns:

  • (self)


90
91
92
93
94
95
96
# File 'lib/insensitive_hash/insensitive_hash.rb', line 90

def merge_recursive!(other_hash)
  detect_clash other_hash
  other_hash.each do |key, value|
    deep_set key, value
  end
  self
end

#replace(other) ⇒ Object

See Also:



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/insensitive_hash/insensitive_hash.rb', line 111

def replace(other)
  super other

  self.safe = other.respond_to?(:safe?) ? other.safe? : safe?

  @key_map.clear
  each do |k, _v|
    ekey = encode k
    @key_map[ekey] = k
  end
end

#safe=(safe_val) ⇒ Boolean

Sets whether to detect key clashes

Parameters:

  • (Boolean)

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


25
26
27
28
29
# File 'lib/insensitive_hash/insensitive_hash.rb', line 25

def safe=(safe_val)
  raise ArgumentError, 'Neither true nor false' unless [true, false].include?(safe_val)

  @safe = safe_val
end

#safe?Boolean

Returns Key-clash detection enabled?.

Returns:

  • (Boolean)

    Key-clash detection enabled?



32
33
34
# File 'lib/insensitive_hash/insensitive_hash.rb', line 32

def safe?
  @safe
end

#shiftObject

See Also:



124
125
126
127
128
# File 'lib/insensitive_hash/insensitive_hash.rb', line 124

def shift
  super.tap do |ret|
    @key_map.delete_if { |_k, v| v == ret.first }
  end
end

#to_hashHash Also known as: sensitive

Returns a normal, sensitive Hash

Returns:



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

def to_hash
  {}.merge self
end

#values_at(*keys) ⇒ Object

See Also:



131
132
133
# File 'lib/insensitive_hash/insensitive_hash.rb', line 131

def values_at(*keys)
  keys.map { |k| self[k] }
end