Module: RailsStuff::ParamsParser

Extended by:
ParamsParser
Included in:
ParamsParser
Defined in:
lib/rails_stuff/params_parser.rb

Overview

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

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#boolean_parserObject



129
130
131
132
133
134
135
136
137
# 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) ⇒ Object

Parses value with specified block. Reraises occured error with Error.



47
48
49
50
51
# 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) ⇒ Object

Parses each value in array with specified block. Returns ‘nil` if `val` is not an array.



55
56
57
58
# 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) ⇒ Object

Parse boolean using ActiveResord’s parser.



122
123
124
125
126
127
# 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) ⇒ Object

Parse time in current TZ using ‘Time.parse`.



140
141
142
# 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) ⇒ Object

Parse decimal value.



112
113
114
# 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) ⇒ Object

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



117
118
119
# File 'lib/rails_stuff/params_parser.rb', line 117

def parse_decimal_array(val)
  parse_array(val) { |x| string_to_decimal(x) }
end

#parse_json(val) ⇒ Object

Parse JSON string.



145
146
147
# 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| ... } ⇒ Object

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.

Yields:

  • (val)


62
63
64
65
# 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) ⇒ Object

Parse string value.



102
103
104
# 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) ⇒ Object

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



107
108
109
# File 'lib/rails_stuff/params_parser.rb', line 107

def parse_string_array(val)
  parse_array(val, allow_blank: true, &:to_s)
end