Sha256: 7c15fe2902ee002b83abb560b65898584cd0fb469e6a8e48ab8c7d7ef4ed3cb9

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module Usable
  class Config
    # = Class
    # 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
    #

    def available_methods
      modules.each_with_object(Hash.new(Null.instance_method(:default_method))) do |mod, result|
        mod.instance_methods.each do |method_name|
          result[method_name] = mod.instance_method method_name
        end
      end
    end

    def add_module(mod)
      modules << mod
    end

    def modules
      @modules ||= []
    end

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

    def spec(key, value = nil)
      @spec ||= OpenStruct.new
      if value
        @spec[key.to_s.tr('=', '')] = value
      else
        @spec[key]
      end
    end

    def [](key)
      spec key
    end

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

    def method_missing(method_name, *args)
      spec method_name, *args
    rescue
      super
    end

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

    module Null
      def default_method(*, &_block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
usable-2.1.3 lib/usable/config.rb