Method: DataModel::Builtin::Numeric#read

Defined in:
lib/data_model/builtin/numeric.rb

#read(val, coerce: false) ⇒ Array(Object, Error)

read a value, and validate it

Parameters:

  • val (Object)

    the value to read

  • coerce (Boolean) (defaults to: false)

    whether to coerce the value

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/data_model/builtin/numeric.rb', line 15

def read(val, coerce: false)
  args = Arguments.new(type_args)
  err = Error.new

  # optional and missing
  if !args.optional && val.nil?
    err.add(missing_error(type_name))
  end

  # when missing, return early
  if val.nil?
    return [val, err]
  end

  val, err = invoke(:or, val, params: [:integer, :float, :decimal], coerce:)

  # done
  return [val, err]
end