Sha256: a334ea5d7507eb09671e974c972b5de0448133c47b6640adc8b4d7cc472013fd

Contents?: true

Size: 775 Bytes

Versions: 4

Compression:

Stored size: 775 Bytes

Contents

module Milestoner
  # Default configuration for gem with support for custom settings.
  class Configuration
    def initialize file_name = ".unknownrc", 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 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

4 entries across 4 versions & 1 rubygems

Version Path
milestoner-1.1.0 lib/milestoner/configuration.rb
milestoner-1.0.0 lib/milestoner/configuration.rb
milestoner-0.5.0 lib/milestoner/configuration.rb
milestoner-0.4.0 lib/milestoner/configuration.rb