Class: Uinit::Type::Integer

Inherits:
TypeOf show all
Defined in:
lib/uinit/type/integer.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) ⇒ Integer



6
7
8
9
10
11
12
13
14
# File 'lib/uinit/type/integer.rb', line 6

def initialize(min: nil, max: nil, positive: nil, negative: nil, zero: nil)
  super(::Integer)

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

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



16
17
18
# File 'lib/uinit/type/integer.rb', line 16

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



16
17
18
# File 'lib/uinit/type/integer.rb', line 16

def min
  @min
end

#negativeObject (readonly)

Returns the value of attribute negative.



16
17
18
# File 'lib/uinit/type/integer.rb', line 16

def negative
  @negative
end

#positiveObject (readonly)

Returns the value of attribute positive.



16
17
18
# File 'lib/uinit/type/integer.rb', line 16

def positive
  @positive
end

#zeroObject (readonly)

Returns the value of attribute zero.



16
17
18
# File 'lib/uinit/type/integer.rb', line 16

def zero
  @zero
end

Instance Method Details

#check!(value, depth) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/uinit/type/integer.rb', line 34

def check!(value, depth)
  super

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

  value
end

#inspectObject



54
55
56
# File 'lib/uinit/type/integer.rb', line 54

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

#is?(value) ⇒ Boolean

rubocop:disable Metrics/PerceivedComplexity



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/uinit/type/integer.rb', line 19

def is?(value)
  return false unless super

  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?

  true
end

#optionsObject

rubocop:enable Metrics/PerceivedComplexity



50
51
52
# File 'lib/uinit/type/integer.rb', line 50

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