Class: Ravioli::Configuration

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/ravioli/configuration.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Configuration

Returns a new instance of Configuration.



8
9
10
11
12
# File 'lib/ravioli/configuration.rb', line 8

def initialize(attributes = {})
  super({})
  @key_path = attributes.delete(:key_path)
  append(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

rubocop:disable Style/MissingRespondToMissing



103
104
105
106
107
108
109
110
111
# File 'lib/ravioli/configuration.rb', line 103

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

Instance Method Details

#append(attributes = {}) ⇒ Object

Convert a hash to accessors and nested Ravioli::Configuration instances.

Parameters:

  • key-value (Hash, #each)

    pairs to be converted to accessors



17
18
19
20
21
22
# 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) ⇒ Object



40
41
42
# File 'lib/ravioli/configuration.rb', line 40

def delete(key)
  table.delete(key.to_s)
end

#dig(*keys, safe: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



36
37
38
# 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) ⇒ Object



44
45
46
# File 'lib/ravioli/configuration.rb', line 44

def fetch(*keys)
  dig(*keys) || yield
end

#pretty_print(printer = nil) ⇒ Object



48
49
50
# File 'lib/ravioli/configuration.rb', line 48

def pretty_print(printer = nil)
  table.pretty_print(printer)
end

#safe(*keys) ⇒ Object



52
53
54
# File 'lib/ravioli/configuration.rb', line 52

def safe(*keys)
  fetch(*keys) { build(keys) }
end