Class: Bureaucrat::Fields::BigDecimalField

Inherits:
Field
  • Object
show all
Defined in:
lib/bureaucrat/fields.rb

Instance Attribute Summary

Attributes inherited from Field

#error_messages, #help_text, #hidden_widget, #initial, #label, #required, #show_hidden_initial, #validators, #widget

Instance Method Summary collapse

Methods inherited from Field

#bound_data, #clean, #default_hidden_widget, #default_validators, #default_widget, #initialize_copy, #populate_object, #prepare_value, #run_validators, #widget_attrs

Constructor Details

#initialize(options = {}) ⇒ BigDecimalField

Returns a new instance of BigDecimalField.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/bureaucrat/fields.rb', line 273

def initialize(options={})
  @max_value = options.delete(:max_value)
  @min_value = options.delete(:min_value)
  @max_digits = options.delete(:max_digits)
  @max_decimal_places = options.delete(:max_decimal_places)

  if @max_digits && @max_decimal_places
    @max_whole_digits = @max_digits - @decimal_places
  end

  super(options)

  if @min_value
    validators << Validators::MinValueValidator.new(@min_value)
  end

  if @max_value
    validators << Validators::MaxValueValidator.new(@max_value)
  end
end

Instance Method Details

#default_error_messagesObject



294
295
296
297
298
299
300
301
# File 'lib/bureaucrat/fields.rb', line 294

def default_error_messages
  super.merge(invalid: 'Enter a number.',
              max_value: 'Ensure this value is less than or equal to %(max)s.',
              min_value: 'Ensure this value is greater than or equal to %(min)s.',
              max_digits: 'Ensure that there are no more than %(max)s digits in total.',
              max_decimal_places: 'Ensure that there are no more than %(max)s decimal places.',
              max_whole_digits: 'Ensure that there are no more than %(max)s digits before the decimal point.')
end

#to_object(value) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/bureaucrat/fields.rb', line 303

def to_object(value)
  if Validators.empty_value?(value)
    return nil
  end

  begin
    Utils.make_float(value)
    BigDecimal.new(value)
  rescue ArgumentError
    raise ValidationError.new(error_messages[:invalid])
  end
end

#validate(value) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/bureaucrat/fields.rb', line 316

def validate(value)
  super(value)

  if Validators.empty_value?(value)
    return nil
  end

  if value.nan? || value.infinite?
    raise ValidationError.new(error_messages[:invalid])
  end

  sign, alldigits, _, whole_digits = value.split

  if @max_digits && alldigits.length > @max_digits
    msg = Utils.format_string(error_messages[:max_digits],
                              max: @max_digits)
    raise ValidationError.new(msg)
  end

  decimals = alldigits.length - whole_digits

  if @max_decimal_places && decimals > @max_decimal_places
    msg = Utils.format_string(error_messages[:max_decimal_places],
                              max: @max_decimal_places)
    raise ValidationError.new(msg)
  end

  if @max_whole_digits && whole_digits > @max_whole_digits
    msg = Utils.format_string(error_messages[:max_whole_digits],
                              max: @max_whole_digits)
    raise ValidationError.new(msg)
  end

  value
end