Created
October 9, 2018 20:17
-
-
Save atog/09625e3c6389e5df602157154e0ce9d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'psych' | |
class MemoryPicker | |
STORAGE_NAME = "picked.yml" | |
def initialize(path) | |
@path = path | |
@storage = read_or_initialize_storage | |
end | |
def pick | |
memory_options = Dir.glob(File.join(@path, "**/*.jpg")) | |
return if memory_options.empty? | |
option = @storage.last | |
while @storage.include?(option) || !option do | |
option = memory_options.sample | |
end | |
@storage << option | |
save | |
option | |
end | |
def read_or_initialize_storage | |
return [] unless File.exists?(storage_path) | |
Psych.load_file(storage_path, fallback: []) | |
end | |
def storage_path | |
@storage_path ||= File.join(@path, STORAGE_NAME) | |
end | |
def save | |
Psych.dump(@storage, File.open(storage_path, "w")) | |
end | |
end | |
if __FILE__ == $0 | |
puts MemoryPicker.new(ARGV[0]).pick | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment