Class: ConvenientService::Support::Counter Private

Inherits:
Object
  • Object
show all
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.

Since:

  • 1.0.0

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

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.

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)

Since:

  • 1.0.0



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)

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.

Since:

  • 1.0.0



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

def initial_value
  @initial_value
end

#max_valueObject (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.

Since:

  • 1.0.0



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

def max_value
  @max_value
end

#min_valueObject (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.

Since:

  • 1.0.0



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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Boolean)

Since:

  • 1.0.0



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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Boolean)

Since:

  • 1.0.0



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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Since:

  • 1.0.0



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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Raises:

Since:

  • 1.0.0



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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Since:

  • 1.0.0



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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Raises:

Since:

  • 1.0.0



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

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.

Returns:

  • (Integer)

Since:

  • 1.0.0



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

def reset
  @current_value = @initial_value
end