Class: GithubBackup::JSONStore

Inherits:
Object
  • Object
show all
Defined in:
lib/github_backup/json_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ JSONStore

Returns a new instance of JSONStore.

[View source]

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

#[](key) ⇒ Object

[View source]

25
26
27
# File 'lib/github_backup/json_store.rb', line 25

def [](key)
  data[key]
end

#[]=(key, value) ⇒ Object

[View source]

29
30
31
# File 'lib/github_backup/json_store.rb', line 29

def []=(key, value)
  data[key] = value
end

#dataObject

[View source]

11
12
13
# File 'lib/github_backup/json_store.rb', line 11

def data
  @data || load!
end

#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

#exists?Boolean

Returns:

  • (Boolean)
[View source]

21
22
23
# File 'lib/github_backup/json_store.rb', line 21

def exists?
  File.exist?(@path)
end

#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

#saveObject

[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

#to_sObject

[View source]

46
47
48
# File 'lib/github_backup/json_store.rb', line 46

def to_s
  JSON.pretty_generate(data)
end