Class: EY::Stonith::History
- Inherits:
-
Object
- Object
- EY::Stonith::History
- Defined in:
- lib/ey_stonith/history.rb
Constant Summary collapse
- SEPARATOR =
' -> '
Instance Method Summary collapse
- #<<(status) ⇒ Object
- #include?(status) ⇒ Boolean
-
#initialize(path, length = 2) ⇒ History
constructor
A new instance of History.
- #last ⇒ Object
- #read ⇒ Object
- #reset ⇒ Object
- #to_s ⇒ Object
- #write(current) ⇒ Object
Constructor Details
#initialize(path, length = 2) ⇒ History
Returns a new instance of History.
8 9 10 11 12 13 14 |
# File 'lib/ey_stonith/history.rb', line 8 def initialize(path, length = 2) raise ArgumentError, "Hey, I need at least 2 history lengthinesses to be a history object." if length < 2 @length = length @path = Pathname.new(path) @path.dirname.mkpath FileUtils.touch(@path) end |
Instance Method Details
#<<(status) ⇒ Object
16 17 18 19 |
# File 'lib/ey_stonith/history.rb', line 16 def <<(status) write(status) unless last.to_s == status.to_s self end |
#include?(status) ⇒ Boolean
21 22 23 |
# File 'lib/ey_stonith/history.rb', line 21 def include?(status) read.include?(status.to_s) end |
#last ⇒ Object
32 |
# File 'lib/ey_stonith/history.rb', line 32 def last() read.last end |
#read ⇒ Object
33 |
# File 'lib/ey_stonith/history.rb', line 33 def read() @path.read.to_s.strip.split(SEPARATOR) end |
#reset ⇒ Object
26 |
# File 'lib/ey_stonith/history.rb', line 26 def reset() @path.truncate(0) end |
#to_s ⇒ Object
25 |
# File 'lib/ey_stonith/history.rb', line 25 def to_s() read.join(SEPARATOR) end |
#write(current) ⇒ Object
27 28 29 30 |
# File 'lib/ey_stonith/history.rb', line 27 def write(current) last_status = last @path.open('w') { |file| file << [last_status, current].compact.join(SEPARATOR); file.close } end |