Sha256: edbb7819181025dc7cf64e0d12906950a4aa0e6f312bd073be9c53f2a9442e64

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require "pathname"

module XDG
  module Paths
    # A collection of XDG directories.
    class Directory
      DELIMITER = ":"

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

      def default = value.split(DELIMITER).map { |path| expand path }

      def dynamic
        String(environment[key]).then { |env_value| env_value.empty? ? value : env_value }
                                .split(DELIMITER)
                                .uniq
                                .map { |path| expand path }
      end

      def to_s = [key, dynamic.join(DELIMITER)].reject(&:empty?).join XDG::DELIMITER

      alias to_str to_s

      def inspect
        pairs = to_s
        type = self.class

        pairs.empty? ? "#<#{type}:#{object_id}>" : "#<#{type}:#{object_id} #{self}>"
      end

      private

      attr_reader :pair, :environment

      def key = String pair.key

      def value = String pair.value

      def expand(path) = Pathname(path).expand_path
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xdg-9.0.0 lib/xdg/paths/directory.rb