Class: Numeric

Inherits:
Object show all
Defined in:
lib/tdriver/util/common/numeric.rb

Overview

extend Ruby Numeric class functionality

Instance Method Summary collapse

Instance Method Details

#check_range(range, message = "value $1 is out of range ($2)") ⇒ Object

TODO: document me

Raises:

  • (TypeError)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tdriver/util/common/numeric.rb', line 100

def check_range( range, message = "value $1 is out of range ($2)"  )

  # check that given argument is type of Range
  raise TypeError, 'wrong argument type #{ range.class } for range (expected Range)' unless range.kind_of?( Range )

  # check that given argument is type of Range
  raise TypeError, 'wrong argument type #{ message.class } for exception message (expected String)' unless message.kind_of?( String )

  # replace macros
  message.gsub!( '$1', inspect )
  
  message.gsub!( '$2', range.inspect )

  # raise exception if number is out of range
  raise RangeError, message unless range.include?( self )
  
  # return self
  self
  
end

#limit(minimum_value, maximum_value) ⇒ Object

TODO: document me



122
123
124
125
126
127
# File 'lib/tdriver/util/common/numeric.rb', line 122

def limit( minimum_value, maximum_value )

  # limit current value
  min( minimum_value ).max( maximum_value )

end

#max(value) ⇒ Object

TODO: document me



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tdriver/util/common/numeric.rb', line 130

def max( value )

  if value.kind_of?( Numeric )

    self > value ? value : self

  else

    raise TypeError, "wrong type #{ value.class } for value (expected Numeric)"

  end

end

#min(value) ⇒ Object

TODO: document me



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tdriver/util/common/numeric.rb', line 145

def min( value )

  if value.kind_of?( Numeric )

    self < value ? value : self

  else

    raise TypeError, "wrong type #{ value.class } for value (expected Numeric)"

  end

end

#negative?Boolean

TODO: document me

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/tdriver/util/common/numeric.rb', line 45

def negative?

  self < 0

end

#non_negative?Boolean

TODO: document me

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/tdriver/util/common/numeric.rb', line 31

def non_negative?

  self >= 0

end

#non_positive?Boolean

TODO: document me

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/tdriver/util/common/numeric.rb', line 38

def non_positive?

  self <= 0  

end

#not_negative(message = 'Given value ($1) must not be negative', exception = ArgumentError) ⇒ Object

TODO: document me



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tdriver/util/common/numeric.rb', line 52

def not_negative( message = 'Given value ($1) must not be negative', exception = ArgumentError )

  if negative?

    # replace macros
    message.gsub!( '$1', inspect )

    raise exception, message, caller

  end

  self
  
end

#not_positive(message = 'Given value ($1) must not be positive', exception = ArgumentError) ⇒ Object

TODO: document me



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tdriver/util/common/numeric.rb', line 84

def not_positive( message = 'Given value ($1) must not be positive', exception = ArgumentError )

  if positive?

    # replace macros
    message.gsub!( '$1', inspect )

    raise exception, message, caller

  end

  self

end

#not_zero(message = 'Given value must not be zero', exception = ArgumentError) ⇒ Object

TODO: document me



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tdriver/util/common/numeric.rb', line 68

def not_zero( message = 'Given value must not be zero', exception = ArgumentError )

  if zero?

    # replace macros
    message.gsub!( '$1', inspect )

    raise exception, message, caller

  end
    
  self

end

#positive?Boolean

TODO: document me

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/tdriver/util/common/numeric.rb', line 24

def positive?

  self > 0

end