module Roda::RodaPlugins::TypecastParamsSizedIntegers

The typecast_params_sized_integers plugin adds sized integer conversion methods to typecast_params:

The int*, pos_int*, and Integer* methods operate the same as the standard int, pos_int, and Integer methods in typecast_params, except that they will only handle parameter values in the given range for the signed integer type. The uint*, pos_int*, and Integeru* methods are similar to the int*, pos_int*, and Integer* methods, except they use the range of the unsigned integer type instead of the range of the signed integer type.

Here are the signed and unsigned integer type ranges:

8
-128, 127], [0, 255
16
-32768, 32767], [0, 65535
32
-2147483648, 2147483647], [0, 4294967295
64
-9223372036854775808, 9223372036854775807], [0, 18446744073709551615

To only create methods for certain integer sizes, you can pass a :sizes option when loading the plugin, and it will only create methods for the sizes you specify.

You can provide a :default_size option when loading the plugin, in which case the int, uint, pos_int, pos_uint, Integer, and Integeru, typecast_params conversion methods will be aliases to the conversion methods for the given sized type:

plugin :typecast_params_sized_integers, default_size: 64

route do |r|
  # Returns nil unless param.to_i > 0 && param.to_i <= 9223372036854775807
  typecast_params.pos_int('param_name')
end