Last active
September 12, 2017 17:57
-
-
Save ZempTime/46caa40bafbdb19699700ca78aecc6df to your computer and use it in GitHub Desktop.
little nested helper class to store some state locally in yaml since the aws-sdk v2 for ruby doesn't support tagging, and thats what we gotta use
This file contains hidden or 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 "yaml" | |
class LocalStorage | |
class DocumentIdAlreadyExists < StandardError; end | |
class DocumentKeyAlreadyExists < StandardError; end | |
# YAML format per-environment | |
# env: { | |
# corrected_doc_ids: [], | |
# corrected_doc_keys: [], | |
# destroyable_doc_keys:[] | |
# } | |
CORRECTED_DOC_IDS = "corrected_doc_ids" | |
CORRECTED_DOC_KEYS = "corrected_doc_keys" | |
DESTROYABLE_DOC_KEYS = "destroyable_doc_keys" | |
def self.read_hash | |
YAML.load_file("#{Rails.root}/lib/corrected_s3_storage.yml") | |
end | |
def self.write_hash(new_hash) | |
File.open("#{Rails.root}/lib/corrected_s3_storage.yml", "r+") do |file| | |
file.write(new_hash.to_yaml) | |
end | |
end | |
def self.corrected_doc_ids | |
read_hash.fetch(env, {}) | |
.fetch(CORRECTED_DOC_IDS, []) | |
end | |
def self.corrected_doc_keys | |
read_hash.fetch(env, {}) | |
.fetch(CORRECTED_DOC_KEYS, []) | |
end | |
def self.destroyable_doc_keys | |
read_hash.fetch(env, {}) | |
.fetch(DESTROYABLE_DOC_KEYS, []) | |
end | |
def self.add_corrected_doc_id(env=Rails.env, id) | |
raise DocumentIdAlreadyExists.new("Document id: #{id} in env: #{env} already exists.") if corrected_doc_ids.include?(id) | |
hash = read_hash | |
hash.fetch(env, {}) | |
.fetch(CORRECTED_DOC_IDS, []) | |
.push(id) | |
write_hash(hash) | |
end | |
def self.add_corrected_doc_key(env=Rails.env, key) | |
raise DocumentKeyAlreadyExists.new("Document with key: #{key} in env: #{env} already exists.") if corrected_doc_keys.include?(key) | |
hash = read_hash | |
hash.fetch(env, {}) | |
.fetch(CORRECTED_DOC_KEYS, []) | |
.push(key) | |
write_hash(hash) | |
end | |
def self.add_destroyable_doc_key(env=Rails.env, key) | |
hash = read_hash | |
hash.fetch(env, {}) | |
.fetch(DESTROYABLE_DOC_KEYS, []) | |
.push(key) | |
hash[env][DESTROYABLE_DOC_KEYS].uniq! | |
write_hash(hash) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment