Module: SecretConfig::Utils

Defined in:
lib/secret_config/utils.rb

Class Method Summary collapse

Class Method Details

.camelize(term) ⇒ Object

Borrow from Rails, when not running Rails



30
31
32
33
34
35
36
# File 'lib/secret_config/utils.rb', line 30

def self.camelize(term)
  string = term.to_s
  string = string.sub(/^[a-z\d]*/, &:capitalize)
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
  string.gsub!('/'.freeze, '::'.freeze)
  string
end

.constantize_symbol(symbol, namespace = 'SecretConfig::Providers') ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/secret_config/utils.rb', line 20

def self.constantize_symbol(symbol, namespace = 'SecretConfig::Providers')
  klass = "#{namespace}::#{camelize(symbol.to_s)}"
  begin
    Object.const_get(klass)
  rescue NameError
    raise(ArgumentError, "Could not convert symbol: #{symbol.inspect} to a class in: #{namespace}. Looking for: #{klass}")
  end
end

.flatten(hash, path = nil) ⇒ Object

Takes a hierarchical structure and flattens it to a single level hash. If path is supplied it is prepended to every key returned.



14
15
16
17
18
# File 'lib/secret_config/utils.rb', line 14

def self.flatten(hash, path = nil)
  h = {}
  flatten_each(hash, path) { |key, value| h[key] = value }
  h
end

.flatten_each(hash, path = nil, &block) ⇒ Object

Takes a hierarchical structure and flattens it to a single level. If path is supplied it is prepended to every key returned.



5
6
7
8
9
10
# File 'lib/secret_config/utils.rb', line 5

def self.flatten_each(hash, path = nil, &block)
  hash.each_pair do |key, value|
    name = path.nil? ? key : "#{path}/#{key}"
    value.is_a?(Hash) ? flatten_each(value, name, &block) : yield(name, value)
  end
end