Class: ConvenientService::Support::Counter Private
- Inherits:
-
Object
- Object
- ConvenientService::Support::Counter
- Defined in:
- lib/convenient_service/support/counter.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Direct Known Subclasses
Defined Under Namespace
Modules: Exceptions
Instance Attribute Summary collapse
- #current_value ⇒ Integer
- #initial_value ⇒ Object readonly private
- #max_value ⇒ Object readonly private
- #min_value ⇒ Object readonly private
Instance Method Summary collapse
-
#bdecrement(n = 1) ⇒ Boolean
private
bdecrementmeans boolean decrement. -
#bincrement(n = 1) ⇒ Boolean
private
bincrementmeans boolean increment. - #decrement(n = 1) ⇒ Integer private
- #decrement!(n = 1) ⇒ Integer private
- #increment(n = 1) ⇒ Integer private
- #increment!(n = 1) ⇒ Integer private
- #initialize(initial_value: 0, min_value: -::Float::INFINITY,, max_value: ::Float::INFINITY) ⇒ void constructor private
- #reset ⇒ Integer private
Constructor Details
#initialize(initial_value: 0, min_value: -::Float::INFINITY,, max_value: ::Float::INFINITY) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Do NOT rely on the fact that min_value and max_value are almost always Integer instances since they are set to -Float::INFINITY and Float::INFINITY by default.
Float::INFINITY and Integer are just contextual ducks, NOT full ducks. For example, Float::INFINITY.to_i raises FloatDomainError.
94 95 96 97 98 99 |
# File 'lib/convenient_service/support/counter.rb', line 94 def initialize(initial_value: 0, min_value: -::Float::INFINITY, max_value: ::Float::INFINITY) @initial_value = initial_value @current_value = initial_value @min_value = min_value @max_value = max_value end |
Instance Attribute Details
#current_value ⇒ Integer
71 72 73 |
# File 'lib/convenient_service/support/counter.rb', line 71 def current_value @current_value end |
#initial_value ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
65 66 67 |
# File 'lib/convenient_service/support/counter.rb', line 65 def initial_value @initial_value end |
#max_value ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
83 84 85 |
# File 'lib/convenient_service/support/counter.rb', line 83 def max_value @max_value end |
#min_value ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
77 78 79 |
# File 'lib/convenient_service/support/counter.rb', line 77 def min_value @min_value end |
Instance Method Details
#bdecrement(n = 1) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
bdecrement means boolean decrement. Works exactly in the same way as decrement except returns a boolean value.
If decremented successfully then returns true, otherwise - returns false.
199 200 201 202 203 204 205 |
# File 'lib/convenient_service/support/counter.rb', line 199 def bdecrement(n = 1) return false if @current_value - n < @min_value @current_value -= n true end |
#bincrement(n = 1) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
bincrement means boolean increment. Works exactly in the same way as increment except returns a boolean value.
If incremented successfully then returns true, otherwise - returns false.
146 147 148 149 150 151 152 |
# File 'lib/convenient_service/support/counter.rb', line 146 def bincrement(n = 1) return false if @current_value + n > @max_value @current_value += n true end |
#decrement(n = 1) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
165 166 167 168 169 |
# File 'lib/convenient_service/support/counter.rb', line 165 def decrement(n = 1) return @current_value if @current_value - n < @min_value @current_value -= n end |
#decrement!(n = 1) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
183 184 185 186 187 |
# File 'lib/convenient_service/support/counter.rb', line 183 def decrement!(n = 1) ::ConvenientService.raise Exceptions::ValueAfterDecrementIsLowerThanMinValue.new(n: n, current_value: @current_value, min_value: @min_value) if @current_value - n < @min_value @current_value -= n end |
#increment(n = 1) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
112 113 114 115 116 |
# File 'lib/convenient_service/support/counter.rb', line 112 def increment(n = 1) return @current_value if @current_value + n > @max_value @current_value += n end |
#increment!(n = 1) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
130 131 132 133 134 |
# File 'lib/convenient_service/support/counter.rb', line 130 def increment!(n = 1) ::ConvenientService.raise Exceptions::ValueAfterIncrementIsGreaterThanMaxValue.new(n: n, current_value: @current_value, max_value: @max_value) if @current_value + n > @max_value @current_value += n end |
#reset ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
213 214 215 |
# File 'lib/convenient_service/support/counter.rb', line 213 def reset @current_value = @initial_value end |