Sha256: 33c97a9ff1fcb9719beb58b37d3ecdf634562575601813b44408b5edc3ef8891

Contents?: true

Size: 810 Bytes

Versions: 3

Compression:

Stored size: 810 Bytes

Contents

class InvalidHashKeyError < Exception; end

module ValidatedHash
  # Raises an exception if a key in the hash does not exist in the list of valid keys
  def validate_only(*keys)
    self.keys.each {|key| raise InvalidHashKeyError.new("#{key} is not a valid key for this hash") unless keys.member?(key)}
  end
  
  # Raises an exception if any of the keys provided are not found in the hash
  def validate_all(*keys)
    keys.each {|key| raise InvalidHashKeyError.new("#{key} is required for this hash") unless self.keys.member?(key)}
  end
  
  # Raises an exception if any of the keys provided are found in the hash
  def validate_none(*keys)
    keys.each {|key| raise InvalidHashKeyError.new("#{key} is not allowed for this hash") if self.keys.member?(key)}
  end
end

class Hash
  include ValidatedHash
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
Neurogami-jimpanzee-1.0.2.1 lib/monkeybars/validated_hash.rb
Neurogami-jimpanzee-1.0.3.2 lib/monkeybars/validated_hash.rb
Neurogami-jimpanzee-1.0.3.4 lib/jimpanzee/validated_hash.rb