Class: ActsAsCalculator::CastJsonSafe

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_calculator/cast_json_safe.rb

Constant Summary collapse

NATIVE =
[NilClass, TrueClass, FalseClass, String, Integer, Float].freeze

Class Method Summary collapse

Class Method Details

.call(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/acts_as_calculator/cast_json_safe.rb', line 9

def self.call(value)
  case value
  when Hash, Array then container(value)
  # BigDecimal#to_json emits "0.1234e4"; a plain decimal string round-trips into the
  # jsonb column as something a human (and a Phase 3 template) can read.
  when BigDecimal then value.to_s("F")
  when Symbol then value.to_s
  when Rational then value.to_f
  else scalar(value)
  end
end