class Staticd::Config
Manage Staticd
Configuration.
Can load configuration from a hash, from environment variables with the STATICD_ prefix or from a config file in yaml format.
Once loaded,configuration is available from anywhere in the app using Staticd::Config.
Public Class Methods
<<(settings)
click to toggle source
Push settings into Staticd
global configuration.
String setting keys are converted to symbols.
Example:
Staticd::Config << {"foo" => bar} Staticd::Config[:foo] # => "bar"
# File lib/staticd/config.rb, line 59 def self.<<(settings) instance << settings end
[](setting)
click to toggle source
Get a setting value from the Staticd
global configuration.
# File lib/staticd/config.rb, line 69 def self.[](setting) instance[setting] end
[]=(setting, value)
click to toggle source
Same as << method.
# File lib/staticd/config.rb, line 64 def self.[]=(setting, value) instance << {setting => value} end
key?(setting_name)
click to toggle source
# File lib/staticd/config.rb, line 73 def self.key?(setting_name) instance.key?(setting_name) end
load_env()
click to toggle source
Load configuration from environment variables.
Example:
ENV["STATICD_FOO"] = "bar" Staticd::Config.load_env Staticd::Config[:foo] # => "bar"
# File lib/staticd/config.rb, line 22 def self.load_env settings = {} env = ENV.select { |name, value| name =~ /^STATICD_/ } env.each do |name, value| setting = name[/^STATICD_(.*)/, 1].downcase.to_sym settings[setting] = value end instance << settings end
load_file(config_file)
click to toggle source
Load configuration from a YAML file.
The configuration file can contain ERB code.
Example (config file)
--- foo: bar
Example:
Staticd::Config.load_file("/etc/staticd/staticd.yml") Staticd::Config[:foo] # => "bar"
# File lib/staticd/config.rb, line 44 def self.load_file(config_file) content = File.read(config_file) erb = ERB.new(content) settings = YAML.load(erb.result) instance << settings end
new()
click to toggle source
# File lib/staticd/config.rb, line 81 def initialize @settings = {} end
to_s()
click to toggle source
# File lib/staticd/config.rb, line 77 def self.to_s instance.to_s end
Public Instance Methods
<<(settings)
click to toggle source
# File lib/staticd/config.rb, line 85 def <<(settings) settings = hash_symbolize_keys(settings) mutex.synchronize { @settings.merge!(settings) } end
[](setting)
click to toggle source
# File lib/staticd/config.rb, line 90 def [](setting) mutex.synchronize { @settings.key?(setting) ? @settings[setting] : nil } end
key?(setting_name)
click to toggle source
# File lib/staticd/config.rb, line 94 def key?(setting_name) mutex.synchronize { @settings.key?(setting_name) } end
to_s()
click to toggle source
# File lib/staticd/config.rb, line 98 def to_s mutex.synchronize { @settings.to_s } end
Private Instance Methods
hash_symbolize_keys(hash)
click to toggle source
# File lib/staticd/config.rb, line 104 def hash_symbolize_keys(hash) hash.keys.each do |key| hash[(key.to_sym rescue key) || key] = hash.delete(key) end hash end
mutex()
click to toggle source
# File lib/staticd/config.rb, line 111 def mutex @mutex ||= Mutex.new end