Class: Taipu::Number

Inherits:
Base
  • Object
show all
Defined in:
lib/taipu/number.rb

Overview

The type number.

Instance Method Summary collapse

Methods inherited from Base

#to_h, #to_sym

Constructor Details

#initialize(min: nil, max: nil) ⇒ Number

Returns a new instance of Number.



7
8
9
10
11
12
# File 'lib/taipu/number.rb', line 7

def initialize(min: nil, max: nil)
  fail 'MinIsGreaterThanMaxError' if !min.nil? && !max.nil? && min > max

  @min = min
  @max = max
end

Instance Method Details

#constraintsObject



22
23
24
25
26
27
# File 'lib/taipu/number.rb', line 22

def constraints
  {
    min: @min,
    max: @max
  }
end

#valid?(value) ⇒ Boolean

Returns:



14
15
16
17
18
19
20
# File 'lib/taipu/number.rb', line 14

def valid?(value)
  return false unless value.is_a?(::Numeric)
  return false if !@min.nil? && value < @min
  return false if !@max.nil? && value > @max

  true
end