class Ravioli::Configuration
Attributes
key_path[R]
Public Class Methods
new(attributes = {})
click to toggle source
Calls superclass method
# File lib/ravioli/configuration.rb, line 8 def initialize(attributes = {}) super({}) @key_path = attributes.delete(:key_path) append(attributes) end
Public Instance Methods
append(attributes = {})
click to toggle source
Convert a hash to accessors and nested {Ravioli::Configuration} instances.
@param [Hash, each] key-value pairs to be converted to accessors
# File lib/ravioli/configuration.rb, line 17 def append(attributes = {}) return unless attributes.respond_to?(:each) attributes.each do |key, value| self[key.to_sym] = cast(key.to_sym, value) end end
delete(key)
click to toggle source
# File lib/ravioli/configuration.rb, line 40 def delete(key) table.delete(key.to_s) end
dig(*keys, safe: false)
click to toggle source
# File lib/ravioli/configuration.rb, line 24 def dig(*keys, safe: false) return safe(*keys) if safe fetch_env_key_for(keys) do keys.inject(self) do |value, key| value = value.try(:[], key) break if value.blank? value end end end
dig!(*keys)
click to toggle source
# File lib/ravioli/configuration.rb, line 36 def dig!(*keys) fetch(*keys) { raise KeyMissingError.new("Could not find value at key path #{keys.inspect}") } end
fetch(*keys) { || ... }
click to toggle source
# File lib/ravioli/configuration.rb, line 44 def fetch(*keys) dig(*keys) || yield end
pretty_print(printer = nil)
click to toggle source
# File lib/ravioli/configuration.rb, line 48 def pretty_print(printer = nil) table.pretty_print(printer) end
safe(*keys)
click to toggle source
# File lib/ravioli/configuration.rb, line 52 def safe(*keys) fetch(*keys) { build(keys) } end
Private Instance Methods
build(keys, attributes = {})
click to toggle source
# File lib/ravioli/configuration.rb, line 60 def build(keys, attributes = {}) attributes[:key_path] = key_path_for(keys) child = self.class.new(attributes) child.freeze if frozen? child end
cast(key, value)
click to toggle source
# File lib/ravioli/configuration.rb, line 67 def cast(key, value) if value.is_a?(Hash) original_value = dig(*Array(key)) value = original_value.table.deep_merge(value.deep_symbolize_keys) if original_value.is_a?(self.class) build(key, value) else fetch_env_key_for(key) { if value.is_a?(Array) value.each_with_index.map { |subvalue, index| cast(Array(key) + [index], subvalue) } else value end } end end
fetch_env_key_for(keys, &block)
click to toggle source
# File lib/ravioli/configuration.rb, line 83 def fetch_env_key_for(keys, &block) env_key = key_path_for(keys).join("_").upcase ENV.fetch(env_key, &block) end
key_path_for(keys)
click to toggle source
# File lib/ravioli/configuration.rb, line 88 def key_path_for(keys) Array(key_path) + Array(keys) end
method_missing(method, *args, &block)
click to toggle source
rubocop:disable Style/MethodMissingSuper rubocop:disable Style/MissingRespondToMissing
Calls superclass method
# File lib/ravioli/configuration.rb, line 94 def method_missing(method, *args, &block) return super unless args.empty? # Return proper booleans from query methods return send(method.to_s.chomp("?")).present? if method.to_s.ends_with?("?") # Try to find a matching ENV key fetch_env_key_for(method) { super(method, *args, &block) } end