Class: TRuby::NumericRangeConstraint

Inherits:
Constraint show all
Defined in:
lib/t_ruby/constraint_checker.rb

Overview

Numeric range constraint: Integer where min..max

Instance Attribute Summary collapse

Attributes inherited from Constraint

#condition, #message, #type

Instance Method Summary collapse

Methods inherited from Constraint

#to_s

Constructor Details

#initialize(base_type:, min: nil, max: nil) ⇒ NumericRangeConstraint



53
54
55
56
57
58
59
# File 'lib/t_ruby/constraint_checker.rb', line 53

def initialize(base_type:, min: nil, max: nil)
  @base_type = base_type
  @min = min
  @max = max
  range_str = build_range_string
  super(type: :numeric_range, condition: range_str)
end

Instance Attribute Details

#base_typeObject (readonly)

Returns the value of attribute base_type.



51
52
53
# File 'lib/t_ruby/constraint_checker.rb', line 51

def base_type
  @base_type
end

#maxObject (readonly)

Returns the value of attribute max.



51
52
53
# File 'lib/t_ruby/constraint_checker.rb', line 51

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



51
52
53
# File 'lib/t_ruby/constraint_checker.rb', line 51

def min
  @min
end

Instance Method Details

#satisfied?(value) ⇒ Boolean



61
62
63
64
65
66
67
# File 'lib/t_ruby/constraint_checker.rb', line 61

def satisfied?(value)
  return false unless value.is_a?(Numeric)
  return false if @min && value < @min
  return false if @max && value > @max

  true
end

#validation_code(var_name) ⇒ Object



69
70
71
72
73
74
# File 'lib/t_ruby/constraint_checker.rb', line 69

def validation_code(var_name)
  conditions = []
  conditions << "#{var_name} >= #{@min}" if @min
  conditions << "#{var_name} <= #{@max}" if @max
  conditions.join(" && ")
end