Class: Uinit::Type::Float
Instance Attribute Summary collapse
-
#finite ⇒ Object
readonly
rubocop:enable Metrics/ParameterLists.
-
#max ⇒ Object
readonly
rubocop:enable Metrics/ParameterLists.
-
#min ⇒ Object
readonly
rubocop:enable Metrics/ParameterLists.
-
#negative ⇒ Object
readonly
rubocop:enable Metrics/ParameterLists.
-
#positive ⇒ Object
readonly
rubocop:enable Metrics/ParameterLists.
-
#precision ⇒ Object
readonly
rubocop:enable Metrics/ParameterLists.
-
#zero ⇒ Object
readonly
rubocop:enable Metrics/ParameterLists.
Attributes inherited from TypeOf
Instance Method Summary collapse
- #check!(value, depth) ⇒ Object
-
#initialize(min: nil, max: nil, positive: nil, negative: nil, zero: nil, precision: nil, finite: true) ⇒ Float
constructor
rubocop:disable Metrics/ParameterLists.
- #inspect ⇒ Object
-
#is?(value) ⇒ Boolean
rubocop:disable Metrics/PerceivedComplexity.
-
#options ⇒ Object
rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity.
Methods inherited from TypeOf
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
#finite ⇒ Object (readonly)
rubocop:enable Metrics/ParameterLists
20 21 22 |
# File 'lib/uinit/type/float.rb', line 20 def finite @finite end |
#max ⇒ Object (readonly)
rubocop:enable Metrics/ParameterLists
20 21 22 |
# File 'lib/uinit/type/float.rb', line 20 def max @max end |
#min ⇒ Object (readonly)
rubocop:enable Metrics/ParameterLists
20 21 22 |
# File 'lib/uinit/type/float.rb', line 20 def min @min end |
#negative ⇒ Object (readonly)
rubocop:enable Metrics/ParameterLists
20 21 22 |
# File 'lib/uinit/type/float.rb', line 20 def negative @negative end |
#positive ⇒ Object (readonly)
rubocop:enable Metrics/ParameterLists
20 21 22 |
# File 'lib/uinit/type/float.rb', line 20 def positive @positive end |
#precision ⇒ Object (readonly)
rubocop:enable Metrics/ParameterLists
20 21 22 |
# File 'lib/uinit/type/float.rb', line 20 def precision @precision end |
#zero ⇒ Object (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 |
#inspect ⇒ Object
65 66 67 |
# File 'lib/uinit/type/float.rb', line 65 def inspect "$#{name}[#{options.inspect}]" end |
#is?(value) ⇒ Boolean
rubocop:disable Metrics/PerceivedComplexity
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 |
#options ⇒ Object
rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
61 62 63 |
# File 'lib/uinit/type/float.rb', line 61 def { min:, max:, positive:, negative:, zero:, precision:, finite: }.compact end |