class Hash::Normalized

Public Class Methods

new(hash={}, &block) click to toggle source
# File lib/hash_ext/normalized.rb, line 14
def initialize(hash={}, &block)
  @normalization_block = block
  update hash
end
subclass(&block) click to toggle source
Calls superclass method
# File lib/hash_ext/normalized.rb, line 6
def self.subclass(&block)
  Class.new(self) do
    define_method :initialize do |hash={}|
      super hash, &block
    end
  end
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/hash_ext/normalized.rb, line 19
def [](key)
  super normalize_key(key)
end
[]=(key, value) click to toggle source
Calls superclass method
# File lib/hash_ext/normalized.rb, line 23
def []=(key, value)
  super normalize_key(key), normalize_value(value)
end
Also aliased as: store
delete(key) click to toggle source
Calls superclass method
# File lib/hash_ext/normalized.rb, line 28
def delete(key)
  super normalize_key(key)
end
dig(*keys) click to toggle source
# File lib/hash_ext/normalized.rb, line 56
def dig(*keys)
  normalized_keys = keys.map { |k| normalize_key(k) }
  normalized_keys.inject(self) do |target, key|
    target ? target[key] : nil
  end
end
fetch(key, *args, &block) click to toggle source
Calls superclass method
# File lib/hash_ext/normalized.rb, line 52
def fetch(key, *args, &block)
  super normalize_key(key), *args, &block
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source
Calls superclass method
# File lib/hash_ext/normalized.rb, line 45
def key?(key)
  super normalize_key(key)
end
Also aliased as: include?, has_key?, member?
member?(key)
Alias for: key?
merge(hash, &block) click to toggle source
# File lib/hash_ext/normalized.rb, line 41
def merge(hash, &block)
  dup.update hash, &block
end
merge!(hash, &block)
Alias for: update
store(key, value)
Alias for: []=
to_h() click to toggle source
# File lib/hash_ext/normalized.rb, line 63
def to_h
  each_with_object({}) do |(key, value), hash|
    hash[key] = value_to_h value
  end
end
update(hash, &block) click to toggle source
# File lib/hash_ext/normalized.rb, line 32
def update(hash, &block)
  hash.each do |key, value|
    new_val = block && key?(key) ? block.call(key, self[key], value) : value
    store key, new_val
  end
  self
end
Also aliased as: merge!

Private Instance Methods

normalize_key(key) click to toggle source
# File lib/hash_ext/normalized.rb, line 71
def normalize_key(key)
  @normalization_block.call key
end
normalize_value(value) click to toggle source
# File lib/hash_ext/normalized.rb, line 75
def normalize_value(value)
  if value.is_a? self.class
    value
  elsif value.kind_of? Hash
    self.class.new value, &@normalization_block
  elsif value.kind_of? Array
    value.map { |v| normalize_value v }
  else
    value
  end
end
value_to_h(value) click to toggle source
# File lib/hash_ext/normalized.rb, line 87
def value_to_h(value)
  if value.kind_of? Hash
    value.to_h
  elsif value.kind_of? Array
    value.map { |v| value_to_h v }
  else
    value
  end
end