Class: Waylon::Config

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/waylon/config.rb

Overview

The global configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#schemaObject

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

Parameters:

  • key (String)

    The key to lookup

Returns:

  • (String, nil)

    The requested value



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

Parameters:

  • key (String)

    The key to use for storing the value

  • value (Object)

    The value for the 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

Parameters:

  • key (String)

    The config key to define a schema for

  • required (Boolean) (defaults to: false)

    Is this key required on startup?

  • type (Class) (defaults to: String)

    The class type for the stored value

  • default (Object) (defaults to: nil)

    The optional default value

Returns:

  • (Boolean)

    Was the schema update successful?



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

#adminsArray<String>

A list of emails specified via the GLOBAL_ADMINS environment variable

Returns:

  • (Array<String>)

    a list of emails



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)

Parameters:

  • key (String)

    The key to check

Returns:

  • (Boolean)

    Is the key set?



70
71
72
73
# File 'lib/waylon/config.rb', line 70

def key?(key)
  @config ||= {}
  @config.key?(key)
end

#load_envBoolean

Load in the config from env variables

Returns:

  • (Boolean)

    Was the configuration loaded?



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_hostString

Provides the redis host used for most of Waylon’s brain

Returns:

  • (String)

    The redis host



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

def redis_host
  self["global.redis"].split(":").first
end

#redis_portString

Provides the redis port used for most of Waylon’s brain

Returns:

  • (String)

    The redis host



56
57
58
# File 'lib/waylon/config.rb', line 56

def redis_port
  self["global.redis"].split(":").last
end

#resetBoolean

Clear the configuration

Returns:

  • (Boolean)

    Was the configuration reset?



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?

Returns:

  • (Boolean)


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

Parameters:

  • schema (Hash)

    The schema definition for this value

  • value

    The value to compare to the schema

Returns:

  • (Boolean)


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)

Parameters:

  • key (String)

    The key to look for

Returns:

  • (Boolean)

    Does the key have a value?



80
81
82
83
# File 'lib/waylon/config.rb', line 80

def value?(key)
  @config ||= {}
  !self[key].nil?
end