Class: InsensitiveHash
- Defined in:
- lib/insensitive_hash/version.rb,
lib/insensitive_hash/insensitive_hash.rb
Overview
Insensitive Hash.
Defined Under Namespace
Classes: KeyClashError
Constant Summary collapse
- VERSION =
'0.4.0'
Class Method Summary collapse
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object (also: #store)
- #clear ⇒ Object
- #clone ⇒ Object
- #delete(key, &block) ⇒ Object
- #dup ⇒ Object
- #fetch(*args, &block) ⇒ Object
-
#initialize(default = nil, &block) ⇒ InsensitiveHash
constructor
A new instance of InsensitiveHash.
- #merge(other_hash) ⇒ Object (also: #update)
- #merge!(other_hash) ⇒ Object (also: #update!)
-
#merge_recursive!(other_hash) ⇒ self
(also: #update_recursive!)
Merge another hash recursively.
- #replace(other) ⇒ Object
-
#safe=(safe_val) ⇒ Boolean
Sets whether to detect key clashes.
-
#safe? ⇒ Boolean
Key-clash detection enabled?.
- #shift ⇒ Object
-
#to_hash ⇒ Hash
(also: #sensitive)
Returns a normal, sensitive Hash.
- #values_at(*keys) ⇒ Object
Methods inherited from Hash
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
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
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 |
#clear ⇒ Object
105 106 107 108 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 105 def clear @key_map.clear super end |
#clone ⇒ Object
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
100 101 102 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 100 def delete(key, &block) super lookup_key(key, true), &block end |
#dup ⇒ Object
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
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
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!
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.
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
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
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?.
32 33 34 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 32 def safe? @safe end |
#shift ⇒ Object
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_hash ⇒ Hash Also known as: sensitive
Returns a normal, sensitive Hash
38 39 40 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 38 def to_hash {}.merge self end |
#values_at(*keys) ⇒ Object
131 132 133 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 131 def values_at(*keys) keys.map { |k| self[k] } end |