Class: Blur::Script::Cache
- Inherits:
-
Object
- Object
- Blur::Script::Cache
- Defined in:
- library/blur/script/cache.rb
Overview
The Cache class enables data storing inside Blur and it scripts.
What it does is simply store a hash, and act like it is that hash.
When the client closes, it sends a message to all available scripts and then those scripts tells the cache to save, in order to remember that data and reload it at the next run.
Cache can then save the contents of the hash to a yaml file that persists in the ./cache/ directory.
That same file is then loaded once needed again.
Class Method Summary (collapse)
-
+ (Boolean) exists?(name)
Check if there exists a cache file for the script with name name.
-
+ (Object) path
Get the path to the cache directory (./cache/).
Instance Method Summary (collapse)
-
- (Object) [](key)
Get a cache value by key.
-
- (Object) []=(key, value)
Set a cache value by key.
-
- (Cache) initialize(script)
constructor
Instantiate a cache with a script reference.
-
- (Hash) load
Load a yaml file as internal data from the cache directory.
-
- (Object) save
Save all internal data to a yaml file in the cache directory.
-
- (Object) to_s
Let Hash#to_s do the job.
Constructor Details
- (Cache) initialize(script)
Instantiate a cache with a script reference.
35 36 37 38 |
# File 'library/blur/script/cache.rb', line 35 def initialize script @hash = {} @script = script end |
Class Method Details
+ (Boolean) exists?(name)
Check if there exists a cache file for the script with name name.
24 25 26 |
# File 'library/blur/script/cache.rb', line 24 def self.exists? name File.exists? "#{path}/#{name}.yml" end |
+ (Object) path
Get the path to the cache directory (./cache/)
19 20 21 |
# File 'library/blur/script/cache.rb', line 19 def self.path %{#{File.dirname File. $0}/cache} end |
Instance Method Details
- (Object) [](key)
Get a cache value by key.
29 |
# File 'library/blur/script/cache.rb', line 29 def [] key; @hash[key] end |
- (Object) []=(key, value)
Set a cache value by key.
32 |
# File 'library/blur/script/cache.rb', line 32 def []= key, value; @hash[key] = value end |
- (Hash) load
Load a yaml file as internal data from the cache directory.
56 57 58 59 60 61 62 |
# File 'library/blur/script/cache.rb', line 56 def load if yaml = YAML.load_file(path) @hash = yaml end rescue File.unlink path end |
- (Object) save
Save all internal data to a yaml file in the cache directory.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'library/blur/script/cache.rb', line 41 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 |
- (Object) to_s
Let Hash#to_s do the job.
65 |
# File 'library/blur/script/cache.rb', line 65 def to_s; @hash end |