Class: LS4::FileRelayTimeStampService::Stamp

Inherits:
Object
  • Object
show all
Includes:
RelayTimeStamp
Defined in:
lib/ls4/service/rts_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Stamp

Returns a new instance of Stamp.



31
32
33
34
35
# File 'lib/ls4/service/rts_file.rb', line 31

def initialize(path)
	@path = path
	@tmp_path = "#{@path}.tmp"
	@file = nil
end

Instance Method Details

#closeObject



37
38
39
40
41
42
43
# File 'lib/ls4/service/rts_file.rb', line 37

def close
	if @file
		@file.close
		@file = nil
	end
	nil
end

#getObject



45
46
47
48
49
50
51
52
53
# File 'lib/ls4/service/rts_file.rb', line 45

def get
	unless @file
		@file = File.open(@path)
	end
	@file.pos = 0
	@file.read.to_i
rescue
	0
end

#set(pos, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ls4/service/rts_file.rb', line 55

def set(pos, &block)
	close

	File.open(@tmp_path, "w") {|f|
		f.write(pos.to_s)
	}

	block.call if block

	File.rename(@tmp_path, @path)

	nil
end