Class: Shale::Type::Decimal

Inherits:
Value
  • Object
show all
Defined in:
lib/shale/type/decimal.rb

Overview

Cast value to BigDecimal

Class Method Summary collapse

Methods inherited from Value

as_hash, as_xml, of_csv, of_hash, of_json, of_toml, of_xml, of_yaml

Class Method Details

.as_csv(value) ⇒ Object



35
36
37
# File 'lib/shale/type/decimal.rb', line 35

def as_csv(value, **)
  value.to_f
end

.as_json(value) ⇒ Object



27
28
29
# File 'lib/shale/type/decimal.rb', line 27

def as_json(value, **)
  value.to_f
end

.as_toml(value) ⇒ Object



39
40
41
# File 'lib/shale/type/decimal.rb', line 39

def as_toml(value, **)
  value.to_f
end

.as_xml_value(value) ⇒ Object



43
44
45
# File 'lib/shale/type/decimal.rb', line 43

def as_xml_value(value, **)
  value.to_s('F')
end

.as_yaml(value) ⇒ Object



31
32
33
# File 'lib/shale/type/decimal.rb', line 31

def as_yaml(value, **)
  value.to_f
end

.cast(value) ⇒ BigDecimal?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (BigDecimal, nil)


17
18
19
20
21
22
23
24
25
# File 'lib/shale/type/decimal.rb', line 17

def cast(value)
  return if value.nil?

  case value
  when ::BigDecimal then value
  when ::Float then BigDecimal(value, value.to_s.length)
  else BigDecimal(value)
  end
end