Sha256: 9ba6b6f29cb42316b8cfd1463fd4ba6dda73bafc16b20cfc633f4c19cac54101

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

# frozen_string_literal: true

module Milestoner
  # Default configuration for gem with support for custom settings.
  class Configuration
    def initialize file_name = Identity.file_name, defaults: {}
      @file_name = file_name
      @defaults = defaults
    end

    def local_file_path
      File.join Dir.pwd, file_name
    end

    def global_file_path
      File.join ENV["HOME"], file_name
    end

    def global?
      File.exist? global_file_path
    end

    def local?
      File.exist? local_file_path
    end

    def computed_file_path
      File.exist?(local_file_path) ? local_file_path : global_file_path
    end

    def settings
      defaults.merge(load_settings).reject { |_, value| value.nil? }
    end

    private

    attr_reader :file_name, :defaults

    def load_settings
      yaml = YAML.load_file computed_file_path
      yaml.is_a?(Hash) ? yaml : {}
    rescue
      defaults
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
milestoner-3.0.0 lib/milestoner/configuration.rb