Class: Hash
- Defined in:
- lib/jactive_support/core_ext/to_java_map.rb,
lib/jactive_support/core_ext/hash/with_keys_values.rb,
lib/jactive_support/core_ext/hash/assert_valid_params.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#assert_valid_params(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising JactiveSupport::InvalidParameter on a mismatch.
- #to_java_map ⇒ Object
Class Method Details
.with_keys_values(keys, values) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/jactive_support/core_ext/hash/with_keys_values.rb', line 2 def Hash.with_keys_values(keys, values) raise ArgumentError, "Unmatching sizes #{keys.size} != #{values.size}" unless keys.size == values.size #Hash[keys.zip(values).flatten] ret = Hash.new keys.each_with_index{|key, idx| ret[key] = values[idx]} ret end |
Instance Method Details
#assert_valid_params(*valid_keys) ⇒ Object
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
15 16 17 18 |
# File 'lib/jactive_support/core_ext/hash/assert_valid_params.rb', line 15 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 |
#to_java_map ⇒ Object
2 3 4 |
# File 'lib/jactive_support/core_ext/to_java_map.rb', line 2 def to_java_map self end |