Class: SimpleStatsStore::FileDump
- Inherits:
-
Object
- Object
- SimpleStatsStore::FileDump
- Defined in:
- lib/simple_stats_store/file_dump.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #files_contents ⇒ Object
-
#initialize(dir, options = {}) ⇒ FileDump
constructor
A new instance of FileDump.
- #write(model, data) ⇒ Object
Constructor Details
#initialize(dir, options = {}) ⇒ FileDump
Returns a new instance of FileDump.
5 6 7 8 |
# File 'lib/simple_stats_store/file_dump.rb', line 5 def initialize(dir, = {}) @dir = dir @max = .has_key?(:max) ? [:max] : nil end |
Instance Method Details
#each(&block) ⇒ Object
26 27 28 |
# File 'lib/simple_stats_store/file_dump.rb', line 26 def each(&block) files_contents.each &block end |
#files_contents ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/simple_stats_store/file_dump.rb', line 10 def files_contents contents = [] Dir["#{@dir}/**/*.stats"].each do |f| begin data = File.open(f, 'r').read if /\n---\n$/.match(data) contents << data File.delete(f) end rescue Errno::ENOENT puts "Failed to open file #{f}" end end contents end |
#write(model, data) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/simple_stats_store/file_dump.rb', line 30 def write(model, data) i = 0 subdir = File.(model.to_s, @dir) Dir.mkdir(subdir) if ! Dir.exists?(subdir) if ! @max.nil? files = Dir["#{subdir}/*"].sort { |a, b| File.ctime(a) <=> File.ctime(b) } if files.length >= @max files[0..(@max-files.length)].each do |f| File.delete(f) end end end while File.exists?(File.("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", subdir)) i += 1 end File.open(File.("sss-#{$$}-#{Time.new.to_i}-#{i}.stats", subdir), 'w') do |f| f.puts "---" f.puts model.to_s data.each do |key, value| f.puts "#{key.to_s}: #{value}" end f.puts "---" end end |