Sha256: a8dcd325f3e8bf0677c228ec17bd7f475a5e713888d55e6f9dd346f117bc9361

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module GemConfig
  class Configuration
    def rules
      @rules ||= Rules.new
    end

    def current
      self.rules.keys.each_with_object({}) do |key, hash|
        hash[key] = get(key)
      end
    end

    def reset
      self.rules.keys.each do |key|
        unset key
      end
    end

    def method_missing(method, *args, &block)
      case
      when self.rules.keys.include?(method.to_sym)
        get method
      when (match = method.to_s.match(/\A(?<key>\w+)=\z/)) && self.rules.keys.include?(match[:key].to_sym)
        set match[:key], args.first
      else
        super method, *args, block
      end
    end

    private

    def unset(key)
      remove_instance_variable "@#{key}" if instance_variable_defined?("@#{key}")
    end

    def set(key, value)
      self.rules.check(key, value)
      instance_variable_set "@#{key}", value
    end

    def get(key)
      if instance_variable_defined?("@#{key}")
        instance_variable_get "@#{key}"
      else
        self.rules[key.to_sym][:default]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gem_config-0.2.4 lib/gem_config/configuration.rb
gem_config-0.2.3 lib/gem_config/configuration.rb
gem_config-0.2.2 lib/gem_config/configuration.rb
gem_config-0.2.1 lib/gem_config/configuration.rb