| Module | URITemplate::Utils |
| In: |
lib/uri_template/utils.rb
|
A collection of some utility methods. The most methods are used to parse or generate uri-parameters. I will use the escape_utils library if available, but runs happily without.
| KCODE_UTF8 | = | (Regexp::KCODE_UTF8 rescue 0) |
Converts an object to a param value. Tries to call :to_param and then :to_s on that object. @raise Unconvertable if the object could not be converted. @example
URITemplate::Utils.object_to_param(5) #=> "5"
o = Object.new
def o.to_param
"42"
end
URITemplate::Utils.object_to_param(o) #=> "42"
Returns true when the given value is an array and it only consists of arrays with two items. This useful when using a hash is not ideal, since it doesn‘t allow duplicate keys. @example
URITemplate::Utils.pair_array?( Object.new ) #=> false URITemplate::Utils.pair_array?( [] ) #=> true URITemplate::Utils.pair_array?( [1,2,3] ) #=> false URITemplate::Utils.pair_array?( [ ['a',1],['b',2],['c',3] ] ) #=> true URITemplate::Utils.pair_array?( [ ['a',1],['b',2],['c',3],[] ] ) #=> false
Turns the given value into a hash if it is an array of pairs. Otherwise it returns the value. You can test whether a value will be converted with {pair_array?}.
@example
URITemplate::Utils.pair_array_to_hash( 'x' ) #=> 'x'
URITemplate::Utils.pair_array_to_hash( [ ['a',1],['b',2],['c',3] ] ) #=> {'a'=>1,'b'=>2,'c'=>3}
URITemplate::Utils.pair_array_to_hash( [ ['a',1],['a',2],['a',3] ] ) #=> {'a'=>3}
@example Carful vs. Ignorant
URITemplate::Utils.pair_array_to_hash( [ ['a',1],'foo','bar'], false ) #UNDEFINED! URITemplate::Utils.pair_array_to_hash( [ ['a',1],'foo','bar'], true ) #=> [ ['a',1], 'foo', 'bar']
@param x the value to convert @param careful [true,false] wheter to check every array item. Use this when you expect array with subarrays which are not pairs. Setting this to false however improves runtime by ~30% even with comparetivly short arrays.