lib/markov_uuid/storage.rb in markov_uuid-0.0.5 vs lib/markov_uuid/storage.rb in markov_uuid-0.0.6

- old
+ new

@@ -1,22 +1,54 @@ require "yaml" require "fileutils" module MarkovUuid class Storage - def initialize filename - @filename = filename + attr_accessor :data, :cache_file, :input_file + + #note these are not by any means guaranteed to be unique + #dependent on uuid length and corpus size + #maybe maintain uniqueness elsewhere by regenerating unless unique + def uuid + chain.uuid end - attr_accessor :data, :filename + def chain + @chain ||= MarkovUuid::Chain.new data + end + def initialize input_file, cache_file + @input_file, @cache_file = input_file, cache_file + + preload_data + end + + #for testing + def file_klass + File + end + + private + def save - File.open(filename, "w"){|f| YAML.dump(@data, f) } + file_klass.open(cache_file, "w"){|f| YAML.dump(@data, f) } end - def load - FileUtils.touch filename - File.open(filename) do |f| + def open + file_klass.open(cache_file) do |f| @data = YAML.load f + end + + @data + end + + def data + @data ||= MarkovUuid::Chain.from_string file_klass.read(input_file) + end + + def preload_data + unless open + data + save end end end end