lib/signore/database.rb in signore-0.1.2 vs lib/signore/database.rb in signore-0.2.0

- old
+ new

@@ -1,27 +1,24 @@ -# encoding: UTF-8 - module Signore class Database - def initialize path @store = YAML::Store.new path end def << sig - @store.transaction do - @store['signatures'] ||= [] - @store['signatures'] << sig + store.transaction do + store['signatures'] ||= [] + store['signatures'] << sig end end - def find opts = {} - opts = { tags: [], no_tags: [] }.merge opts - - @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 + 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 end end + attr_reader :store + private :store end end