Sha256: ad7a0bb3844c2b864fda2bdf43f5484e82b00cfe5da873c8e211d7a576de19a4

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'usable/config_multi'
require 'usable/config_register'

module Usable
  # Store and manage configuration settings. Keep methods to a minimum since this class relies on method_missing to read
  # and write to the underlying @spec object
  class Config
    include ConfigRegister
    include ConfigMulti

    def initialize
      @spec = OpenStruct.new
    end

    def each(&block)
      @spec.to_h.each(&block)
    end

    def spec(key, value = nil)
      if value
        @spec[key.to_s.tr('=', '')] = value
      else
        # Handle the case where the value may be defined with a block, in which case it's a method
        @spec[key] ||= call_lazy_method(key)
      end
    end

    def _spec
      @spec
    end

    def [](key)
      spec key
    end

    def []=(key, val)
      spec key, val
    end

    def call_lazy_method(key)
      @spec.public_send(key.to_s.tr('=', ''))
    end

    def method_missing(method_name, *args, &block)
      if block
        _spec.define_singleton_method(method_name) { yield }
      else
        spec method_name, *args
      end
    rescue => e
      super
    end

    def respond_to_missing?(method_name, _private = false)
      method_name.to_s.end_with?('=') || _spec.respond_to?(method_name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
usable-2.2.1 lib/usable/config.rb
usable-2.2.0 lib/usable/config.rb