Module: VV::FloatMethods

Included in:
Float
Defined in:
lib/vv/float_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/vv/float_methods.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#vv_json(nan_coerces: false, infinity_coerces: false, max: nil, min: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vv/float_methods.rb', line 16

def vv_json nan_coerces: false,
            infinity_coerces: false,
            max: nil,
            min: nil

  fail "Expecting numeric value for `max`" if max.is_a? String
  fail "Expecting numeric value for `min`" if min.is_a? String

  message = \
  "Infinite not convertable sans `infinity_coerces: true`. "
  message += "Set `max` and `min` for non-null coercion."
  fail message if infinite? unless infinity_coerces

  message = \
  "NaN not convertable sans `nan_coerces: true`."
  fail message if self.nan? unless nan_coerces

  return nil.to_json if nan?

  use_max   = ( max.present? and self > max )
  use_max ||= ( infinite?    and self > 0 )
  return max.to_json if use_max

  use_min   = ( min.present? and self < min )
  use_min ||= ( infinite?    and self < 0 )
  return min.to_json if use_min

  ::JSON.dump(self)
end