class Kplay::Config

Represents the global or local configuration

Constants

GLOBAL_CONFIG_FILENAME
GLOBAL_DEFAULTS

Attributes

data[R]
path[R]

Public Class Methods

expand_template(text, vars) click to toggle source

Expand templates in given string value.

Example:

"${name}" -> "my-pod"

@param text [String] @param vars [Hash]

# File lib/kplay/config.rb, line 69
def self.expand_template(text, vars)
  vars.keys.reduce(text) do |t, var_name|
    t.gsub("${#{var_name}}", vars[var_name].to_s)
  end
end
expand_templates!(hash, vars) click to toggle source

In-place expansion of templates. Templates found in values are substituted with given vars

# File lib/kplay/config.rb, line 48
def self.expand_templates!(hash, vars)
  hash.keys.each do |key|
    case hash[key]
    when String
      hash[key] = expand_template(hash[key], vars)
    when Array
      hash[key] = hash[key].map { |t| expand_template(t, vars) }
    when Hash
      expand_templates!(hash[key], vars)
    end
  end
end
global() click to toggle source
# File lib/kplay/config.rb, line 75
def self.global
  new(GLOBAL_DEFAULTS.dup, Kplay.data_path(GLOBAL_CONFIG_FILENAME))
end
local() click to toggle source
# File lib/kplay/config.rb, line 79
def self.local
  new(global.data, Pathname.pwd.join('.kplay'))
end
new(data = {}, path = nil) click to toggle source
# File lib/kplay/config.rb, line 22
def initialize(data = {}, path = nil)
  @path = path ? Pathname.new(path).expand_path : nil
  @data = data.dup
  @extra_data = YAML.load(File.read(path)) if path && File.exist?(path)
  @data = @data.merge(@extra_data) if @extra_data
end

Public Instance Methods

[](key) click to toggle source
# File lib/kplay/config.rb, line 29
def [](key)
  value = data[key.to_s]
  value = self.class.new(value) if value.is_a?(Hash)
  value
end
expand_templates!(vars) click to toggle source

Expands templates in the values of the current config.

# File lib/kplay/config.rb, line 41
def expand_templates!(vars)
  self.class.expand_templates!(@data, vars)
end
to_h() click to toggle source
# File lib/kplay/config.rb, line 35
def to_h
  data.dup
end