Class: InsensitiveHash
- Defined in:
- lib/insensitive_hash/version.rb,
lib/insensitive_hash/insensitive_hash.rb
Defined Under Namespace
Classes: KeyClashError
Constant Summary collapse
- VERSION =
"0.3.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!)
- #replace(other) ⇒ Object
-
#safe=(s) ⇒ 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.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 5 def initialize default = nil, &block if block_given? raise ArgumentError.new('wrong number of arguments') unless default.nil? super &block else super end @key_map = {} @safe = false end |
Class Method Details
.[](*init) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 37 def self.[] *init h = Hash[*init] InsensitiveHash.new.tap do |ih| ih.merge! h end end |
Instance Method Details
#[]=(key, value) ⇒ Object Also known as: store
52 53 54 55 56 57 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 52 def []= key, value delete key ekey = encode key @key_map[ekey] = key super key, value end |
#clear ⇒ Object
81 82 83 84 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 81 def clear @key_map.clear super end |
#clone ⇒ Object
119 120 121 122 123 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 119 def clone super.tap { |copy| copy.instance_variable_set :@key_map, @key_map.dup } end |
#delete(key, &block) ⇒ Object
77 78 79 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 77 def delete key, &block super lookup_key(key, true), &block end |
#dup ⇒ Object
113 114 115 116 117 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 113 def dup super.tap { |copy| copy.instance_variable_set :@key_map, @key_map.dup } end |
#fetch(*args, &block) ⇒ Object
108 109 110 111 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 108 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
69 70 71 72 73 74 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 69 def merge other_hash InsensitiveHash.new.tap do |ih| ih.replace self ih.merge! other_hash end end |
#merge!(other_hash) ⇒ Object Also known as: update!
60 61 62 63 64 65 66 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 60 def merge! other_hash detect_clash other_hash other_hash.each do |key, value| deep_set key, value end self end |
#replace(other) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 86 def replace other super other self.safe = other.respond_to?(:safe?) ? other.safe? : safe? @key_map.clear self.each do |k, v| ekey = encode k @key_map[ekey] = k end end |
#safe=(s) ⇒ Boolean
Sets whether to detect key clashes
20 21 22 23 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 20 def safe= s raise ArgumentError.new("Neither true nor false") unless [true, false].include?(s) @safe = s end |
#safe? ⇒ Boolean
Returns Key-clash detection enabled?.
26 27 28 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 26 def safe? @safe end |
#shift ⇒ Object
98 99 100 101 102 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 98 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
32 33 34 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 32 def to_hash {}.merge self end |
#values_at(*keys) ⇒ Object
104 105 106 |
# File 'lib/insensitive_hash/insensitive_hash.rb', line 104 def values_at *keys keys.map { |k| self[k] } end |