Class: Blur::ScriptCache

Inherits:
Hash
  • Object
show all
Defined in:
library/blur/script_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script_name, path, hash) ⇒ ScriptCache

Returns a new instance of ScriptCache.



5
6
7
8
9
# File 'library/blur/script_cache.rb', line 5

def initialize script_name, path, hash
  @script_name = script_name
  @path = path
  @hash = hash
end

Class Method Details

.load(script_name, cache_dir) ⇒ Object

Loads the cache file for script_name in cache_dir if it exists.



31
32
33
34
35
36
37
38
39
40
41
# File 'library/blur/script_cache.rb', line 31

def self.load script_name, cache_dir
  cache_path = File.join cache_dir, "#{script_name}.yml"

  if File.exists? cache_path
    object = YAML.load_file cache_path

    ScriptCache.new script_name, cache_path, object
  else
    ScriptCache.new script_name, cache_path, {}
  end
end

Instance Method Details

#[](key) ⇒ Object

Gets a cache value by its key.



12
# File 'library/blur/script_cache.rb', line 12

def [] key; @hash[key] end

#[]=(key, value) ⇒ Object

Sets the cache key to the provided value.



15
# File 'library/blur/script_cache.rb', line 15

def []= key, value; @hash[key] = value end

#saveObject

Saves the cache as a YAML file.



18
19
20
21
22
23
24
25
26
27
28
# File 'library/blur/script_cache.rb', line 18

def save
  directory = File.dirname @path
  
  unless File.directory? directory
    Dir.mkdir directory
  end

  File.open @path, ?w do |file|
    YAML.dump @hash, file
  end
end