Class: Toppings::Config

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

Constant Summary collapse

CONFIGS =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
16
# File 'lib/toppings/config.rb', line 10

def initialize(options = {})
  super()

  options.each do |option, value|
    send "#{option}=", value.kind_of?(Hash) ? Toppings::Config.new(value) : value
  end
end

Class Method Details

.app_config_pathObject

The app config path is based on the current directory, from where the toppings command is called.

TODO: app_config path should be configurable by a thor param



75
76
77
# File 'lib/toppings/config.rb', line 75

def app_config_path
  Pathname.new('.').join('config')
end

.custom_configObject



38
39
40
# File 'lib/toppings/config.rb', line 38

def custom_config
  @customs = parsed_config(custom_config_path)
end

.custom_config_nameObject



63
64
65
# File 'lib/toppings/config.rb', line 63

def custom_config_name
  'toppings.json'
end

.custom_config_pathObject



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

def custom_config_path
  app_config_path.join custom_config_name
end

.default_configObject



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

def default_config
  @defaults = parsed_config(default_config_path)
end

.default_config_nameObject



59
60
61
# File 'lib/toppings/config.rb', line 59

def default_config_name
  'default.json'
end

.default_config_pathObject



51
52
53
# File 'lib/toppings/config.rb', line 51

def default_config_path
  gem_config_path.join default_config_name
end

.gem_config_pathObject



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

def gem_config_path
  Pathname.new(Toppings.gem_root).join('config')
end

.inject_config(config) ⇒ Object



33
34
35
36
# File 'lib/toppings/config.rb', line 33

def inject_config(config)
  CONFIGS << config
  reload
end

.joined_configObject



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

def joined_config
  configs = CONFIGS.dup
  configs << custom_config
  configs.each_with_object({}) { |config, result_config| result_config.deep_merge!(config) }
end

.loadObject



19
20
21
# File 'lib/toppings/config.rb', line 19

def load
  @config ||= new(joined_config)
end

.parsed_config(path) ⇒ Object



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

def parsed_config(path)
  config_file = File.read(path) if File.exists?(path)
  JSON.parse(config_file || '{}')
end

.reloadObject



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

def reload
  @config = new(joined_config)
end