Class: TRuby::NumericRangeConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- TRuby::NumericRangeConstraint
- Defined in:
- lib/t_ruby/constraint_checker.rb
Overview
Numeric range constraint: Integer where min..max
Instance Attribute Summary collapse
-
#base_type ⇒ Object
readonly
Returns the value of attribute base_type.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Attributes inherited from Constraint
Instance Method Summary collapse
-
#initialize(base_type:, min: nil, max: nil) ⇒ NumericRangeConstraint
constructor
A new instance of NumericRangeConstraint.
- #satisfied?(value) ⇒ Boolean
- #validation_code(var_name) ⇒ Object
Methods inherited from Constraint
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_type ⇒ Object (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 |
#max ⇒ Object (readonly)
Returns the value of attribute max.
51 52 53 |
# File 'lib/t_ruby/constraint_checker.rb', line 51 def max @max end |
#min ⇒ Object (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 |