Class: Inq::Config
- Inherits:
-
Hash
- Object
- Hash
- Inq::Config
- Defined in:
- lib/inq/config.rb
Overview
Instance Attribute Summary collapse
-
#site_configs ⇒ Object
readonly
Returns the value of attribute site_configs.
Instance Method Summary collapse
-
#initialize ⇒ Config
constructor
A new instance of Config.
-
#load(*configs) ⇒ Config
Take a collection of config hashes and cascade them, meaning values in later ones override values in earlier ones.
-
#load_defaults ⇒ Hash
If the INQ_USE_ENV+ environment variable is set, load config from the environment.
-
#load_env ⇒ Config
Load config info from environment variables.
-
#load_files(*file_paths) ⇒ Object
TODO: See if this can be consolidated with load_site_configs.
-
#load_site_configs(*files) ⇒ Config
Load the config files as specified via
files
.
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
38 39 40 41 |
# File 'lib/inq/config.rb', line 38 def initialize super() @site_configs = [] end |
Instance Attribute Details
#site_configs ⇒ Object (readonly)
Returns the value of attribute site_configs.
22 23 24 |
# File 'lib/inq/config.rb', line 22 def site_configs @site_configs end |
Instance Method Details
#load(*configs) ⇒ Config
Take a collection of config hashes and cascade them, meaning values in later ones override values in earlier ones.
E.g., this results in {‘a’=>‘x’, ‘c’=>‘d’}:
load({'a'=>'b'}, {'c'=>'d'}, {'a'=>'x'})
And this results in {‘a’=>[‘b’, ‘c’]}:
load({'a'=>['b']}, {'a'=>['c']})
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/inq/config.rb', line 83 def load(*configs) configs.each do |config| config.each do |k, v| if self[k] && self[k].is_a?(Array) self[k] += v else self[k] = v end end end self end |
#load_defaults ⇒ Hash
If the INQ_USE_ENV+ environment variable is set, load config from the environment.
Otherwise, load the the default config file.
30 31 32 33 34 35 36 |
# File 'lib/inq/config.rb', line 30 def load_defaults if ENV["INQ_USE_ENV"] == "true" load_env else load_site_configs(HOME_CONFIG) end end |
#load_env ⇒ Config
Load config info from environment variables.
Supported environment variables:
-
INQ_GITHUB_TOKEN: a GitHub authentication token.
-
INQ_GITHUB_USERNAME: the GitHub username corresponding to the token.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/inq/config.rb', line 104 def load_env Inq::Text.puts "Using configuration from environment variables." gh_token = ENV["INQ_GITHUB_TOKEN"] || ENV["HOWIS_GITHUB_TOKEN"] gh_username = ENV["INQ_GITHUB_USERNAME"] || ENV["HOWIS_GITHUB_USERNAME"] raise "INQ_GITHUB_TOKEN environment variable is not set" \ unless gh_token raise "INQ_GITHUB_USERNAME environment variable is not set" \ unless gh_username load({ "sources/github" => { "username" => gh_username, "token" => gh_token, }, }) end |
#load_files(*file_paths) ⇒ Object
TODO: See if this can be consolidated with load_site_configs.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/inq/config.rb', line 59 def load_files(*file_paths) files = (site_configs + file_paths).map { |f| Pathname.new(f) } # Keep only files that exist. files.select!(&:file?) # Load the YAML files into Hashes. configs = files.map { |file| YAML.safe_load(file.read) } # Apply configs. load(*configs) end |
#load_site_configs(*files) ⇒ Config
Load the config files as specified via files
.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/inq/config.rb', line 47 def load_site_configs(*files) # Allows both: # load_site_configs('foo', 'bar') # load_site_configs(['foo', bar']) # but not: # load_site_configs(['foo'], 'bar') files = files[0] if files.length == 1 && files[0].is_a?(Array) load_files(*files) end |