Module: Common::HashHelpers

Defined in:
lib/common/hash_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deep_compact(hash) ⇒ Object



51
52
53
# File 'lib/common/hash_helpers.rb', line 51

def deep_compact(hash)
  deep_remove_helper(hash, &:nil?)
end

.deep_remove_blanks(hash) ⇒ Object



24
25
26
27
28
# File 'lib/common/hash_helpers.rb', line 24

def deep_remove_blanks(hash)
  deep_remove_helper(hash) do |v|
    v.blank? && v != false
  end
end

.deep_remove_helper(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/common/hash_helpers.rb', line 30

def deep_remove_helper(hash, &)
  delete_if_block = proc do |_k, v|
    case v
    when Hash
      v.delete_if(&delete_if_block)
      nil
    when Array
      v.reject!(&)
      v.each do |item|
        delete_if_block.call(nil, item)
      end

      nil
    else
      yield(v)
    end
  end

  hash.delete_if(&delete_if_block)
end

.deep_transform_parameters!(val) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/common/hash_helpers.rb', line 7

def deep_transform_parameters!(val, &)
  # rails 5.1 no longer has deep_transform_keys!
  # because params are no longer inheriting from HashWithIndifferentAccess
  case val
  when Array
    val.map { |v| deep_transform_parameters!(v, &) }
  when Hash, ActionController::Parameters
    val.keys.each do |k, v = val[k]|
      val.delete(k)
      val[yield(k)] = deep_transform_parameters!(v, &)
    end
    val
  else
    val
  end
end

Instance Method Details

#deep_compact(hash) ⇒ Object (private)



51
52
53
# File 'lib/common/hash_helpers.rb', line 51

def deep_compact(hash)
  deep_remove_helper(hash, &:nil?)
end

#deep_remove_blanks(hash) ⇒ Object (private)



24
25
26
27
28
# File 'lib/common/hash_helpers.rb', line 24

def deep_remove_blanks(hash)
  deep_remove_helper(hash) do |v|
    v.blank? && v != false
  end
end

#deep_remove_helper(hash) ⇒ Object (private)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/common/hash_helpers.rb', line 30

def deep_remove_helper(hash, &)
  delete_if_block = proc do |_k, v|
    case v
    when Hash
      v.delete_if(&delete_if_block)
      nil
    when Array
      v.reject!(&)
      v.each do |item|
        delete_if_block.call(nil, item)
      end

      nil
    else
      yield(v)
    end
  end

  hash.delete_if(&delete_if_block)
end

#deep_transform_parameters!(val) ⇒ Object (private)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/common/hash_helpers.rb', line 7

def deep_transform_parameters!(val, &)
  # rails 5.1 no longer has deep_transform_keys!
  # because params are no longer inheriting from HashWithIndifferentAccess
  case val
  when Array
    val.map { |v| deep_transform_parameters!(v, &) }
  when Hash, ActionController::Parameters
    val.keys.each do |k, v = val[k]|
      val.delete(k)
      val[yield(k)] = deep_transform_parameters!(v, &)
    end
    val
  else
    val
  end
end