Class: NRSER::Types::Bounded

Inherits:
Type show all
Defined in:
lib/nrser/types/bounded.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#===, #builtin_inspect, #check, #check!, #from_data, #from_s, #has_from_data?, #has_from_s?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #test, #to_data, #to_s, #union, #xor

Constructor Details

#initialize(min: nil, max: nil, **options) ⇒ Bounded

Returns a new instance of Bounded.



20
21
22
23
24
25
26
27
# File 'lib/nrser/types/bounded.rb', line 20

def initialize  min: nil,
                max: nil,
                **options
  super **options
  
  @min = min
  @max = max
end

Instance Attribute Details

#maxNumber (readonly)

Minimum value.

Returns:

  • (Number)


17
18
19
# File 'lib/nrser/types/bounded.rb', line 17

def max
  @max
end

#minNumber (readonly)

Minimum value.

Returns:

  • (Number)


10
11
12
# File 'lib/nrser/types/bounded.rb', line 10

def min
  @min
end

Instance Method Details

#explainObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nrser/types/bounded.rb', line 35

def explain
  attrs_str = ['min', 'max'].map {|name|
    [name, instance_variable_get("@#{ name }")]
  }.reject {|name, value|
    value.nil?
  }.map {|name, value|
    "#{ name }=#{ value }"
  }.join(', ')
  
  "#{ self.class.demod_name }<#{ attrs_str }>"
end

#test?(value) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/nrser/types/bounded.rb', line 29

def test? value
  return false if @min && value < @min
  return false if @max && value > @max
  true
end