Class: Rtype::Behavior::NumericCheck
- Defined in:
- lib/rtype/behavior/numeric_check.rb
Direct Known Subclasses
Constant Summary collapse
- @@conditions =
[ :>, :<, :>=, :<=, :== ]
Instance Method Summary collapse
- #error_message(value) ⇒ Object
-
#initialize(condition, x) ⇒ NumericCheck
constructor
A new instance of NumericCheck.
- #valid?(value) ⇒ Boolean
Methods inherited from Base
Constructor Details
#initialize(condition, x) ⇒ NumericCheck
Returns a new instance of NumericCheck.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rtype/behavior/numeric_check.rb', line 10 def initialize(condition, x) raise ArgumentError, "Invalid condition '#{condition}'" unless @@conditions.include?(condition) raise ArgumentError, "x is not a Numeric" unless x.is_a?(Numeric) @condition = condition @x = x @lambda = case condition when :> lambda { |obj| obj > @x } when :< lambda { |obj| obj < @x } when :>= lambda { |obj| obj >= @x } when :<= lambda { |obj| obj <= @x } when :== lambda { |obj| obj == @x } end end |
Instance Method Details
#error_message(value) ⇒ Object
37 38 39 |
# File 'lib/rtype/behavior/numeric_check.rb', line 37 def (value) "Expected #{value.inspect} to be a numeric #{@condition} #{@x}" end |
#valid?(value) ⇒ Boolean
29 30 31 32 33 34 35 |
# File 'lib/rtype/behavior/numeric_check.rb', line 29 def valid?(value) if value.is_a?(Numeric) @lambda.call(value) else false end end |