Sha256: 9bd57af4926db9903502c06e24ef04b5de4737ed08ddc432d43650f72fc43ca8

Contents?: true

Size: 803 Bytes

Versions: 9

Compression:

Stored size: 803 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)
      end

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

      def inspect
        [pair.key, dynamic].compact.join XDG::PAIR_DELIMITER
      end

      private

      attr_reader :pair, :environment

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
xdg-5.1.0 lib/xdg/paths/home.rb
xdg-5.0.0 lib/xdg/paths/home.rb
xdg-4.5.0 lib/xdg/paths/home.rb
xdg-4.4.0 lib/xdg/paths/home.rb
xdg-4.3.0 lib/xdg/paths/home.rb
xdg-4.2.0 lib/xdg/paths/home.rb
xdg-4.1.0 lib/xdg/paths/home.rb
xdg-4.0.1 lib/xdg/paths/home.rb
xdg-4.0.0 lib/xdg/paths/home.rb