Class: GithubBackup::JSONStore
- Inherits:
-
Object
- Object
- GithubBackup::JSONStore
- Defined in:
- lib/github_backup/json_store.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #data ⇒ Object
- #delete! ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(path) ⇒ JSONStore
constructor
A new instance of JSONStore.
- #load! ⇒ Object
- #save ⇒ Object
- #to_s ⇒ Object
Constructor Details
permalink #initialize(path) ⇒ JSONStore
Returns a new instance of JSONStore.
6 7 8 9 |
# File 'lib/github_backup/json_store.rb', line 6 def initialize(path) @path = path FileUtils.mkdir_p(File.dirname(@path)) end |
Instance Method Details
permalink #[](key) ⇒ Object
[View source]
25 26 27 |
# File 'lib/github_backup/json_store.rb', line 25 def [](key) data[key] end |
permalink #[]=(key, value) ⇒ Object
[View source]
29 30 31 |
# File 'lib/github_backup/json_store.rb', line 29 def []=(key, value) data[key] = value end |
permalink #data ⇒ Object
[View source]
11 12 13 |
# File 'lib/github_backup/json_store.rb', line 11 def data @data || load! end |
permalink #delete! ⇒ Object
[View source]
42 43 44 |
# File 'lib/github_backup/json_store.rb', line 42 def delete! File.unlink(@path) if File.exist?(@path) end |
permalink #exists? ⇒ Boolean
21 22 23 |
# File 'lib/github_backup/json_store.rb', line 21 def exists? File.exist?(@path) end |
permalink #load! ⇒ Object
[View source]
15 16 17 18 19 |
# File 'lib/github_backup/json_store.rb', line 15 def load! @data = exists? ? JSON.parse(File.read(@path)) : {} @data['_read_at'] = Time.now @data end |
permalink #save ⇒ Object
[View source]
33 34 35 36 37 38 39 40 |
# File 'lib/github_backup/json_store.rb', line 33 def save return false unless @data @data['_saved_at'] = Time.now File.open(@path, 'w') do |file| file.write(to_s) end true end |
permalink #to_s ⇒ Object
[View source]
46 47 48 |
# File 'lib/github_backup/json_store.rb', line 46 def to_s JSON.pretty_generate(data) end |