Class: Blur::ScriptCache

Inherits:
Object
  • 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.



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

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

  if File.exist? 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
13
14
# File 'library/blur/script_cache.rb', line 12

def [] key
  @hash[key]
end

#[]=(key, value) ⇒ Object

Sets the cache key to the provided value.



17
18
19
# File 'library/blur/script_cache.rb', line 17

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

#saveObject

Saves the cache as a YAML file.



22
23
24
25
26
27
28
29
30
# File 'library/blur/script_cache.rb', line 22

def save
  directory = File.dirname @path

  Dir.mkdir directory unless File.directory? directory

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