Sha256: 97a59d4cbf9eea78eccf1c23e0aec7a326c7e826125b8ebd9630e0d88d61f1f7

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

require 'yaml'

module Unipept
  class Configuration
    attr_reader :config, :file_name

    # 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.
    #
    # @param [String] file An optional file name of the YAML file to create the
    # config from
    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

    # Saves the config to disk. If the file doesn't exist yet, a new one will be
    # created
    def save
      File.write(file_name, config.to_yaml)
    end

    # Deletes a key
    def delete(key)
      config.delete(key)
    end

    # forwards [] to the internal config hash
    def [](*args)
      config.[](*args)
    end

    # forwards =[] to the internal config hash
    def []=(*args)
      config.[]=(*args) # rubocop:disable Layout/SpaceBeforeBrackets
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
unipept-3.1.0 lib/configuration.rb
unipept-3.0.2 lib/configuration.rb
unipept-3.0.1 lib/configuration.rb
unipept-3.0.0 lib/configuration.rb
unipept-2.2.2 lib/configuration.rb