Class: Lexdrill::Counter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Counter

Returns a new instance of Counter.



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

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/lexdrill/counter.rb', line 4

def path
  @path
end

Instance Method Details

#bounded_value(size) ⇒ Object

The current value, unless it's out of bounds for the given size, in which case it resets to 0 first.



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

def bounded_value(size)
  current = value
  return current if current < size

  reset
  0
end

#incrementObject



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

def increment
  File.write(path, (value + 1).to_s)
end

#resetObject



20
21
22
# File 'lib/lexdrill/counter.rb', line 20

def reset
  File.write(path, "0")
end

#valueObject



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

def value
  return 0 unless File.exist?(path)

  File.read(path).strip.to_i
end