Sha256: 069a1e133bcaad651a51895797002cd02b34229dcccd9f9a9b43e1e4265ba222

Contents?: true

Size: 722 Bytes

Versions: 4

Compression:

Stored size: 722 Bytes

Contents

# frozen_string_literal: true

require "forwardable"
require "pathname"

module XDG
  module Paths
    # A XDG home path.
    class Home
      extend Forwardable

      KEY = "HOME"

      delegate %i[key value] => :pair

      def initialize pair, environment = ENV
        @pair = pair
        @environment = environment
      end

      def default = expand(String(value))

      def dynamic = String(environment[key]).then { |path| path.empty? ? default : expand(path) }

      def inspect = [pair.key, dynamic].compact.join(XDG::DELIMITER)

      private

      attr_reader :pair, :environment

      def expand(path) = home.join(path).expand_path

      def home = Pathname(environment.fetch(KEY))
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
xdg-7.0.3 lib/xdg/paths/home.rb
xdg-7.0.2 lib/xdg/paths/home.rb
xdg-7.0.1 lib/xdg/paths/home.rb
xdg-7.0.0 lib/xdg/paths/home.rb