Class: ParamsReady::Value::RangeConstraint

Inherits:
Constraint
  • Object
show all
Defined in:
lib/params_ready/value/constraint.rb

Instance Attribute Summary

Attributes inherited from Constraint

#condition

Instance Method Summary collapse

Methods inherited from Constraint

build, instance, register

Methods included from Extensions::Registry

#human_string, #registry

Constructor Details

#initialize(cond, *args, **opts) ⇒ RangeConstraint

Returns a new instance of RangeConstraint.

Raises:



59
60
61
62
# File 'lib/params_ready/value/constraint.rb', line 59

def initialize(cond, *args, **opts)
  raise ParamsReadyError, "Expected Range, got: " + cond.class.name unless cond.is_a?(Range)
  super cond, *args, **opts
end

Instance Method Details

#clamp(value) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/params_ready/value/constraint.rb', line 72

def clamp(value)
  if value < @condition.min
    @condition.min
  elsif value > @condition.max
    @condition.max
  else
    value
  end
end

#clamp?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/params_ready/value/constraint.rb', line 82

def clamp?
  return false if @condition.min.nil? || @condition.max.nil?

  true
end

#error_messageObject



68
69
70
# File 'lib/params_ready/value/constraint.rb', line 68

def error_message
  'not in range'
end

#valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/params_ready/value/constraint.rb', line 64

def valid?(input)
  @condition.include?(input)
end