class Attributor::BigDecimal

Public Class Methods

as_json_schema( shallow: false, example: nil, attribute_options: {} ) click to toggle source

Bigdecimal numbers are too big to be represented as numbers in JSON schema The way to do so, is to represent them as strings, but add a ‘format’ specifying how to parse that (seemingly, there is a ‘double’ well known type that json API claims we can use) … not sure if this is the right one, as technically BigDecimal has very large precision that a double wouldn’t be able to fit…

Calls superclass method
# File lib/attributor/types/bigdecimal.rb, line 32
def self.as_json_schema( shallow: false, example: nil, attribute_options: {} )
  hash = super
  hash[:format] = 'bigdecimal'
  hash
end
example(_context = nil, options: {}) click to toggle source
# File lib/attributor/types/bigdecimal.rb, line 11
def self.example(_context = nil, options: {})
  BigDecimal("#{Faker::Number.decimal(l_digits: 3, r_digits: 3)}")
end
json_schema_type() click to toggle source
# File lib/attributor/types/bigdecimal.rb, line 23
def self.json_schema_type
  :string
end
load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) click to toggle source
# File lib/attributor/types/bigdecimal.rb, line 15
def self.load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
  return nil if value.nil?
  return value if value.is_a?(native_type)
  return BigDecimal(value, 10) if value.is_a?(::Float)
  return BigDecimal(value + '0') if value.is_a?(::String) && value.end_with?('.')
  BigDecimal(value)
end
native_type() click to toggle source
# File lib/attributor/types/bigdecimal.rb, line 7
def self.native_type
  ::BigDecimal
end