Class: ConvenientService::Support::Counter
- Inherits:
-
Object
- Object
- ConvenientService::Support::Counter
- Defined in:
- lib/convenient_service/support/counter.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Exceptions
Instance Attribute Summary collapse
- #current_value ⇒ Integer
-
#initial_value ⇒ Object
readonly
Returns the value of attribute initial_value.
-
#max_value ⇒ Object
readonly
Returns the value of attribute max_value.
-
#min_value ⇒ Object
readonly
Returns the value of attribute min_value.
Instance Method Summary collapse
-
#bdecrement(n = 1) ⇒ Boolean
‘bdecrement` means boolean decrement.
-
#bincrement(n = 1) ⇒ Boolean
‘bincrement` means boolean increment.
- #decrement(n = 1) ⇒ Integer
- #decrement!(n = 1) ⇒ Integer
- #increment(n = 1) ⇒ Integer
- #increment!(n = 1) ⇒ Integer
- #initialize(initial_value: 0, min_value: -::Float::INFINITY,, max_value: ::Float::INFINITY) ⇒ void constructor
- #reset ⇒ Integer
Constructor Details
#initialize(initial_value: 0, min_value: -::Float::INFINITY,, max_value: ::Float::INFINITY) ⇒ void
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`.
89 90 91 92 93 94 |
# File 'lib/convenient_service/support/counter.rb', line 89 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
66 67 68 |
# File 'lib/convenient_service/support/counter.rb', line 66 def current_value @current_value end |
#initial_value ⇒ Object (readonly)
Returns the value of attribute initial_value.
60 61 62 |
# File 'lib/convenient_service/support/counter.rb', line 60 def initial_value @initial_value end |
#max_value ⇒ Object (readonly)
Returns the value of attribute max_value.
78 79 80 |
# File 'lib/convenient_service/support/counter.rb', line 78 def max_value @max_value end |
#min_value ⇒ Object (readonly)
Returns the value of attribute min_value.
72 73 74 |
# File 'lib/convenient_service/support/counter.rb', line 72 def min_value @min_value end |
Instance Method Details
#bdecrement(n = 1) ⇒ Boolean
‘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`.
194 195 196 197 198 199 200 |
# File 'lib/convenient_service/support/counter.rb', line 194 def bdecrement(n = 1) return false if @current_value - n < @min_value @current_value -= n true end |
#bincrement(n = 1) ⇒ Boolean
‘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`.
141 142 143 144 145 146 147 |
# File 'lib/convenient_service/support/counter.rb', line 141 def bincrement(n = 1) return false if @current_value + n > @max_value @current_value += n true end |
#decrement(n = 1) ⇒ Integer
160 161 162 163 164 |
# File 'lib/convenient_service/support/counter.rb', line 160 def decrement(n = 1) return @current_value if @current_value - n < @min_value @current_value -= n end |
#decrement!(n = 1) ⇒ Integer
178 179 180 181 182 |
# File 'lib/convenient_service/support/counter.rb', line 178 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
107 108 109 110 111 |
# File 'lib/convenient_service/support/counter.rb', line 107 def increment(n = 1) return @current_value if @current_value + n > @max_value @current_value += n end |
#increment!(n = 1) ⇒ Integer
125 126 127 128 129 |
# File 'lib/convenient_service/support/counter.rb', line 125 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
208 209 210 |
# File 'lib/convenient_service/support/counter.rb', line 208 def reset @current_value = @initial_value end |