Sha256: 68bdb6fde484303940b300a0a02f8fd26b140be1e91fc6819c163c9ad89dcc56

Contents?: true

Size: 944 Bytes

Versions: 10

Compression:

Stored size: 944 Bytes

Contents

module JactiveSupport
  class InvalidParameter < ArgumentError
  end
end

class Hash
  # Validate all keys in a hash match *valid keys, raising JactiveSupport::InvalidParameter on a mismatch.
  # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
  # as keys, this will fail.
  #
  # ==== Examples
  #   { :name => "Rob", :years => "28" }.assert_valid_params(:name, :age) # => raises "InvalidParameter: Unknown key(s): years"
  #   { :name => "Rob", :age => "28" }.assert_valid_params("name", "age") # => raises "InvalidParameter: Unknown key(s): name, age"
  #   { :name => "Rob", :age => "28" }.assert_valid_params(:name, :age) # => passes, raises nothing
  def assert_valid_params(*valid_keys)
    unknown_keys = keys.map{|key| key.to_sym} - [valid_keys].flatten
    raise(::JactiveSupport::InvalidParameter, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
jactive_support-2.1.2 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-3.0.0 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-3.0.0.pre2 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-3.0.0.pre1 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-2.1.1 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-2.1.0 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-2.0.0 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-1.0.2 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-1.0.1-universal-java-1.6 lib/jactive_support/core_ext/hash/assert_valid_params.rb
jactive_support-1.0.0-universal-java-1.6 lib/jactive_support/core_ext/hash/assert_valid_params.rb