Class: NRSER::Types::Bounded

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

Overview

Note:

Construct Bounded types using the Bounded factory.

Types whose members satisfy a #min, #max or both (inclusive).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

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

Constructor Details

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

Returns a new instance of Bounded.



44
45
46
47
48
49
50
51
# File 'lib/nrser/types/bounded.rb', line 44

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

Instance Attribute Details

#maxNumber (readonly)

Minimum value.

Returns:

  • (Number)


41
42
43
# File 'lib/nrser/types/bounded.rb', line 41

def max
  @max
end

#minNumber (readonly)

Minimum value.

Returns:

  • (Number)


34
35
36
# File 'lib/nrser/types/bounded.rb', line 34

def min
  @min
end

Instance Method Details

#explainObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nrser/types/bounded.rb', line 76

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

#symbolicObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nrser/types/bounded.rb', line 59

def symbolic
  if min
    if max
      # has min and max, use range notation
      "(#{ min.inspect }..#{ max.inspect })"
    else
      # only has min
      "(#{ min.inspect }..)"
      # "{ x : x #{ NRSER::Types::GEQ } #{ min } }"
    end
  else
    # only has max
    "(..#{ max.inspect })"
    # "{ x : x #{ NRSER::Types::LEQ } #{ max } }"
  end
end

#test?(value) ⇒ Boolean

Returns:



53
54
55
56
57
# File 'lib/nrser/types/bounded.rb', line 53

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