module ActiveObject::Hash

Public Instance Methods

assert_valid_keys(*valid_keys) click to toggle source
# File lib/active_object/hash.rb, line 8
def assert_valid_keys(*valid_keys)
  valid_keys.flatten!

  each_key do |key|
    next if valid_keys.include?(key)

    raise ArgumentError,
          "Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  end
end
assert_valid_keys!(*valid_keys) click to toggle source
# File lib/active_object/hash.rb, line 19
def assert_valid_keys!(*valid_keys)
  if empty?
    raise ArgumentError,
          "Empty hash. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  else
    assert_valid_keys(*valid_keys)
  end
end
bury(*args) click to toggle source

rubocop:disable Style/GuardClause

# File lib/active_object/hash.rb, line 29
def bury(*args)
  if args.count < 2
    raise ArgumentError, '2 or more arguments required'
  elsif args.count == 2
    self[args[0]] = args[1]
  else
    arg = args.shift

    self[arg] = {} unless self[arg]
    self[arg].bury(*args) unless args.empty?
  end

  self
end
collect_keys() { |key| ... } click to toggle source

rubocop:disable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 54
def collect_keys(&block)
  return enum_for(:collect_keys) unless block_given?

  collect { |key, _| yield(key) }
end
collect_values() { |val| ... } click to toggle source
# File lib/active_object/hash.rb, line 60
def collect_values(&block)
  return enum_for(:collect_values) unless block_given?

  collect { |_, val| yield(val) }
end
compact() click to toggle source

rubocop:enable Style/GuardClause

# File lib/active_object/hash.rb, line 45
def compact
  select { |_, val| !val.nil? }
end
compact!() click to toggle source
# File lib/active_object/hash.rb, line 49
def compact!
  reject! { |_, val| val.nil? }
end
deep_merge(other_hash) { |block| ... } click to toggle source

rubocop:enable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 67
def deep_merge(other_hash, &block)
  dup.deep_merge!(other_hash, yield(block))
end
deep_merge!(other_hash) { |block| ... } click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/active_object/hash.rb, line 72
def deep_merge!(other_hash, &block)
  other_hash.each_pair do |current_key, other_value|
    this_value = self[current_key]

    self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
                          this_value.deep_merge(other_value, yield(block))
                        elsif block_given? && key?(current_key)
                          yield(current_key, this_value, other_value)
                        else
                          other_value
                        end
  end

  self
end
demote(key) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/active_object/hash.rb, line 89
def demote(key)
  return self unless key?(key)

  merge(key => delete(key))
end
demote!(key) click to toggle source
# File lib/active_object/hash.rb, line 95
def demote!(key)
  replace(demote(key))
end
denillify(value = 0) click to toggle source
# File lib/active_object/hash.rb, line 99
def denillify(value = 0)
  each { |key, val| self[key] = val.nil? ? value : val }
end
denillify!(value = 0) click to toggle source
# File lib/active_object/hash.rb, line 103
def denillify!(value = 0)
  replace(denillify(value))
end
dig(key, *rest) click to toggle source
# File lib/active_object/hash.rb, line 107
def dig(key, *rest)
  value = (self[key] rescue nil)

  return if value.nil?
  return value if rest.empty?
  return value.dig(*rest) if value.respond_to?(:dig)
end
except(*keys) click to toggle source
# File lib/active_object/hash.rb, line 115
def except(*keys)
  dup.except!(*keys)
end
except!(*keys) click to toggle source
# File lib/active_object/hash.rb, line 119
def except!(*keys)
  keys.flatten.each { |key| delete(key) }
  self
end
extract!(*keys) click to toggle source
# File lib/active_object/hash.rb, line 124
def extract!(*keys)
  keys.each_with_object({}) { |key, hash| hash[key] = delete(key) if key?(key) }
end
hmap(&block) click to toggle source
# File lib/active_object/hash.rb, line 128
def hmap(&block)
  dup.hmap!(&block)
end
hmap!() { |key, val| ... } click to toggle source

rubocop:disable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 133
def hmap!(&block)
  inject({}) { |hash, (key, val)| hash.merge(yield(key, val)) }
end
nillify() click to toggle source

rubocop:enable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 138
def nillify
  dup.nillify!
end
nillify!() click to toggle source
# File lib/active_object/hash.rb, line 142
def nillify!
  each do |key, val|
    self[key] = nil if !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?)
  end
end
only(*keys) click to toggle source
# File lib/active_object/hash.rb, line 148
def only(*keys)
  dup.only!(*keys)
end
only!(*keys) click to toggle source
# File lib/active_object/hash.rb, line 152
def only!(*keys)
  hash = {}
  keys.flatten.each { |key| hash[key] = self[key] if key?(key) }
  replace(hash)
end
only_fill(*keys, placeholder: nil) click to toggle source
# File lib/active_object/hash.rb, line 158
def only_fill(*keys, placeholder: nil)
  dup.only_fill!(*keys, placeholder: placeholder)
end
only_fill!(*keys, placeholder: nil) click to toggle source
# File lib/active_object/hash.rb, line 162
def only_fill!(*keys, placeholder: nil)
  hash = {}
  keys.flatten.each { |key| hash[key] = key?(key) ? self[key] : placeholder }
  replace(hash)
