Class: Unipept::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Configuration

Creates a new config object, based on a given YAML file. If no filename given, ‘.unipeptrc’ in the home dir of the user will be used.

If the file doesn’t exist, an empty config will be loaded.

config from

Parameters:

  • file (String) (defaults to: nil)

    An optional file name of the YAML file to create the



14
15
16
17
18
19
20
21
# File 'lib/configuration.rb', line 14

def initialize(file = nil)
  @file_name = file || File.join(Dir.home, '.unipeptrc')
  @config = if File.exist? file_name
              YAML.load_file file_name, permitted_classes: [Time]
            else
              {}
            end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/configuration.rb', line 5

def config
  @config
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



5
6
7
# File 'lib/configuration.rb', line 5

def file_name
  @file_name
end

Instance Method Details

#[](*args) ⇒ Object

forwards [] to the internal config hash



35
36
37
# File 'lib/configuration.rb', line 35

def [](*args)
  config.[](*args)
end

#[]=(*args) ⇒ Object

forwards =[] to the internal config hash



40
41
42
# File 'lib/configuration.rb', line 40

def []=(*args)
  config.[]=(*args) # rubocop:disable Layout/SpaceBeforeBrackets
end

#delete(key) ⇒ Object

Deletes a key



30
31
32
# File 'lib/configuration.rb', line 30

def delete(key)
  config.delete(key)
end

#saveObject

Saves the config to disk. If the file doesn’t exist yet, a new one will be created



25
26
27
# File 'lib/configuration.rb', line 25

def save
  File.write(file_name, config.to_yaml)
end