Class: ConvenientService::Support::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/convenient_service/support/counter.rb

Direct Known Subclasses

ThreadSafeCounter

Defined Under Namespace

Modules: Exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_value: 0, min_value: -::Float::INFINITY,, max_value: ::Float::INFINITY) ⇒ void

Note:

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.

Note:

Float::INFINITY and Integer are just contextual ducks, NOT full ducks. For example, Float::INFINITY.to_i raises FloatDomainError.

Parameters:

  • initial_value (Integer) (defaults to: 0)
  • min_value (Integer, -::Float::Infinity) (defaults to: -::Float::INFINITY,)
  • max_value (Integer, Float::Infinity) (defaults to: ::Float::INFINITY)


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_valueInteger

Returns:

  • (Integer)


71
72
73
# File 'lib/convenient_service/support/counter.rb', line 71

def current_value
  @current_value
end

#initial_valueObject (readonly)

Returns the value of attribute initial_value.



65
66
67
# File 'lib/convenient_service/support/counter.rb', line 65

def initial_value
  @initial_value
end

#max_valueObject (readonly)

Returns the value of attribute max_value.



83
84
85
# File 'lib/convenient_service/support/counter.rb', line 83

def max_value
  @max_value
end

#min_valueObject (readonly)

Returns the value of attribute min_value.



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

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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Boolean)


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

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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Boolean)


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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)


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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Raises:



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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)


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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Raises:



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

#resetInteger

Returns:

  • (Integer)


213
214
215
# File 'lib/convenient_service/support/counter.rb', line 213

def reset
  @current_value = @initial_value
end