Class: Boothby::ConfigurationData
- Inherits:
-
Object
- Object
- Boothby::ConfigurationData
- 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
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #add_dot_access(hash) ⇒ Object
- #allow_dot_access(hash, base_level: true) ⇒ Object
- #config(first_key = nil, **options) ⇒ Object
- #dig(*keys, **options) ⇒ Object
- #dig!(*keys, **options) ⇒ Object
- #fetch(key, **options) ⇒ Object
-
#initialize(hash, **options) ⇒ ConfigurationData
constructor
A new instance of ConfigurationData.
- #key?(key, **options) ⇒ Boolean
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 = .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, **) 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, **) 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, **) return @hash if first_key.blank? opts = @options.merge(**) @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, **) hash = config(keys.first, **) 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, **) hash = config(keys.first, **) 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, **) opts = @options.merge(**) hash = config(key, **opts) if opts[:default] == DEFAULT hash.fetch(key) else hash.fetch(key, opts[:default]) end end |
#key?(key, **options) ⇒ Boolean
77 78 79 80 81 |
# File 'lib/boothby/configuration_data.rb', line 77 def key?(key, **) opts = @options.merge(**) hash = config(key, **opts) hash.key?(key) end |