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

boolean_parser() click to toggle source
# 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
parse(val, *args, &block) click to toggle source

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
parse_array(array, *args, &block) click to toggle source

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(val) click to toggle source

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_datetime(val) click to toggle source

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(val) click to toggle source

Parse decimal value.

# File lib/rails_stuff/params_parser.rb, line 112
def parse_decimal(val)
  parse(val) { |x| string_to_decimal(x) }
end
parse_decimal_array(val) click to toggle source

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(val) click to toggle source

Parse float value.

# File lib/rails_stuff/params_parser.rb, line 77
    
parse_float_array(val) click to toggle source

Parses array of floats. Returns `nil` if `val` is not an array.

# File lib/rails_stuff/params_parser.rb, line 82
    
parse_int(val) click to toggle source

Parse int value.

# File lib/rails_stuff/params_parser.rb, line 67
    
parse_int_array(val) click to toggle source

Parses array of ints. Returns `nil` if `val` is not an array.

# File lib/rails_stuff/params_parser.rb, line 72
    
parse_json(val) click to toggle source

Parse JSON string.

# File lib/rails_stuff/params_parser.rb, line 145
def parse_json(val)
  parse(val) { JSON.parse(val) }
end
parse_not_blank(val, allow_blank: false) { |val| ... } click to toggle source

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(val) click to toggle source

Parse string value.

# File lib/rails_stuff/params_parser.rb, line 102
def parse_string(val)
  parse(val, allow_blank: true, &:to_s)
end
parse_string_array(val) click to toggle source

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

string_to_decimal(val) click to toggle source

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