lib/signore/database.rb in signore-0.0.0 vs lib/signore/database.rb in signore-0.1.0

- old
+ new

@@ -1,32 +1,27 @@ # encoding: UTF-8 module Signore class Database - def self.db - @db + def initialize path + @store = YAML::Store.new path end - def self.find opts = {} - opts = {tags: [], no_tags: []}.merge opts - @db - .select { |sig| opts[:tags].all? { |tag| sig.tagged_with? tag } } - .reject { |sig| opts[:no_tags].any? { |tag| sig.tagged_with? tag } } - .shuffle.first + def << sig + @store.transaction do + @store['signatures'] ||= [] + @store['signatures'] << sig + end end - def self.load path - @path = path - @db = File.exists?(@path) ? YAML.load_file(@path) : [] - end + def find opts = {} + opts = { tags: [], no_tags: [] }.merge opts - def self.min_yaml - @db.to_yaml.gsub /^ (author|source|subject|tags): !!null \n/, '' - end - - def self.save sig - @db << sig - FileUtils.mkpath File.dirname @path - File.open(@path, 'w') { |file| file << self.min_yaml } + @store.transaction true do + @store['signatures'] + .select { |sig| opts[:tags].all? { |tag| sig.tagged_with? tag } } + .reject { |sig| opts[:no_tags].any? { |tag| sig.tagged_with? tag } } + .shuffle.first + end end end end