Class: Furaffinity::Config

Inherits:
Object
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/furaffinity/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/furaffinity/config.rb', line 9

def initialize(path)
  @path = path
  if File.exist?(path)
    logger.measure_trace("Loading configuration") do
      @config_hash = YAML.safe_load_file(path)
    end
  else
    @config_hash = {}
  end
rescue => e
  logger.fatal("Error while loading configuration:", e)
  raise
end

Instance Method Details

#[](key) ⇒ Object Also known as: get



26
# File 'lib/furaffinity/config.rb', line 26

def [](key) = @config_hash[key.to_s]

#[]=(key, value) ⇒ Object



29
30
31
# File 'lib/furaffinity/config.rb', line 29

def []=(key, value)
  @config_hash[key.to_s] = value
end

#new_clientFuraffinity::Client

Returns:



24
# File 'lib/furaffinity/config.rb', line 24

def new_client = Furaffinity::Client.new(a: self[:a], b: self[:b])

#saveObject



44
45
46
47
48
49
50
51
# File 'lib/furaffinity/config.rb', line 44

def save
  logger.measure_debug("Saving configuration") do
    yaml = @config_hash.to_yaml
    File.open(@path, "w") do |f|
      f.puts yaml
    end
  end
end

#set(**kwargs) ⇒ Object



33
34
35
36
37
# File 'lib/furaffinity/config.rb', line 33

def set(**kwargs)
  kwargs.each do |k, v|
    self[k] = v
  end
end

#set!(**kwargs) ⇒ Object



39
40
41
42
# File 'lib/furaffinity/config.rb', line 39

def set!(**kwargs)
  set(**kwargs)
  save
end