Module: CounterContainer

Defined in:
lib/counter_container.rb

Overview

Provides counting functionality for @counter. Affords incrementation, decrementation and resetting the count via convenient methods.

For this module to work, @counter and @start_val are assumed to be of type Integer.

Constant Summary collapse

COUNTING_INSTANCE_OBJECT_IS_NIL =
'The counting instance attribute @counter was nil.'
COUNTING_INSTANCE_OBJECT_IS_NON_INTEGER =
'The counting instance attribute @counter is not an Integer even though it should be.'
START_VAL_INSTANCE_OBJECT_IS_NIL =
'The starting value attribute @start_val was nil.'
START_VAL_INSTANCE_OBJECT_IS_NON_INTEGER =
'The starting value attribute @start_val is not an Integer even though it should be.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#counter=(value) ⇒ Object (writeonly)

sets @counter, the object holding the value of the current count



19
20
21
# File 'lib/counter_container.rb', line 19

def counter=(value)
  @counter = value
end

#start_val=(value) ⇒ Object (writeonly)

sets @start_val, the default value of the counter



15
16
17
# File 'lib/counter_container.rb', line 15

def start_val=(value)
  @start_val = value
end

Instance Method Details

#countInteger

Gets the current count.

Returns:

  • (Integer)

    a duplicate of @counter

Raises:



49
50
51
52
53
# File 'lib/counter_container.rb', line 49

def count
  raise COUNTING_INSTANCE_OBJECT_IS_NIL if @counter.nil?
  raise COUNTING_INSTANCE_OBJECT_IS_NON_INTEGER if @counter.class != Integer
  @counter
end

#dec!Object

Synonymous method for CounterContainer#increment!



40
41
42
43
44
45
# File 'lib/counter_container.rb', line 40

def dec!
  raise COUNTING_INSTANCE_OBJECT_IS_NIL if @counter.nil?
  raise COUNTING_INSTANCE_OBJECT_IS_NON_INTEGER if @counter.class != Integer

  decrement!
end

#decrement!Object

Decrements @counter by Integer value of 1



35
36
37
# File 'lib/counter_container.rb', line 35

def decrement!
  @counter -= 1
end

#inc!Object

Synonymous method for CounterContainer#increment!



30
31
32
# File 'lib/counter_container.rb', line 30

def inc!
  increment!
end

#increment!Object

Increments @counter by Integer value of 1



22
23
24
25
26
27
# File 'lib/counter_container.rb', line 22

def increment!
  raise COUNTING_INSTANCE_OBJECT_IS_NIL if @counter.nil?
  raise COUNTING_INSTANCE_OBJECT_IS_NON_INTEGER if @counter.class != Integer

  @counter += 1
end

#reset!Object

Resets the value of @counter to @basis



56
57
58
59
60
61
62
63
# File 'lib/counter_container.rb', line 56

def reset!
  raise COUNTING_INSTANCE_OBJECT_IS_NIL if @counter.nil?
  raise COUNTING_INSTANCE_OBJECT_IS_NON_INTEGER if @counter.class != Integer
  raise START_VAL_INSTANCE_OBJECT_IS_NIL if @start_val.nil?
  raise START_VAL_INSTANCE_OBJECT_IS_NON_INTEGER if @start_val.class != Integer

  @counter = @start_val.dup
end