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
- .decompose(key, value, h = {}) ⇒ Object
-
.flatten(hash, path = nil) ⇒ Object
Takes a hierarchical structure and flattens it to a single level hash.
-
.flatten_each(hash, path = nil, &block) ⇒ Object
Takes a hierarchical structure and flattens it to a single level.
-
.hierarchical(flat_hash) ⇒ Object
Takes a flat hash and expands the keys on each ‘/` into a deep hierarchy.
Class Method Details
.camelize(term) ⇒ Object
Borrow from Rails, when not running Rails
62 63 64 65 66 67 68 |
# File 'lib/secret_config/utils.rb', line 62 def self.camelize(term) string = term.to_s string = string.sub(/^[a-z\d]*/, &:capitalize) string.gsub!(%r{(?:_|(/))([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
52 53 54 55 56 57 58 59 |
# File 'lib/secret_config/utils.rb', line 52 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 |
.decompose(key, value, h = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/secret_config/utils.rb', line 31 def self.decompose(key, value, h = {}) full_path, name = File.split(key) if full_path == "." h[key] = value return h end last = full_path.split("/").reduce(h) do |target, path| if path == "" target elsif target.key?(path) val = target[path] val = target[path] = {NODE_KEY => val} unless val.is_a?(Hash) val else target[path] = {} end end last[name] = value h 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.
18 19 20 21 22 |
# File 'lib/secret_config/utils.rb', line 18 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 11 12 13 14 |
# File 'lib/secret_config/utils.rb', line 5 def self.flatten_each(hash, path = nil, &block) hash.each_pair do |key, value| if key == NODE_KEY yield(path, value) else name = path.nil? ? key : File.join(path, key) value.is_a?(Hash) ? flatten_each(value, name, &block) : yield(name, value) end end end |
.hierarchical(flat_hash) ⇒ Object
Takes a flat hash and expands the keys on each ‘/` into a deep hierarchy.
25 26 27 28 29 |
# File 'lib/secret_config/utils.rb', line 25 def self.hierarchical(flat_hash) h = {} flat_hash.each_pair { |path, value| decompose(path, value, h) } h end |