From 8f95084cd854aef4e3493854e58cefd352cdc68d Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Mon, 27 Jun 2005 21:44:46 +0000 Subject: renaming blink to puppet git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@302 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/storage.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/puppet/storage.rb (limited to 'lib/puppet/storage.rb') diff --git a/lib/puppet/storage.rb b/lib/puppet/storage.rb new file mode 100644 index 000000000..9fc21d38b --- /dev/null +++ b/lib/puppet/storage.rb @@ -0,0 +1,48 @@ +# $Id$ + +module Blink + # a class for storing state + class Storage + include Singleton + @@state = Hash.new { |hash,key| + hash[key] = Hash.new(nil) + } + @@splitchar = "\t" + + def initialize + self.class.load + end + + def Storage.load + # XXX I should probably use a better default state dir + Blink[:statefile] ||= "/var/tmp/blinkstate" + return unless File.exists?(Blink[:statefile]) + File.open(Blink[:statefile]) { |file| + file.gets { |line| + myclass, key, value = line.split(@@splitchar) + + @@state[myclass][key] = Marshal::load(value) + } + } + end + + def Storage.state(myclass) + unless myclass.is_a? Class + myclass = myclass.class + end + result = @@state[myclass] + return result + end + + def Storage.store + File.open(Blink[:statefile], File::CREAT|File::WRONLY, 0600) { |file| + @@state.each { |klass, thash| + thash.each { |key,value| + mvalue = Marshal::dump(value) + file.puts([klass,key,mvalue].join(@@splitchar)) + } + } + } + end + end +end -- cgit