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)


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_valueInteger

Returns:

  • (Integer)


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

def current_value
  @current_value
end

#initial_valueObject (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_valueObject (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_valueObject (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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Boolean)


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.

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Boolean)


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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Raises:



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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (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

Parameters:

  • n (Integer) (defaults to: 1)

Returns:

  • (Integer)

Raises:



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

#resetInteger

Returns:

  • (Integer)


208
209
210
# File 'lib/convenient_service/support/counter.rb', line 208

def reset
  @current_value = @initial_value
end