The typecast_params_sized_integers plugin adds sized integer conversion methods to typecast_params:
-
int8, uint8, pos_int8, pos_uint8, Integer8, Integeru8
-
int16, uint16, pos_int16, pos_uint16, Integer16, Integeru16
-
int32, uint32, pos_int32, pos_uint32, Integer32, Integeru32
-
int64, uint64, pos_int64, pos_uint64, Integer64, Integeru64
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 |
| ||
16 |
| ||
32 |
| ||
64 |
|
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
Public Class methods
# File lib/roda/plugins/typecast_params_sized_integers.rb 44 def self.load_dependencies(app, opts=OPTS) 45 app.plugin :typecast_params do 46 (opts[:sizes] || [8, 16, 32, 64]).each do |i| 47 # Avoid defining the same methods more than once 48 next if method_defined?(:"pos_int#{i}") 49 50 min_signed = -(2**(i-1)) 51 max_signed = 2**(i-1)-1 52 max_unsigned = 2**i-1 53 54 handle_type(:"int#{i}", :max_input_bytesize=>100) do |v| 55 if (v = base_convert_int(v)) && v >= min_signed && v <= max_signed 56 v 57 end 58 end 59 60 handle_type(:"uint#{i}", :max_input_bytesize=>100) do |v| 61 if (v = base_convert_int(v)) && v >= 0 && v <= max_unsigned 62 v 63 end 64 end 65 66 handle_type(:"pos_int#{i}", :max_input_bytesize=>100) do |v| 67 if (v = base_convert_int(v)) && v > 0 && v <= max_signed 68 v 69 end 70 end 71 72 handle_type(:"pos_uint#{i}", :max_input_bytesize=>100) do |v| 73 if (v = base_convert_int(v)) && v > 0 && v <= max_unsigned 74 v 75 end 76 end 77 78 handle_type(:"Integer#{i}", :max_input_bytesize=>100) do |v| 79 if (v = base_convert_Integer(v)) && v >= min_signed && v <= max_signed 80 v 81 end 82 end 83 84 handle_type(:"Integeru#{i}", :max_input_bytesize=>100) do |v| 85 if (v = base_convert_Integer(v)) && v >= 0 && v <= max_unsigned 86 v 87 end 88 end 89 end 90 end 91 92 if default = opts[:default_size] 93 app::TypecastParams.class_eval do 94 %w[int uint pos_int pos_uint Integer Integeru].each do |type| 95 ['', 'convert_', '_convert_array_', '_max_input_bytesize_for_'].each do |prefix| 96 alias_method :"#{prefix}#{type}", :"#{prefix}#{type}#{default}" 97 end 98 alias_method :"#{type}!", :"#{type}#{default}!" 99 end 100 end 101 end 102 end