Sha256: 258ebf61478cdf3bd5876c6b5aa8a3b76b4170d06ad1c9169bb9270486797912

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 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
      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

11 entries across 11 versions & 1 rubygems

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