module RailsStuff::ParamsParser
Provides parsing and type-casting functions. Reraises all ocured errors with Error
class, so you can handle it together:
rescue_from RailsStuff::ParamsParser::Error, with: :render_bad_request
You can define more parsing methods by extending with this module and using .parse:
# models/params_parser module ParamsParser extend RailsStuff::ParamsParser extend self def parse_money(val) parse(val) { your_stuff(val) } end end
Public Instance Methods
# File lib/rails_stuff/params_parser.rb, line 129 def boolean_parser require 'active_record' ar_parser = ActiveRecord::Type::Boolean.new if RailsStuff.rails4? ->(val) { ar_parser.type_cast_from_user(val) } else ->(val) { ar_parser.cast(val) } end end
Parses value with specified block. Reraises occured error with Error
.
# File lib/rails_stuff/params_parser.rb, line 47 def parse(val, *args, &block) parse_not_blank(val, *args, &block) rescue => e # rubocop:disable Lint/RescueWithoutErrorClass raise Error.new(e.message, val), nil, e.backtrace end
Parses each value in array with specified block. Returns `nil` if `val` is not an array.
# File lib/rails_stuff/params_parser.rb, line 55 def parse_array(array, *args, &block) return unless array.is_a?(Array) parse(array) { array.map { |val| parse_not_blank(val, *args, &block) } } end
Parse boolean using ActiveResord's parser.
# File lib/rails_stuff/params_parser.rb, line 122 def parse_boolean(val) parse(val) do @boolean_parser ||= boolean_parser @boolean_parser[val] end end
Parse time in current TZ using `Time.parse`.
# File lib/rails_stuff/params_parser.rb, line 140 def parse_datetime(val) parse(val) { Time.zone.parse(val) || raise('Invalid datetime') } end
Parse decimal value.
# File lib/rails_stuff/params_parser.rb, line 112 def parse_decimal(val) parse(val) { |x| string_to_decimal(x) } end
Parses array of decimals. Returns `nil` if `val` is not an array.
# File lib/rails_stuff/params_parser.rb, line 117 def parse_decimal_array(val) parse_array(val) { |x| string_to_decimal(x) } end
Parse float value.
# File lib/rails_stuff/params_parser.rb, line 77
Parses array of floats. Returns `nil` if `val` is not an array.
# File lib/rails_stuff/params_parser.rb, line 82
Parse int value.
# File lib/rails_stuff/params_parser.rb, line 67
Parses array of ints. Returns `nil` if `val` is not an array.
# File lib/rails_stuff/params_parser.rb, line 72
Parse JSON string.
# File lib/rails_stuff/params_parser.rb, line 145 def parse_json(val) parse(val) { JSON.parse(val) } end
Parses value with given block only when it is not nil. Empty string is converted to nil. Pass `allow_blank: true` to return it as is.
# File lib/rails_stuff/params_parser.rb, line 62 def parse_not_blank(val, allow_blank: false) return if val.nil? || !allow_blank && val.is_a?(String) && val.blank? yield(val) end
Parse string value.
# File lib/rails_stuff/params_parser.rb, line 102 def parse_string(val) parse(val, allow_blank: true, &:to_s) end
Parses array of strings. Returns `nil` if `val` is not an array.
# File lib/rails_stuff/params_parser.rb, line 107 def parse_string_array(val) parse_array(val, allow_blank: true, &:to_s) end
Private Instance Methods
Workaround for github.com/ruby/bigdecimal/issues/63
# File lib/rails_stuff/params_parser.rb, line 152 def string_to_decimal(val) BigDecimal.new(val) rescue ArgumentError BigDecimal.new(0) end