Class: Plumb::Codec::Forms::NumericEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/plumb/codec.rb

Overview

For fields typed as the Numeric union. A more specific field (Integer, Float, Decimal) picks its own encoder via most-specific matching.

Instance Method Summary collapse

Instance Method Details

#decode(str) ⇒ Object

A fractional or scientific string is a Float; a plain integer literal is an Integer ("1e20".to_i would wrongly be 1).



761
# File 'lib/plumb/codec.rb', line 761

def decode(str) = str.match?(/[.eE]/) ? str.to_f : str.to_i

#encode(num) ⇒ Object



758
759
760
# File 'lib/plumb/codec.rb', line 758

def encode(num) = num.is_a?(BigDecimal) ? num.to_s('F') : num.to_s
# A fractional or scientific string is a Float; a plain integer literal
# is an Integer ("1e20".to_i would wrongly be 1).