Skip to content

Commit

Permalink
default XDG_CONFIG_HOME by the book
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Mar 6, 2013
1 parent 7f3ef96 commit 0d3c63a
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/raury/config.rb
Expand Up @@ -11,13 +11,6 @@ def self.method_missing(meth, *args, &block)
self.instance.send(meth, *args, &block)
end

# user config file location
CONFIG_FILE = if ENV['XDG_CONFIG_HOME']
File.join(ENV['XDG_CONFIG_HOME'], 'rauryrc')
else
File.join(ENV['HOME'], '.rauryrc')
end

# default behavior
DEFAULTS = { 'color' => :auto,
'confirm' => true,
Expand Down Expand Up @@ -106,15 +99,28 @@ def development_pkg?(pkg)
false
end

private

# lazy-load the defaults hash merged with your yaml configuration
# when present.
def config
unless @config
yaml = YAML::load(File.open(CONFIG_FILE)) rescue {}
@config = DEFAULTS.merge(yaml)
end
@config ||= load_config
end

def load_config
yaml = YAML::load(File.open(config_file)) rescue {}

@config
DEFAULTS.merge(yaml)
end

def config_file
xdg = ENV.fetch('XDG_CONFIG_HOME') { File.join(ENV['HOME'], '.config') }

if File.exists?(xdg)
File.join(xdg, 'rauryrc')
else
File.join(ENV['HOME'], '.rauryrc')
end
end
end
end

0 comments on commit 0d3c63a

Please sign in to comment.