Class: Uinit::Type::Float

Inherits:
TypeOf show all
Defined in:
lib/uinit/type/float.rb

Instance Attribute Summary collapse

Attributes inherited from TypeOf

#class_module

Instance Method Summary collapse

Methods inherited from TypeOf

from, from?

Methods inherited from Base

#==, [], from, from?, #is!, #name, #to_s, #trace!, #type_error!

Methods included from Operators

#&, #|

Constructor Details

#initialize(min: nil, max: nil, positive: nil, negative: nil, zero: nil, precision: nil, finite: true) ⇒ Float

rubocop:disable Metrics/ParameterLists



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/uinit/type/float.rb', line 7

def initialize(min: nil, max: nil, positive: nil, negative: nil, zero: nil, precision: nil, finite: true)
  super(::Float)

  @min = min
  @max = max
  @positive = positive
  @negative = negative
  @zero = zero
  @precision = precision
  @finite = finite
end

Instance Attribute Details

#finiteObject (readonly)

rubocop:enable Metrics/ParameterLists



20
21
22
# File 'lib/uinit/type/float.rb', line 20

def finite
  @finite
end

#maxObject (readonly)

rubocop:enable Metrics/ParameterLists



20
21
22
# File 'lib/uinit/type/float.rb', line 20

def max
  @max
end

#minObject (readonly)

rubocop:enable Metrics/ParameterLists



20
21
22
# File 'lib/uinit/type/float.rb', line 20

def min
  @min
end

#negativeObject (readonly)

rubocop:enable Metrics/ParameterLists



20
21
22
# File 'lib/uinit/type/float.rb', line 20

def negative
  @negative
end

#positiveObject (readonly)

rubocop:enable Metrics/ParameterLists



20
21
22
# File 'lib/uinit/type/float.rb', line 20

def positive
  @positive
end

#precisionObject (readonly)

rubocop:enable Metrics/ParameterLists



20
21
22
# File 'lib/uinit/type/float.rb', line 20

def precision
  @precision
end

#zeroObject (readonly)

rubocop:enable Metrics/ParameterLists



20
21
22
# File 'lib/uinit/type/float.rb', line 20

def zero
  @zero
end

Instance Method Details

#check!(value, depth) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/uinit/type/float.rb', line 40

def check!(value, depth)
  super

  type_error!('Float must be finite (not Infinity or NaN)', depth) if finite && !value.finite?
  type_error!("Float too small (minimum #{min})", depth) if min && value < min
  type_error!("Float too large (maximum #{max})", depth) if max && value > max
  type_error!('Float cannot be positive', depth) if positive == false && value.positive?
  type_error!('Float must be positive', depth) if positive == true && value <= 0
  type_error!('Float cannot be negative', depth) if negative == false && value.negative?
  type_error!('Float must be negative', depth) if negative == true && value >= 0
  type_error!('Float cannot be zero', depth) if zero == false && value.zero?
  type_error!('Float must be zero', depth) if zero == true && !value.zero?

  if precision && !valid_precision?(value)
    type_error!("Float exceeds maximum precision of #{precision} decimal places", depth)
  end

  value
end

#inspectObject



65
66
67
# File 'lib/uinit/type/float.rb', line 65

def inspect
  "$#{name}[#{options.inspect}]"
end

#is?(value) ⇒ Boolean

rubocop:disable Metrics/PerceivedComplexity

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/uinit/type/float.rb', line 23

def is?(value)
  return false unless super

  return false if finite && !value.finite?
  return false if min && value < min
  return false if max && value > max
  return false if positive == false && value.positive?
  return false if positive == true && value <= 0
  return false if negative == false && value.negative?
  return false if negative == true && value >= 0
  return false if zero == false && value.zero?
  return false if zero == true && !value.zero?
  return false if precision && !valid_precision?(value)

  true
end

#optionsObject

rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



61
62
63
# File 'lib/uinit/type/float.rb', line 61

def options
  { min:, max:, positive:, negative:, zero:, precision:, finite: }.compact
end