Module: SecretConfig::Utils
- Defined in:
- lib/secret_config/utils.rb
Class Method Summary collapse
-
.camelize(term) ⇒ Object
Borrow from Rails, when not running Rails.
- .constantize_symbol(symbol, namespace = 'SecretConfig::Providers') ⇒ Object
-
.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.
Class Method Details
.camelize(term) ⇒ Object
Borrow from Rails, when not running Rails
22 23 24 25 26 27 28 |
# File 'lib/secret_config/utils.rb', line 22 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
12 13 14 15 16 17 18 19 |
# File 'lib/secret_config/utils.rb', line 12 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_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 |