Class: GithubBackup::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_path) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
# File 'lib/github_backup/config.rb', line 9

def initialize(default_path)
  @override = {}
  @default_path = default_path
  puts "Config: #{path}"
end

Instance Attribute Details

#overrideObject

Returns the value of attribute override.



7
8
9
# File 'lib/github_backup/config.rb', line 7

def override
  @override
end

Instance Method Details

#[](key) ⇒ Object

Read a value from the config ex: config # => value



61
62
63
# File 'lib/github_backup/config.rb', line 61

def [](key)
  @override[key] || values[key] || defaults[key]
end

#[]=(key, value) ⇒ Object

Set a value in the config ex: config = value



67
68
69
70
# File 'lib/github_backup/config.rb', line 67

def []=(key, value)
  values[key] = value
  values.save
end

#dataObject



27
28
29
# File 'lib/github_backup/config.rb', line 27

def data
  @data ||= JSONStore.new(data_path)
end

#data_dirObject



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

def data_dir
  ENV['XDG_DATA_HOME'] || File.join(home, '.local', 'share')
end

#data_pathObject



50
51
52
# File 'lib/github_backup/config.rb', line 50

def data_path
  File.join(data_dir, "#{APP}.json")
end

#defaultsObject



15
16
17
18
19
20
21
# File 'lib/github_backup/config.rb', line 15

def defaults
  if @default_path
    @defaults ||= JSONStore.new(@default_path)
  else
    {}
  end
end

#delete!Object

Removes the saved config



79
80
81
# File 'lib/github_backup/config.rb', line 79

def delete!
  values.delete!
end

#dirObject

The user’s preferred config dir



42
43
44
# File 'lib/github_backup/config.rb', line 42

def dir
  ENV['XDG_CONFIG_HOME'] || File.join(home, '.config')
end

#exists?Boolean

Check if the config exists

Returns:

  • (Boolean)


32
33
34
# File 'lib/github_backup/config.rb', line 32

def exists?
  values.exists?
end

#homeObject

The user’s home directory



37
38
39
# File 'lib/github_backup/config.rb', line 37

def home
  ENV['HOME'] || File.expand_path('~')
end

#pathObject

the path to the config file



55
56
57
# File 'lib/github_backup/config.rb', line 55

def path
  File.join(dir, "#{APP}.json")
end

#saveObject

Write config to disk at the user’s config dir. Doesn’t save overrides



74
75
76
# File 'lib/github_backup/config.rb', line 74

def save
  values.save
end

#to_sObject

writes the config json to stdout



84
85
86
# File 'lib/github_backup/config.rb', line 84

def to_s
  values.to_s
end

#valuesObject



23
24
25
# File 'lib/github_backup/config.rb', line 23

def values
  @values ||= JSONStore.new(path)
end