Class: Waylon::Config
- Inherits:
-
Object
- Object
- Waylon::Config
- Extended by:
- Forwardable
- Includes:
- Singleton
- Defined in:
- lib/waylon/config.rb
Overview
The global configuration
Instance Attribute Summary collapse
-
#schema ⇒ Object
Returns the value of attribute schema.
Instance Method Summary collapse
-
#[](key) ⇒ String?
Retrieve the value for the given key.
-
#[]=(key, value) ⇒ Object
Set the value for a key.
-
#add_schema(key, default: nil, required: false, type: String) ⇒ Boolean
private
Stores schema metadata about config items.
-
#admins ⇒ Array<String>
A list of emails specified via the GLOBAL_ADMINS environment variable.
-
#key?(key) ⇒ Boolean
(also: #set?)
Check if a given key is explicitly set (not including defaults).
-
#load_env ⇒ Boolean
Load in the config from env variables.
-
#redis_host ⇒ String
Provides the redis host used for most of Waylon’s brain.
-
#redis_port ⇒ String
Provides the redis port used for most of Waylon’s brain.
-
#reset ⇒ Boolean
Clear the configuration.
-
#valid? ⇒ Boolean
Is the state of the config valid?.
-
#validate_config(schema, value) ⇒ Boolean
Checks if a value aligns with a schema.
-
#value?(key) ⇒ Boolean
Check if a given key is has any value (default or otherwise).
Instance Attribute Details
#schema ⇒ Object
Returns the value of attribute schema.
9 10 11 |
# File 'lib/waylon/config.rb', line 9 def schema @schema end |
Instance Method Details
#[](key) ⇒ String?
Retrieve the value for the given key
103 104 105 106 107 108 109 110 111 |
# File 'lib/waylon/config.rb', line 103 def [](key) @config ||= {} @schema ||= {} if @config.key?(key) @config[key].dup elsif @schema.key?(key) && @schema[key][:default] @schema[key][:default].dup end end |
#[]=(key, value) ⇒ Object
Set the value for a key
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/waylon/config.rb', line 88 def []=(key, value) @schema ||= {} if (@schema[key] && validate_config(@schema[key], value)) || key.start_with?("global.") @config ||= {} @config[key] = value elsif @schema[key] ::Waylon::Logger.log("Ignoring invalid config value for key: #{key}", :warn) else ::Waylon::Logger.log("Ignoring unknown config key: #{key}", :warn) end end |
#add_schema(key, default: nil, required: false, type: String) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Stores schema metadata about config items
20 21 22 23 24 |
# File 'lib/waylon/config.rb', line 20 def add_schema(key, default: nil, required: false, type: String) @schema ||= {} @schema[key] = { default:, required:, type: } true end |
#admins ⇒ Array<String>
A list of emails specified via the GLOBAL_ADMINS environment variable
28 29 30 31 |
# File 'lib/waylon/config.rb', line 28 def admins admin_emails = self["global.admins"] admin_emails ? admin_emails.split(",") : [] end |
#key?(key) ⇒ Boolean Also known as: set?
Check if a given key is explicitly set (not including defaults)
70 71 72 73 |
# File 'lib/waylon/config.rb', line 70 def key?(key) @config ||= {} @config.key?(key) end |
#load_env ⇒ Boolean
Load in the config from env variables
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/waylon/config.rb', line 35 def load_env @schema ||= {} self["global.log.level"] = ENV.fetch("LOG_LEVEL", "info") self["global.redis"] = ENV.fetch("REDIS", "localhost:6379") self["global.admins"] = ENV.fetch("GLOBAL_ADMINS", "") ENV.keys.grep(/CONF_/).each do |env_key| conf_key = env_key.downcase.split("_")[1..].join(".") ::Waylon::Logger.log("Attempting to set #{conf_key} from #{env_key}", :debug) self[conf_key] = ENV.fetch(env_key, nil) end true end |
#redis_host ⇒ String
Provides the redis host used for most of Waylon’s brain
50 51 52 |
# File 'lib/waylon/config.rb', line 50 def redis_host self["global.redis"].split(":").first end |
#redis_port ⇒ String
Provides the redis port used for most of Waylon’s brain
56 57 58 |
# File 'lib/waylon/config.rb', line 56 def redis_port self["global.redis"].split(":").last end |
#reset ⇒ Boolean
Clear the configuration
62 63 64 65 |
# File 'lib/waylon/config.rb', line 62 def reset @config = {} true end |
#valid? ⇒ Boolean
Is the state of the config valid?
115 116 117 118 119 120 121 122 |
# File 'lib/waylon/config.rb', line 115 def valid? @schema ||= {} missing = @schema.select { |_k, v| v[:required] }.reject { |k, _v| @config[k] } missing.each do |key, _value| ::Waylon::Logger.log("Missing config: #{key}", :debug) end missing.empty? end |
#validate_config(schema, value) ⇒ Boolean
Checks if a value aligns with a schema
128 129 130 131 132 133 134 135 |
# File 'lib/waylon/config.rb', line 128 def validate_config(schema, value) type = schema[:type] if type == :boolean value.is_a?(TrueClass) || value.is_a?(FalseClass) else value.is_a?(type) end end |
#value?(key) ⇒ Boolean
Check if a given key is has any value (default or otherwise)
80 81 82 83 |
# File 'lib/waylon/config.rb', line 80 def value?(key) @config ||= {} !self[key].nil? end |