Class: Axn::Configurable::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/axn/configurable.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Config

Returns a new instance of Config.



480
481
482
483
# File 'lib/axn/configurable.rb', line 480

def initialize(settings)
  @settings = settings
  @values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/axn/configurable.rb', line 490

def method_missing(name, *args)
  str = name.to_s

  if str.end_with?("=")
    base = str.delete_suffix("=").to_sym
    return super unless @settings.key?(base)

    _write(base, args.first)
  elsif str.end_with?("?")
    base = str.delete_suffix("?").to_sym
    return super unless @settings.key?(base)

    !!_read(base)
  elsif @settings.key?(name)
    _read(name)
  else
    super
  end
end

Instance Method Details

#reset!(*names) ⇒ Object

Returns the named settings to their declared defaults (all of them with no arguments). reset_config! on the owning module discards the whole bag; this is the per-setting form, and the supported alternative to assigning nil, which is a VALUE rather than a reset.



513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/axn/configurable.rb', line 513

def reset!(*names)
  targets = names.empty? ? @settings.keys : names.map(&:to_sym)
  # Validate the WHOLE list before deleting anything, so a typo alongside a real name cannot
  # leave the config half-reset behind the raise.
  unknown = targets.reject { |name| @settings.key?(name) }
  if unknown.any?
    raise ArgumentError,
          "reset! got unknown setting #{unknown.first.inspect}. Declared settings are: " \
          "#{@settings.keys.map(&:inspect).join(', ')}."
  end

  targets.each { |name| @values.delete(name) }
  self
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


485
486
487
488
# File 'lib/axn/configurable.rb', line 485

def respond_to_missing?(name, include_private = false)
  base = name.to_s.delete_suffix("?").delete_suffix("=").to_sym
  @settings.key?(base) || super
end