class Blufin::YmlCacheHandler

Constants

CONFIG
FORMAT_DATE
FORMAT_EXTENSION
KEY_DATA
KEY_DATE
SCHEMA_CONFIG
SCHEMA_DATA
SCHEMA_DESCRIPTIONS
SCHEMA_FKS
SCHEMA_FKS_DEPENDENCIES
SCHEMA_FKS_PLACEHOLDERS
SCHEMA_RESOURCES
VALID_KEYS

Public Class Methods

get(site, key, with_date = false) click to toggle source

Retrieves a Yaml HASH. @return Hash

# File lib/core/yml/yml_cache_handler.rb, line 50
def self.get(site, key, with_date = false)

    files = get_files_for_key(site, key)

    # Throw crude error if cache file doesn't exist.
    # TODO - This currently throws RunTimeError but should ideally run 'blufin s g' or something to rebuild the cache automatically...
    raise RuntimeError, "Cache file for key '#{key}' doesn't exist. Please run: #{Blufin::Terminal::format_command("blufin s g #{site}")}" unless files.any?

    # Get the last file -- although there should never be more than 1.
    file = files[files.length - 1]

    date = File.basename(file).gsub("#{key}-", '').gsub(FORMAT_EXTENSION, '')
    date = DateTime.strptime(date, FORMAT_DATE)
    date = date.strftime("%A \xe2\x80\x94 %B %d [%H:%M:%S]")

    hash = {}

    eval("hash = #{Blufin::Files::read_file(file)[0]}")

    return with_date ? { KEY_DATE => date.to_s, KEY_DATA => hash } : hash

end
store(site, key, data) click to toggle source

Stores a Yaml HASH. @return void

# File lib/core/yml/yml_cache_handler.rb, line 37
def self.store(site, key, data)

    Blufin::SiteResolver::validate_site(site)

    validate_key(key)
    delete_pre_existing_cache_files(site, key)

    Blufin::Files::write_file_string("#{get_cache_directory(site)}/#{key}-#{get_timestamp}#{FORMAT_EXTENSION}", data.inspect)

end

Private Class Methods

delete_pre_existing_cache_files(site, key) click to toggle source

Removes any pre-existing cache files from the /tmp dir. @return void

# File lib/core/yml/yml_cache_handler.rb, line 83
def self.delete_pre_existing_cache_files(site, key)
    get_files_for_key(site, key).each do |file|
        Blufin::Files::delete_file(file)
    end
end
get_cache_directory(site) click to toggle source

Get the cache directory. @return String

# File lib/core/yml/yml_cache_handler.rb, line 101
def self.get_cache_directory(site)
    cache_directory = "/tmp/cache-#{Blufin::SiteResolver::get_site_name(site)}/"
    Blufin::Files::create_directory(cache_directory) unless Blufin::Files::path_exists(cache_directory)
    cache_directory
end
get_files_for_key(site, key) click to toggle source

Gets an Array of files for specific key. @return Array

# File lib/core/yml/yml_cache_handler.rb, line 91
def self.get_files_for_key(site, key)
    files_return = []
    Blufin::Files::get_files_in_dir(get_cache_directory(site)).each do |file|
        files_return << file if File.basename(file) =~ /\A#{key}-[0-9-]+#{FORMAT_EXTENSION}\z/
    end
    files_return
end
get_timestamp() click to toggle source

Get a timestamp of the current time. @return String

# File lib/core/yml/yml_cache_handler.rb, line 109
def self.get_timestamp
    Time.now.strftime(FORMAT_DATE)
end
validate_key(key) click to toggle source

Makes sure the 'key' is a constant within this class. @return void

# File lib/core/yml/yml_cache_handler.rb, line 77
def self.validate_key(key)
    raise RuntimeError, "Invalid YmlCacheHandler key: #{key}" unless VALID_KEYS.include?(key)
end