Class: Boothby::ConfigurationData

Inherits:
Object
  • Object
show all
Defined in:
lib/boothby/configuration_data.rb

Overview

Specialize hash to allow ease of access for configuration values

Constant Summary collapse

DEFAULT =

Default is to raise KeyError if key does not exist

'EMPTY'
ENVIRONMENT =
Rails.env.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, **options) ⇒ ConfigurationData

Returns a new instance of ConfigurationData.



19
20
21
22
23
24
25
26
# File 'lib/boothby/configuration_data.rb', line 19

def initialize(hash, **options)
  @options = options.reverse_merge(
    environment: ENVIRONMENT,
    default: DEFAULT
  )

  @hash = add_dot_access(hash.with_indifferent_access)
end

Class Method Details

.from_yaml(filename, **options) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/boothby/configuration_data.rb', line 9

def self.from_yaml(filename, **options)
  file_path = Rails.root.join(filename)
  return unless File.file?(file_path)

  content = File.read(file_path)
  content = ERB.new(content).result
  yml = YAML.safe_load(content, [Time, Date], aliases: true)
  new(yml, **options)
end

Instance Method Details

#[](key) ⇒ Object



55
56
57
# File 'lib/boothby/configuration_data.rb', line 55

def [](key)
  dig(key)
end

#[]=(key, value) ⇒ Object



59
60
61
62
63
64
# File 'lib/boothby/configuration_data.rb', line 59

def []=(key, value)
  config_hash = @hash.fetch(@options[:environment], @hash)
  config_hash[key] = value
  define_singleton_method(key) { value }
  config_hash.define_singleton_method(key) { value }
end

#add_dot_access(hash) ⇒ Object



83
84
85
86
87
88
# File 'lib/boothby/configuration_data.rb', line 83

def add_dot_access(hash)
  allow_dot_access(hash, base_level: true)
  config_hash = hash.fetch(@options[:environment], hash)
  allow_dot_access(config_hash, base_level: true) if config_hash != hash
  hash
end

#allow_dot_access(hash, base_level: true) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/boothby/configuration_data.rb', line 90

def allow_dot_access(hash, base_level: true)
  hash.each do |key, value|
    hash.define_singleton_method(key) { value }
    define_singleton_method(key) { value } if base_level
    allow_dot_access(value, base_level: false) if value.is_a?(Hash)
  end
end

#config(first_key = nil, **options) ⇒ Object



28
29
30
31
32
33
# File 'lib/boothby/configuration_data.rb', line 28

def config(first_key = nil, **options)
  return @hash if first_key.blank?

  opts = @options.merge(**options)
  @hash.key?(first_key) ? @hash : @hash.fetch(opts[:environment], @hash)
end

#dig(*keys, **options) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/boothby/configuration_data.rb', line 35

def dig(*keys, **options)
  hash = config(keys.first, **options)

  while hash.respond_to?(:fetch) && (key = keys.shift)
    hash = hash.fetch(key, nil)
  end

  keys.empty? ? hash : nil
end

#dig!(*keys, **options) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/boothby/configuration_data.rb', line 45

def dig!(*keys, **options)
  hash = config(keys.first, **options)

  while hash.respond_to?(:fetch) && (key = keys.shift)
    hash = hash.fetch(key, nil)
  end

  keys.empty? ? hash : raise(KeyError)
end

#fetch(key, **options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/boothby/configuration_data.rb', line 66

def fetch(key, **options)
  opts = @options.merge(**options)
  hash = config(key, **opts)

  if opts[:default] == DEFAULT
    hash.fetch(key)
  else
    hash.fetch(key, opts[:default])
  end
end

#key?(key, **options) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/boothby/configuration_data.rb', line 77

def key?(key, **options)
  opts = @options.merge(**options)
  hash = config(key, **opts)
  hash.key?(key)
end