Class: Believer::Counter

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

Overview

The counter value

Instance Method Summary collapse

Constructor Details

#initialize(v = 0, initial_val = nil) ⇒ Counter

Returns a new instance of Counter.



6
7
8
9
# File 'lib/believer/counter.rb', line 6

def initialize(v = 0, initial_val = nil)
  @value = v
  @initial_value = initial_val.nil? ? @value : initial_val
end

Instance Method Details

#adopt_value(v) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/believer/counter.rb', line 26

def adopt_value(v)
  if v.nil?
    @value = 0
    return self
  end
  @value = v.to_i
  self
end

#changed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/believer/counter.rb', line 57

def changed?
  diff > 0
end

#decr(val = 1) ⇒ Object



44
45
46
47
# File 'lib/believer/counter.rb', line 44

def decr(val = 1)
  @value = @value - val
  self
end

#decremented?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/believer/counter.rb', line 49

def decremented?
  initial_value > @value
end

#diffObject



53
54
55
# File 'lib/believer/counter.rb', line 53

def diff
  (@value - initial_value).abs
end

#incr(val = 1) ⇒ Object



35
36
37
38
# File 'lib/believer/counter.rb', line 35

def incr(val = 1)
  @value = @value + val
  self
end

#incremented?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/believer/counter.rb', line 40

def incremented?
  @value > initial_value
end

#initial_valueObject



66
67
68
69
70
71
# File 'lib/believer/counter.rb', line 66

def initial_value
  if @initial_value.nil?
    @initial_value = self.to_i
  end
  @initial_value
end

#reconcile!Object



11
12
13
14
# File 'lib/believer/counter.rb', line 11

def reconcile!
  @initial_value = @value
  self
end

#reset!Object



16
17
18
19
# File 'lib/believer/counter.rb', line 16

def reset!
  @value = 0
  self
end

#undo_changes!Object



21
22
23
24
# File 'lib/believer/counter.rb', line 21

def undo_changes!
  @value = initial_value
  self
end

#valueObject Also known as: to_i



61
62
63
# File 'lib/believer/counter.rb', line 61

def value
  @value
end