class Librarian::Config::Source

Constants

CONFIG_KEY_VALIDITY_PATTERN
RAW_KEY_SUFFIX_VALIDITY_PATTERN

Attributes

adapter_name[RW]
data[RW]
forbidden_keys[RW]

Public Class Methods

config_key_validity_pattern() click to toggle source
# File lib/librarian/config/source.rb, line 16
def config_key_validity_pattern
  CONFIG_KEY_VALIDITY_PATTERN
end
new(adapter_name, options = { }) click to toggle source
# File lib/librarian/config/source.rb, line 24
def initialize(adapter_name, options = { })
  self.adapter_name = adapter_name

  self.forbidden_keys = options.delete(:forbidden_keys) || []
end
raw_key_suffix_validity_pattern() click to toggle source
# File lib/librarian/config/source.rb, line 13
def raw_key_suffix_validity_pattern
  RAW_KEY_SUFFIX_VALIDITY_PATTERN
end

Public Instance Methods

[](key) click to toggle source
# File lib/librarian/config/source.rb, line 30
def [](key)
  load!

  data[key]
end
[]=(key, value) click to toggle source
# File lib/librarian/config/source.rb, line 36
def []=(key, value)
  key_permitted?(key) or raise Error, "key not permitted: #{key.inspect}"
  value_permitted?(key, value) or raise Error, "value for key #{key.inspect} not permitted: #{value.inspect}"

  load!
  if value.nil?
    data.delete(key)
  else
    data[key] = value
  end
  save(data)
end
keys() click to toggle source
# File lib/librarian/config/source.rb, line 49
def keys
  load!

  data.keys
end

Private Instance Methods

assert_config_keys_valid!(config) click to toggle source
# File lib/librarian/config/source.rb, line 107
def assert_config_keys_valid!(config)
  bad_keys = config.keys.reject{|k| config_key_valid?(k)}
  unless bad_keys.empty?
    bad_keys_s = bad_keys.map(&:inspect).join(", ")
    raise Error, "config has bad keys: #{bad_keys_s}"
  end
end
assert_raw_keys_valid!(raw) click to toggle source
# File lib/librarian/config/source.rb, line 98
def assert_raw_keys_valid!(raw)
  bad_keys = raw.keys.reject{|k| raw_key_valid?(k)}
  unless bad_keys.empty?
    config_path_s = config_path.to_s.inspect
    bad_keys_s = bad_keys.map(&:inspect).join(", ")
    raise Error, "config #{to_s} has bad keys: #{bad_keys_s}"
  end
end
assert_values_valid!(data) click to toggle source
# File lib/librarian/config/source.rb, line 115
def assert_values_valid!(data)
  bad_data = data.reject{|k, v| String === v}
  bad_keys = bad_data.keys

  unless bad_keys.empty?
    bad_keys_s = bad_keys.map(&:inspect).join(", ")
    raise Error, "config has bad values for keys: #{bad_keys_s}"
  end
end
config_key_valid?(key) click to toggle source
# File lib/librarian/config/source.rb, line 86
def config_key_valid?(key)
  config_key_validity_pattern === key
end
config_key_validity_pattern() click to toggle source
# File lib/librarian/config/source.rb, line 90
def config_key_validity_pattern
  self.class.config_key_validity_pattern
end
key_permitted?(key) click to toggle source
# File lib/librarian/config/source.rb, line 63
def key_permitted?(key)
  String === key &&
  config_key_validity_pattern === key &&
  !forbidden_keys.any?{|k| k === key}
end
load!() click to toggle source
# File lib/librarian/config/source.rb, line 59
def load!
  self.data = load unless data
end
raw_key_prefix() click to toggle source
# File lib/librarian/config/source.rb, line 94
def raw_key_prefix
  @key_prefix ||= "LIBRARIAN_#{adapter_name.upcase}_"
end
raw_key_suffix_validity_pattern() click to toggle source
# File lib/librarian/config/source.rb, line 82
def raw_key_suffix_validity_pattern
  self.class.raw_key_suffix_validity_pattern
end
raw_key_valid?(key) click to toggle source
# File lib/librarian/config/source.rb, line 75
def raw_key_valid?(key)
  return false unless key.start_with?(raw_key_prefix)

  suffix = key[raw_key_prefix.size..-1]
  raw_key_suffix_validity_pattern =~ suffix
end
translate_config_to_raw(config) click to toggle source
# File lib/librarian/config/source.rb, line 136
def translate_config_to_raw(config)
  assert_config_keys_valid!(config)
  assert_values_valid!(config)

  Hash[config.map do |key, value|
    key = key.gsub(/\./, "__").gsub(/\-/, "_").upcase
    key = "#{raw_key_prefix}#{key}"
    [key, value]
  end]
end
translate_raw_to_config(raw) click to toggle source
# File lib/librarian/config/source.rb, line 125
def translate_raw_to_config(raw)
  assert_raw_keys_valid!(raw)
  assert_values_valid!(raw)

  Hash[raw.map do |key, value|
    key = key[raw_key_prefix.size .. -1]
    key = key.downcase.gsub(/__/, ".").gsub(/_/, "-")
    [key, value]
  end]
end
value_permitted?(key, value) click to toggle source
# File lib/librarian/config/source.rb, line 69
def value_permitted?(key, value)
  return true if value.nil?

  String === value
end