Class: EY::Stonith::History

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_stonith/history.rb

Constant Summary collapse

SEPARATOR =
' -> '

Instance Method Summary collapse

Constructor Details

#initialize(path, length = 2) ⇒ History

Returns a new instance of History.

Raises:

  • (ArgumentError)


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

Returns:

  • (Boolean)


21
22
23
# File 'lib/ey_stonith/history.rb', line 21

def include?(status)
  read.include?(status.to_s)
end

#lastObject



32
# File 'lib/ey_stonith/history.rb', line 32

def last() read.last end

#readObject



33
# File 'lib/ey_stonith/history.rb', line 33

def read() @path.read.to_s.strip.split(SEPARATOR) end

#resetObject



26
# File 'lib/ey_stonith/history.rb', line 26

def reset() @path.truncate(0) end

#to_sObject



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