File: directory.rb

package info (click to toggle)
ruby-xdg 9.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136 kB
  • sloc: ruby: 199; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,092 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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