Class: UniversalID::Settings

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, Singleton
Defined in:
lib/universalid/settings.rb

Constant Summary collapse

DEFAULT_FILE_PATH =
File.expand_path("../../config/default.yml", __dir__)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](key) ⇒ Object



24
25
26
# File 'lib/universalid/settings.rb', line 24

def [](key)
  instance[key]
end

.build(**options) ⇒ Object



14
15
16
17
18
# File 'lib/universalid/settings.rb', line 14

def build(**options)
  instance.default_copy.tap do |settings|
    options.each { |key, val| assign key, val, to: settings }
  end
end

.registerObject



20
21
22
# File 'lib/universalid/settings.rb', line 20

def register(...)
  instance.register(...)
end

Instance Method Details

#[](key) ⇒ Object



70
71
72
# File 'lib/universalid/settings.rb', line 70

def [](key)
  registry[key.to_sym]
end

#register(key, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/universalid/settings.rb', line 50

def register(key, options = {})
  key = key.to_s.strip.downcase.to_sym
  synchronize do
    raise ArgumentError, "Already registered! key: #{key}" if registry.key? key
    config = case options
    when String then Config.load_files(options)
    when Hash then Config::Options.new(options)
    when Config::Options then options
    else raise ArgumentError, "Invalid options! Must be a String, Hash, or Config::Options."
    end

    config = self.class.build(**config) unless key == :default
    registry[key] = config
    self.class.define_method(key) { config }
    self.class.define_method(:"#{key}_copy") { Marshal.load Marshal.dump(config) }
    self.class.define_singleton_method(key) { instance.public_send key }
    [key, config]
  end
end