Class: Keep::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/keep/disk.rb

Direct Known Subclasses

Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject

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

#dataObject

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

#keysObject

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.

Returns:

  • (Boolean)


76
77
78
# File 'lib/keep/disk.rb', line 76

def present?(key)
  !get(key).nil?
end

#saveObject

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

#touchObject

Ensures the destination file is created.

Returns nothing.



30
31
32
# File 'lib/keep/disk.rb', line 30

def touch
  FileUtils.touch(path)
end