Module: Visor::Common::Extensions::Hash

Defined in:
lib/common/extensions/hash.rb

Overview

Extending Hash class

Instance Method Summary collapse

Instance Method Details

#assert_exclusion_keys(*exclude_keys) ⇒ Object

Validate non-inclusion of some keys in a hash.

Parameters:

  • exclude_keys (Array)

    Keys that must be not be included in the hash.

Raises:

  • (ArgumentError)

    If some key is included.



77
78
79
80
# File 'lib/common/extensions/hash.rb', line 77

def assert_exclusion_keys(*exclude_keys)
  exc = exclude_keys.flatten & keys
  raise ArgumentError, "These fields are read-only: #{exc.join(', ')}" unless exc.empty?
end

#assert_inclusion_keys(*inclusion_keys) ⇒ Object

Validate inclusions of some keys in a hash.

Parameters:

  • inclusion_keys (Array)

    Keys that must be included in the hash.

Raises:

  • (ArgumentError)

    If some key is not included.



66
67
68
69
# File 'lib/common/extensions/hash.rb', line 66

def assert_inclusion_keys(*inclusion_keys)
  inc = inclusion_keys.flatten - keys
  raise ArgumentError, "These fields are required: #{inc.join(', ')}" unless inc.empty?
end

#assert_valid_keys(*valid_keys) ⇒ Object

Validate all keys in a hash match *valid keys.

Parameters:

  • valid_keys (Array)

    Valid keys.

Raises:

  • (ArgumentError)

    On a mismatch.



55
56
57
58
# File 'lib/common/extensions/hash.rb', line 55

def assert_valid_keys(*valid_keys)
  unknown_keys = keys - valid_keys.flatten
  raise ArgumentError, "Unknown fields: #{unknown_keys.join(", ")}" unless unknown_keys.empty?
end

#assert_valid_values_for(key, *valid_values) ⇒ Object

Validate value of some key in a hash.

Parameters:

  • valid_values (Array)

    Values to assert against the given key.

  • key (Array)

    The key to assert its value.

Raises:

  • (ArgumentError)

    If some key is included.



89
90
91
92
93
# File 'lib/common/extensions/hash.rb', line 89

def assert_valid_values_for(key, *valid_values)
  unless valid_values.flatten.include?(self[key.to_sym]) || self[key].nil?
    raise ArgumentError, "Invalid #{key.to_s} '#{self[key]}', available options: #{valid_values.join(', ')}"
  end
end

#set_blank_keys_value_to(keys_to_set, keys_to_ignore, to_value) ⇒ Object

Set some keys in a hash to a given value, possibly ignoring some keys.

Parameters:

  • keys_to_set (Array)

    Keys to set its value.

  • keys_to_ignore (Array)

    Keys to be ignored.

  • to_value (Array)

    Value to set the wanted keys.

Raises:

  • (ArgumentError)

    If some key is included.



103
104
105
# File 'lib/common/extensions/hash.rb', line 103

def set_blank_keys_value_to(keys_to_set, keys_to_ignore, to_value)
  keys_to_set.each { |k| self.merge!(k => to_value) unless self[k] || keys_to_ignore.include?(k) }
end

#stringify_keysObject

Return a new hash with all keys converted to strings.



17
18
19
20
21
22
23
24
# File 'lib/common/extensions/hash.rb', line 17

def stringify_keys
  inject({}) do |acc, (k, v)|
    key = Symbol === k ? k.to_s : k
    value = Hash === v ? v.stringify_keys : v
    acc[key] = value
    acc
  end
end

#stringify_keys!Object

Destructively convert all keys to strings.



28
29
30
# File 'lib/common/extensions/hash.rb', line 28

def stringify_keys!
  self.replace(self.stringify_keys)
end

#symbolize_keysObject

Return a new hash with all keys converted to symbols.



34
35
36
37
38
39
40
41
# File 'lib/common/extensions/hash.rb', line 34

def symbolize_keys
  inject({}) do |acc, (k, v)|
    key = String === k ? k.to_sym : k
    value = Hash === v ? v.symbolize_keys : v
    acc[key] = value
    acc
  end
end

#symbolize_keys!Object

Destructively convert all keys to symbols.



45
46
47
# File 'lib/common/extensions/hash.rb', line 45

def symbolize_keys!
  self.replace(self.symbolize_keys)
end

#to_openstructOpenStruct

Convert a Hash to a OpenStruct, so it can be accessed as options like: h => h.key

Returns:

  • (OpenStruct)

    The resulting OpenStruct object.



111
112
113
114
115
# File 'lib/common/extensions/hash.rb', line 111

def to_openstruct
  mapped = {}
  each { |key, value| mapped[key] = value.to_openstruct }
  OpenStruct.new(mapped)
end