Module: Float::NoBrainerExtensions

Included in:
Float
Defined in:
lib/no_brainer/document/types/float.rb

Constant Summary collapse

InvalidType =
NoBrainer::Error::InvalidType

Instance Method Summary collapse

Instance Method Details

#nobrainer_cast_user_to_model(value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/no_brainer/document/types/float.rb', line 5

def nobrainer_cast_user_to_model(value)
  case value
  when Float   then value
  when Integer then value.to_f
  when String
    value = value.strip.gsub(/^\+/, '')
    value = value.gsub(/0+$/, '') if value['.']
    value = value.gsub(/\.$/, '')
    value = "#{value}.0" unless value['.']
    value.to_f.tap { |new_value| new_value.to_s == value or raise InvalidType }
  else raise InvalidType
  end
end