Sha256: 9eb5bf518b7a5f9eff325050df768ca96318d8878d089b9b94de2fafed0beb24

Contents?: true

Size: 967 Bytes

Versions: 6

Compression:

Stored size: 967 Bytes

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
      end

      def default
        value.split(DELIMITER).map(&method(:expand))
      end

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

      def inspect
        [key, dynamic.join(DELIMITER)].reject(&:empty?).join XDG::PAIR_DELIMITER
      end

      private

      attr_reader :pair, :environment

      def key
        String pair.key
      end

      def value
        String pair.value
      end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
xdg-4.4.0 lib/xdg/paths/directory.rb
xdg-4.3.0 lib/xdg/paths/directory.rb
xdg-4.2.0 lib/xdg/paths/directory.rb
xdg-4.1.0 lib/xdg/paths/directory.rb
xdg-4.0.1 lib/xdg/paths/directory.rb
xdg-4.0.0 lib/xdg/paths/directory.rb