Class: Toppings::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



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

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



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

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

.custom_configObject



25
26
27
# File 'lib/toppings/config.rb', line 25

def custom_config
  @customs = parsed_config(custom_config_path)
end

.custom_config_nameObject



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

def custom_config_name
  'toppings.json'
end

.custom_config_pathObject



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

def custom_config_path
  app_config_path.join custom_config_name
end

.default_configObject



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

def default_config
  @defaults = parsed_config(default_config_path)
end

.default_config_nameObject



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

def default_config_name
  'default.json'
end

.default_config_pathObject



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

def default_config_path
  gem_config_path.join default_config_name
end

.gem_config_pathObject



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

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

.joined_configObject



21
22
23
# File 'lib/toppings/config.rb', line 21

def joined_config
  default_config.deep_merge(custom_config)
end

.loadObject



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

def load
  @config = new(joined_config)
end

.parsed_config(path) ⇒ Object



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

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