end
pair?(key, value) click to toggle source
# File lib/active_object/hash.rb, line 168
def pair?(key, value)
  self[key] == value
end
promote(key) click to toggle source
# File lib/active_object/hash.rb, line 172
def promote(key)
  return self unless key?(key)

  { key => delete(key) }.merge(self)
end
promote!(key) click to toggle source
# File lib/active_object/hash.rb, line 178
def promote!(key)
  replace(promote(key))
end
rename_keys(*keys) click to toggle source
# File lib/active_object/hash.rb, line 182
def rename_keys(*keys)
  dup.rename_keys!(*keys)
end
rename_keys!(*keys) click to toggle source
# File lib/active_object/hash.rb, line 186
def rename_keys!(*keys)
  keys = ::Hash[*keys.flatten]
  keys.each { |key, val| self[val] = delete(key) if self[key] }
  self
end
reverse_merge(other_hash) click to toggle source
# File lib/active_object/hash.rb, line 192
def reverse_merge(other_hash)
  other_hash.merge(self)
end
reverse_merge!(other_hash) click to toggle source
# File lib/active_object/hash.rb, line 196
def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end
sample() click to toggle source
# File lib/active_object/hash.rb, line 200
def sample
  key = sample_key
  [key, fetch(key)]
end
sample!() click to toggle source
# File lib/active_object/hash.rb, line 205
def sample!
  key, value = sample
  delete(key)
  [key, value]
end
sample_key() click to toggle source
# File lib/active_object/hash.rb, line 211
def sample_key
  hash_keys = keys
  hash_keys.at(::Random.rand(hash_keys.length - 1))
end
sample_key!() click to toggle source
# File lib/active_object/hash.rb, line 216
def sample_key!
  key, = sample
  delete(key)
  key
end
sample_value() click to toggle source
# File lib/active_object/hash.rb, line 222
def sample_value
  fetch(sample_key)
end
sample_value!() click to toggle source
# File lib/active_object/hash.rb, line 226
def sample_value!
  key, value = sample
  delete(key)
  value
end
shuffle() click to toggle source
# File lib/active_object/hash.rb, line 232
def shuffle
  ::Hash[to_a.sample(length)]
end
shuffle!() click to toggle source
# File lib/active_object/hash.rb, line 236
def shuffle!
  replace(shuffle)
end
slice(*keys) click to toggle source
# File lib/active_object/hash.rb, line 240
def slice(*keys)
  keys.flatten.each_with_object({}) { |key, hsh| hsh[key] = self[key] if key?(key) }
end
slice!(*keys) click to toggle source
# File lib/active_object/hash.rb, line 244
def slice!(*keys)
  omit = slice(*self.keys - keys)
  hash = slice(*keys)

  hash.default = default
  hash.default_proc = default_proc if default_proc

  replace(hash)
  omit
end
stringify_keys() click to toggle source
# File lib/active_object/hash.rb, line 255
def stringify_keys
  dup.stringify_keys!
end
stringify_keys!() click to toggle source
# File lib/active_object/hash.rb, line 259
def stringify_keys!
  each_with_object({}) { |(key, val), options| options[key.to_s] = val }
end
strip() click to toggle source
# File lib/active_object/hash.rb, line 263
def strip
  select { |_, val| !val.blank? }
end
strip!() click to toggle source
# File lib/active_object/hash.rb, line 267
def strip!
  reject! { |_, val| val.blank? }
end
symbolize_and_underscore_keys() click to toggle source
# File lib/active_object/hash.rb, line 279
def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end
symbolize_and_underscore_keys!() click to toggle source
# File lib/active_object/hash.rb, line 283
def symbolize_and_underscore_keys!
  each_with_object({}) do |(key, val), options|
    options[(key.to_s.tr(' ', '_').underscore.to_sym rescue key) || key] = val
  end
end
symbolize_keys() click to toggle source
# File lib/active_object/hash.rb, line 271
def symbolize_keys
  dup.symbolize_keys!
end
symbolize_keys!() click to toggle source
# File lib/active_object/hash.rb, line 275
def symbolize_keys!
  each_with_object({}) { |(key, val), options| options[(key.to_sym rescue key) || key] = val }
end
to_o() click to toggle source
# File lib/active_object/hash.rb, line 289
def to_o
  JSON.parse(to_json, object_class: OpenStruct)
end
transform_keys(&block) click to toggle source
# File lib/active_object/hash.rb, line 293
def transform_keys(&block)
  dup.transform_keys!(&block)
end
transform_keys!() { |key| ... } click to toggle source

rubocop:disable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 298
def transform_keys!(&block)
  return enum_for(:transform_keys!) unless block_given?

  each_key { |key| self[yield(key)] = delete(key) }
  self
end
transform_values(&block) click to toggle source

rubocop:enable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 306
def transform_values(&block)
  dup.transform_values!(&block)
end
transform_values!() { |val| ... } click to toggle source

rubocop:disable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 311
def transform_values!(&block)
  return enum_for(:transform_values!) unless block_given?

  each { |key, val| self[key] = yield(val) }
end
vacant?(key) click to toggle source

rubocop:enable Lint/UnusedMethodArgument

# File lib/active_object/hash.rb, line 318
def vacant?(key)
  self[key].blank?
end