Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/sequencescape-api/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#required!(*keys) {|missing| ... } ⇒ Object

Yields all of the missing keys if there are any so that you can do what you like, like error maybe?

Yields:

  • (missing)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sequencescape-api/core_ext/hash.rb', line 4

def required!(*keys)
  options = keys.extract_options!
  return if keys.empty?

  allowance_method = options[:allow_blank] == false ? :blank? : :nil?

  missing = keys.inject([]) do |missing, next_key|
    missing.tap do
      value = self[next_key]
      missing << next_key if value.send(allowance_method)
    end
  end
  yield(missing) unless missing.empty?
end