Class: Rtype::Behavior::NumericCheck

Inherits:
Base show all
Defined in:
lib/rtype/behavior/numeric_check.rb

Direct Known Subclasses

FloatCheck, IntegerCheck

Constant Summary collapse

@@conditions =
[
  :>, :<, :>=, :<=, :==
]

Instance Method Summary collapse

Methods inherited from Base

[]

Constructor Details

#initialize(condition, x) ⇒ NumericCheck

Returns a new instance of NumericCheck.

Parameters:

  • condition (Symbol)
  • x (Numeric)

Raises:

  • (ArgumentError)


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 error_message(value)
  "Expected #{value.inspect} to be a numeric #{@condition} #{@x}"
end

#valid?(value) ⇒ Boolean

Returns:



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