Class: Rockit::HashStore
- Inherits:
-
Object
- Object
- Rockit::HashStore
- Defined in:
- lib/rockit/hash_store.rb
Constant Summary collapse
- DIR =
'.rockit'
- FILE =
'hash'
Instance Attribute Summary collapse
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#filename ⇒ Object
Returns the value of attribute filename.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
-
#initialize(dir = DIR, filename = FILE) ⇒ HashStore
constructor
A new instance of HashStore.
Constructor Details
#initialize(dir = DIR, filename = FILE) ⇒ HashStore
Returns a new instance of HashStore.
10 11 12 13 |
# File 'lib/rockit/hash_store.rb', line 10 def initialize(dir=DIR, filename=FILE) @dir = dir @filename = filename end |
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
8 9 10 |
# File 'lib/rockit/hash_store.rb', line 8 def dir @dir end |
#filename ⇒ Object
Returns the value of attribute filename.
8 9 10 |
# File 'lib/rockit/hash_store.rb', line 8 def filename @filename end |
Instance Method Details
#[](key) ⇒ Object
21 22 23 24 25 |
# File 'lib/rockit/hash_store.rb', line 21 def [](key) if m = File.read(filepath).match(/^#{key}:(.*)$/) m[1] end end |
#[]=(key, value) ⇒ Object
27 28 29 30 31 |
# File 'lib/rockit/hash_store.rb', line 27 def []=(key, value) contents = File.read(filepath) contents << "#{key}:#{value}\n" unless contents.gsub!(/^#{key}:.*$/, "#{key}:#{value}") File.open(filepath, 'w') {|f| f.write(contents) } end |
#clear ⇒ Object
15 16 17 18 19 |
# File 'lib/rockit/hash_store.rb', line 15 def clear p = File.join(@dir,@filename) File.delete(p) if File.exists?(p) Dir.rmdir(@dir) if File.exists?(@dir) end |