class HashEx::Base
Public Class Methods
[](*args)
click to toggle source
# File lib/HashEx.rb, line 23 def self.[](*args) new.merge!(Hash[*args]) end
new(constructor = {})
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 10 def initialize(constructor = {}) if constructor.respond_to?(:to_hash) super() update(constructor) hash = constructor.to_hash self.default = hash.default if hash.default self.default_proc = hash.default_proc if hash.default_proc else super(constructor) end end
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 44 def [](key) super(convert_key(key)) end
[]=(key, value)
click to toggle source
# File lib/HashEx.rb, line 52 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end
Also aliased as: regular_writer, store
compact()
click to toggle source
# File lib/HashEx.rb, line 145 def compact dup.tap(&:compact!) end
default(*args)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 81 def default(*args) super(*args.map { |arg| convert_key(arg) }) end
delete(key)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 111 def delete(key) super(convert_key(key)) end
dig(*args)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 75 def dig(*args) args[0] = convert_key(args[0]) if args.size > 0 super(*args) end
fetch(key, *extras)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 48 def fetch(key, *extras) super(convert_key(key), *extras) end
fetch_values(*indices, &block)
click to toggle source
# File lib/HashEx.rb, line 90 def fetch_values(*indices, &block) indices.collect { |key| fetch(key, &block) } end
key?(key)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 36 def key?(key) super(convert_key(key)) end
merge(hash, &block)
click to toggle source
# File lib/HashEx.rb, line 95 def merge(hash, &block) dup.update(hash, &block) end
method_missing(method, *args, &block)
click to toggle source
# File lib/HashEx.rb, line 27 def method_missing(method, *args, &block) key = method.to_s if key[-1] == '=' self[key.chop] = args[0] else self[key] end end
reject(*args, &block)
click to toggle source
# File lib/HashEx.rb, line 120 def reject(*args, &block) return to_enum(:reject) unless block_given? dup.tap { |hash| hash.reject!(*args, &block) } end
replace(other_hash)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 107 def replace(other_hash) super(self.class.new(other_hash)) end
reverse_merge(other_hash)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 99 def reverse_merge(other_hash) super(self.class.new(other_hash)) end
reverse_merge!(other_hash)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 103 def reverse_merge!(other_hash) super(self.class.new(other_hash)) end
select(*args, &block)
click to toggle source
# File lib/HashEx.rb, line 115 def select(*args, &block) return to_enum(:select) unless block_given? dup.tap { |hash| hash.select!(*args, &block) } end
slice(*keys)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 135 def slice(*keys) keys.map! { |key| convert_key(key) } self.class.new(super) end
slice!(*keys)
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 140 def slice!(*keys) keys.map! { |key| convert_key(key) } super end
to_h()
click to toggle source
# File lib/HashEx.rb, line 149 def to_h h = Hash.new set_defaults(h) each do |key, value| h[key] = convert_value(value, revert: true) end h end
transform_keys(*args, &block)
click to toggle source
# File lib/HashEx.rb, line 125 def transform_keys(*args, &block) return to_enum(:transform_keys) unless block_given? dup.tap { |hash| hash.transform_keys!(*args, &block) } end
transform_values(*args, &block)
click to toggle source
# File lib/HashEx.rb, line 130 def transform_values(*args, &block) return to_enum(:transform_values) unless block_given? dup.tap { |hash| hash.transform_values!(*args, &block) } end
update(other_hash) { |convert_key(key), self, value| ... }
click to toggle source
Calls superclass method
# File lib/HashEx.rb, line 58 def update(other_hash) if other_hash.is_a? self.class super(other_hash) else other_hash.to_hash.each_pair do |key, value| if block_given? && key?(key) value = yield(convert_key(key), self[key], value) end regular_writer(convert_key(key), convert_value(value)) end self end end
Also aliased as: regular_update, merge!
values_at(*indices)
click to toggle source
# File lib/HashEx.rb, line 85 def values_at(*indices) indices.collect { |key| self[convert_key(key)] } end
Protected Instance Methods
convert_key(key)
click to toggle source
# File lib/HashEx.rb, line 160 def convert_key(key) raise "Method #convert_key must be rewritten" end
convert_value(value, revert: false)
click to toggle source
# File lib/HashEx.rb, line 164 def convert_value(value, revert: false) if value.is_a? Hash if revert value.to_h else value.is_a?(self.class) ? value : self.class.new(value) end elsif value.is_a? Array value.map { |e| convert_value(e) } else value end end