Module: Lazier::Hash

Extended by:
ActiveSupport::Concern
Defined in:
lib/lazier/hash.rb

Overview

Extensions for Hash objects.

Constant Summary

VALID_ACCESSES =

The supported accesses for #ensure_access

{
  strings: :stringify_keys,
  symbols: :symbolize_keys,
  indifferent: :with_indifferent_access,
  dotted: :enable_dotted_access
}

Instance Method Summary (collapse)

Instance Method Details

- (Hash) compact(&validator)

Returns a new hash, removing all keys which values are blank.

Parameters:

  • validator (Proc)

    , if present all the keys which evaluates to true will be removed. Otherwise all blank values will be removed.

Returns:

  • (Hash)

    The hash with all blank values removed.



24
25
26
# File 'lib/lazier/hash.rb', line 24

def compact(&validator)
  dup.compact!(&validator)
end

- (Object) compact!(&validator)

Compacts the current hash, removing all keys which values are blank.

Parameters:

  • validator (Proc)

    , if present all the keys which evaluates to true will be removed. Otherwise all blank values will be removed.



31
32
33
34
# File 'lib/lazier/hash.rb', line 31

def compact!(&validator)
  validator ||= ->(_, v) { v.blank? }
  reject!(&validator)
end

- (Hash) enable_dotted_access(readonly = true)

Makes sure that the hash is accessible using dotted notation. This is also applied to every embedded hash.

Parameters:

  • readonly (Boolean) (defaults to: true)

    If the dotted notation is only enable for reading. true by default.

Returns:

  • (Hash)

    The current hash with keys enabled for dotted access.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lazier/hash.rb', line 52

def enable_dotted_access(readonly = true)
  extend(Hashie::Extensions::MethodReader)
  extend(Hashie::Extensions::MethodQuery)
  extend(Hashie::Extensions::MethodWriter) unless readonly

  each do |_, value|
    enable_dotted_access_for_value(value, readonly)
  end

  self
end

- (Hash) enable_dotted_access_for_value(value, readonly)

Makes sure that the value is accessible using dotted notation. This is also applied to every embedded hash.

Parameters:

  • value (Object)

    The value to manipulate.

  • readonly (Boolean)

    If the dotted notation is only enable for reading. true by default.

Returns:

  • (Hash)

    The current value enabled for dotted access.



69
70
71
72
73
74
75
76
77
# File 'lib/lazier/hash.rb', line 69

def enable_dotted_access_for_value(value, readonly)
  if value.is_a?(Hash)
    value.enable_dotted_access(readonly)
  elsif value.respond_to?(:each) then
    value.each do |element|
      element.enable_dotted_access(readonly) if element.is_a?(Hash)
    end
  end
end

- (Hash) ensure_access(*accesses)

Makes sure that the keys of the hash are accessible in the desired way.

Parameters:

  • accesses (Array)

    The requested access for the keys. Can be :strings, :symbols or :indifferent. If nil the keys are not modified.

Returns:

  • (Hash)

    The current hash with keys modified.



40
41
42
43
44
45
46
# File 'lib/lazier/hash.rb', line 40

def ensure_access(*accesses)
  accesses.compact.reduce(self) do |rv, access|
    method = VALID_ACCESSES.fetch(access.ensure_string.to_sym, nil)
    rv = rv.send(method) if method
    rv
  end
end