Class: DiscourseTheme::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_theme/config.rb

Defined Under Namespace

Classes: PathSetting

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Config

Returns a new instance of Config.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/discourse_theme/config.rb', line 46

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

Instance Attribute Details

#filename ⇒ Object (readonly)

Returns the value of attribute filename.



44
45
46
# File 'lib/discourse_theme/config.rb', line 44

def filename
  @filename
end

#raw_config ⇒ Object (readonly)

Returns the value of attribute raw_config.



44
45
46
# File 'lib/discourse_theme/config.rb', line 44

def raw_config
  @raw_config
end

Instance Method Details

#[](path) ⇒ Object



73
74
75
# File 'lib/discourse_theme/config.rb', line 73

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

#save ⇒ Object



69
70
71
# File 'lib/discourse_theme/config.rb', line 69

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

#set(path, url:, api_key:) ⇒ Object



62
63
64
65
66
67
# File 'lib/discourse_theme/config.rb', line 62

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