Class: Lexdrill::Counter
- Inherits:
-
Object
- Object
- Lexdrill::Counter
- Defined in:
- lib/lexdrill/counter.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#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.
- #increment ⇒ Object
-
#initialize(path) ⇒ Counter
constructor
A new instance of Counter.
- #reset ⇒ Object
- #value ⇒ Object
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
#path ⇒ Object (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 |
#increment ⇒ Object
16 17 18 |
# File 'lib/lexdrill/counter.rb', line 16 def increment File.write(path, (value + 1).to_s) end |
#reset ⇒ Object
20 21 22 |
# File 'lib/lexdrill/counter.rb', line 20 def reset File.write(path, "0") end |
#value ⇒ Object
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 |