Class: Keep::Disk
- Inherits:
-
Object
- Object
- Keep::Disk
- Defined in:
- lib/keep/disk.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#path ⇒ Object
The path to save the file.
Instance Method Summary collapse
-
#data ⇒ Object
The in-memory data structure representing the configuration you’re keeping.
-
#get(key) ⇒ Object
Returns the value of the specified key.
-
#initialize(path) ⇒ Disk
constructor
Creates a new Disk instance.
-
#keys ⇒ Object
Accesses all of the keys we’re keeping.
-
#present?(key) ⇒ Boolean
Searches for the key in the in-memory Hash.
-
#save ⇒ Object
Persists the in-memory hash to disk.
-
#set(key, value) ⇒ Object
Sets a value to a given key.
-
#touch ⇒ Object
Ensures the destination file is created.
Constructor Details
#initialize(path) ⇒ Disk
Creates a new Disk instance.
Returns nothing.
22 23 24 25 |
# File 'lib/keep/disk.rb', line 22 def initialize(path) @path = path touch end |
Instance Attribute Details
#path ⇒ Object
The path to save the file.
Returns a String path to the file.
10 11 12 |
# File 'lib/keep/disk.rb', line 10 def path @path end |
Instance Method Details
#data ⇒ Object
The in-memory data structure representing the configuration you’re keeping. Implemented by child classes.
Returns a Hash of configuration keys and values.
38 |
# File 'lib/keep/disk.rb', line 38 def data ; end |
#get(key) ⇒ Object
Returns the value of the specified key.
key - a String value for the key of this Hash
Returns the value if found, otherwise nil.
62 63 64 |
# File 'lib/keep/disk.rb', line 62 def get(key) data[key.to_s] end |
#keys ⇒ Object
Accesses all of the keys we’re keeping.
Returns an Array of Objects.
69 70 71 |
# File 'lib/keep/disk.rb', line 69 def keys data.keys end |
#present?(key) ⇒ Boolean
Searches for the key in the in-memory Hash.
Returns whether or not the key is present.
76 77 78 |
# File 'lib/keep/disk.rb', line 76 def present?(key) !get(key).nil? end |
#save ⇒ Object
Persists the in-memory hash to disk. Implemented by child classes.
Returns whether the save was successful.
43 |
# File 'lib/keep/disk.rb', line 43 def save ; end |
#set(key, value) ⇒ Object
Sets a value to a given key. Updates the in-memory hash, and then saves.
key - a String value for the key of this Hash
value - an Object that maps to the key, will be serialized
Returns the value..
51 52 53 54 55 |
# File 'lib/keep/disk.rb', line 51 def set(key,value) data[key.to_s] = value save value end |
#touch ⇒ Object
Ensures the destination file is created.
Returns nothing.
30 31 32 |
# File 'lib/keep/disk.rb', line 30 def touch FileUtils.touch(path) end |