Class: Jarbs::Config

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

Constant Summary collapse

FILE_NAME =
'.jarbs'
GLOBAL_CONFIG =
File.join(Dir.home, FILE_NAME)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = FILE_NAME) ⇒ Config

Returns a new instance of Config.



17
18
19
20
# File 'lib/jarbs/config.rb', line 17

def initialize(file=FILE_NAME)
  @file = file
  @config = read
end

Class Method Details

.globalObject



13
14
15
# File 'lib/jarbs/config.rb', line 13

def self.global
  new GLOBAL_CONFIG
end

.touch(for_global: false) ⇒ Object



8
9
10
11
# File 'lib/jarbs/config.rb', line 8

def self.touch(for_global: false)
  path = for_global ? GLOBAL_CONFIG : FILE_NAME
  File.open(path, 'w') {|f| f.write JSON.pretty_generate({}) }
end

Instance Method Details

#exists?(key, in_global: false) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/jarbs/config.rb', line 46

def exists?(key, in_global: false)
  return global.exists?(key) if in_global

  !get(key).nil?
end

#get(key, from_global: false, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jarbs/config.rb', line 33

def get(key, from_global: false, &block)
  return global.get(key, &block) if from_global

  val = @config[key]

  if !val && block_given?
    val = yield
    set(key, val)
  end

  val
end

#globalObject



22
23
24
# File 'lib/jarbs/config.rb', line 22

def global
  @global ||= self.class.global
end


52
53
54
55
56
# File 'lib/jarbs/config.rb', line 52

def print(for_global: false)
  return global.print if for_global

  pp @config
end

#set(key, value, from_global: false) ⇒ Object



26
27
28
29
30
31
# File 'lib/jarbs/config.rb', line 26

def set(key, value, from_global: false)
  return global.set(key, value) if from_global

  @config[key] = value
  finalize
end