Sha256: 0201138d73829ca4a23fa4e1c9d3af0452e6d31135b9e207450bd5c67bfd565f

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 Bytes

Contents

# frozen_string_literal: true

require 'yaml'

class Repofetch
  # Utilities for managing config.
  class Config
    PATH = File.expand_path('.repofetch.yml', Dir.home)
    DEFAULT_CONFIG = File.read(File.expand_path('DEFAULT_CONFIG', __dir__))

    # Loads from config file.
    def self.load
      new(File.read(PATH))
    end

    # Loads from a config file if it exists. If it doesn't, it writes the config file,
    # then creates a default.
    def self.load!
      if File.exist?(PATH)
        self.load
      else
        File.write(PATH, DEFAULT_CONFIG)
        new(DEFAULT_CONFIG)
      end
    end

    # @param config_yaml [String] a YAML string
    def initialize(config_yaml = DEFAULT_CONFIG)
      @config = YAML.safe_load(config_yaml, symbolize_names: true)
    end

    def plugins
      @config[:plugins] || []
    end

    def [](key)
      @config[key]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
repofetch-0.4.0 lib/repofetch/config.rb