Class: Blufin::YmlCacheHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/core/yml/yml_cache_handler.rb

Constant Summary collapse

FORMAT_EXTENSION =
'.txt'
FORMAT_DATE =
'%Y-%d-%m-%H-%M-%S'
CONFIG =
'config'
SCHEMA_CONFIG =
'schema-config'
SCHEMA_DATA =
'schema-data'
SCHEMA_DESCRIPTIONS =
'schema-descriptions'
SCHEMA_FKS =
'schema-fks'
SCHEMA_FKS_DEPENDENCIES =
'schema-fks-dependencies'
SCHEMA_FKS_PLACEHOLDERS =
'schema-fks-placeholders'
'schema-fks-links'
SCHEMA_RESOURCES =
'schema-resources'
VALID_KEYS =
[
    CONFIG,
    SCHEMA_CONFIG,
    SCHEMA_DATA,
    SCHEMA_DESCRIPTIONS,
    SCHEMA_FKS,
    SCHEMA_FKS_DEPENDENCIES,
    SCHEMA_FKS_PLACEHOLDERS,
    SCHEMA_FKS_LINKS,
    SCHEMA_RESOURCES
]
KEY_DATE =
'date'
KEY_DATA =
'data'

Class Method Summary collapse

Class Method Details

.get(site, key, with_date = false) ⇒ Object

Retrieves a Yaml HASH.

Returns:

  • Hash

Raises:

  • (RuntimeError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object

Stores a Yaml HASH.

Returns:

  • void



37
38
39
40
41
42
43
44
45
46
# 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