Skip to content

Instantly share code, notes, and snippets.

@atog
Created October 9, 2018 20:17
Show Gist options
  • Save atog/09625e3c6389e5df602157154e0ce9d8 to your computer and use it in GitHub Desktop.
Save atog/09625e3c6389e5df602157154e0ce9d8 to your computer and use it in GitHub Desktop.
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