class NRSER::Types::Bounded

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

@note

Construct {Bounded} types using the {.Bounded} factory.

Attributes

max[R]

Minimum value.

@return [Number]

min[R]

Minimum value.

@return [Number]

Public Class Methods

new(min: nil, max: nil, **options) click to toggle source
Calls superclass method
# File lib/nrser/types/bounded.rb, line 44
def initialize  min: nil,
                max: nil,
                **options
  super **options
  
  @min = min
  @max = max
end

Public Instance Methods

explain() click to toggle source
# 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
symbolic() click to toggle source
# 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) click to toggle source
# 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