lib/signore/database.rb in signore-0.2.2 vs lib/signore/database.rb in signore-0.2.3
- old
+ new
@@ -1,24 +1,37 @@
-module Signore class Database
- def initialize path
- @store = YAML::Store.new path
- end
+require 'fileutils'
+require 'yaml/store'
+require_relative 'settings'
+require_relative 'sig_finder'
+require_relative 'tags'
- def << sig
- store.transaction do
- store['signatures'] ||= []
- store['signatures'] << sig
+module Signore
+ class Database
+ def initialize(path: Settings.new.db_path, sig_finder: SigFinder)
+ @path = path
+ @sig_finder = sig_finder
+ initialise_store if path.zero? or not path.exist?
+ @store = YAML::Store.new(path)
end
- end
- def find forbidden_tags: [], random: Random.new, required_tags: []
- store.transaction true do
- store['signatures']
- .select { |sig| required_tags.all? { |tag| sig.tagged_with? tag } }
- .reject { |sig| forbidden_tags.any? { |tag| sig.tagged_with? tag } }
- .sample random: random
+ def <<(sig)
+ store.transaction { store['signatures'] << sig }
+ sig
end
- end
- attr_reader :store
- private :store
-end end
+ def find(tags: Tags.new)
+ sigs = store.transaction(true) { store['signatures'] }
+ sig_finder.find(sigs, tags: tags)
+ end
+
+ attr_reader :path, :sig_finder, :store
+ private :path, :sig_finder, :store
+
+ private
+
+ def initialise_store
+ FileUtils.mkdir_p path.dirname
+ FileUtils.touch path
+ YAML::Store.new(path).transaction { |store| store['signatures'] = [] }
+ end
+ end
+end