Sha256: 5d9739c30d5b1d139c69e1881dec0efa841531d9c0038020281b052dbe2b9ff9

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 KB

Contents

class DiscourseTheme::Config

  class PathSetting
    def initialize(config, path)
      @config = config
      @path = path
    end

    def api_key
      safe_config["api_key"]
    end

    def api_key=(val)
      set("api_key", val)
    end

    def url
      safe_config["url"]
    end

    def url=(val)
      set("url", val)
    end

    protected

    def set(name, val)
      hash = @config.raw_config[@path] ||= {}
      hash[name] = val
      @config.save
      val
    end

    def safe_config
      config = @config.raw_config[@path]
      if Hash === config
        config
      else
        {}
      end
    end
  end

  attr_reader :raw_config, :filename

  def initialize(filename)
    @filename = filename

    if File.exist?(@filename)
      begin
        @raw_config = YAML.load_file(@filename)
        raise unless Hash === @raw_config
      rescue
        @raw_config = {}
        $stderr.puts "ERROR: #{@filename} contains invalid config, resetting"
      end
    else
      @raw_config = {}
    end
  end

  def set(path, url:, api_key:)
    @raw_config[path] = {
      "url" => url,
      "api_key" => api_key
    }
  end

  def save
    File.write(@filename, @raw_config.to_yaml)
  end

  def [](path)
    PathSetting.new(self, path)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
discourse_theme-0.1.8 lib/discourse_theme/config.rb
discourse_theme-0.1.7 lib/discourse_theme/config.rb
discourse_theme-0.1.6 lib/discourse_theme/config.rb
discourse_theme-0.1.5 lib/discourse_theme/config.rb
discourse_theme-0.1.4 lib/discourse_theme/config.rb
discourse_theme-0.1.3 lib/discourse_theme/config.rb
discourse_theme-0.1.2 lib/discourse_theme/config.rb
discourse_theme-0.1.1 lib/discourse_theme/config.rb