Sha256: fa8a8d3a7a486c58e92834d5a98fccbe88f67ef2df06ff845a760b14352744fd

Contents?: true

Size: 802 Bytes

Versions: 11

Compression:

Stored size: 802 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 to_s = [pair.key, dynamic].compact.join XDG::DELIMITER

      alias to_str to_s

      def inspect = "#<#{self.class}:#{object_id} #{self}>"

      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

11 entries across 11 versions & 1 rubygems

Version Path
xdg-8.10.0 lib/xdg/paths/home.rb
xdg-8.9.0 lib/xdg/paths/home.rb
xdg-8.8.0 lib/xdg/paths/home.rb
xdg-8.7.0 lib/xdg/paths/home.rb
xdg-8.6.0 lib/xdg/paths/home.rb
xdg-8.5.0 lib/xdg/paths/home.rb
xdg-8.4.1 lib/xdg/paths/home.rb
xdg-8.3.0 lib/xdg/paths/home.rb
xdg-8.2.0 lib/xdg/paths/home.rb
xdg-8.1.0 lib/xdg/paths/home.rb
xdg-8.0.0 lib/xdg/paths/home.rb