Class: Hash

Inherits:
Object show all
Defined in:
lib/support/hash.rb

Instance Method Summary collapse

Instance Method Details

#extract(key) ⇒ Object

get a value from the given key whether it be stored as a symbol or string.



18
19
20
# File 'lib/support/hash.rb', line 18

def extract(key)
  self[key.to_s] || self[key.to_sym]
end

#extract_values_at(*keys) ⇒ Object

Same as values_at(), but uses extract().



23
24
25
# File 'lib/support/hash.rb', line 23

def extract_values_at(*keys)
  keys.map { |key| extract(key) }
end

#verify_keys_present_and_values_not_blank(*keys) ⇒ Object

This gem was meant to help deal with the strictness of APIs and their inability to give good feedback for mundane detail errors. This method helps eliminate guessing when it comes to Request query/body/etc. hashes.



6
7
8
9
10
11
12
13
14
15
# File 'lib/support/hash.rb', line 6

def verify_keys_present_and_values_not_blank(*keys)
  keys.each do |key|
    unless has_key?(key.to_sym) || has_key?(key.to_s)
      raise "Options missing '#{key}'"
    end
    if extract(key).blank?
      raise "Option '#{key}' cannot be nil"
    end
  end
end