Sha256: c3765f0e99ec4462f0bd88c27dbee69087e51183cc0fde56e94d627ac4a51be7

Contents?: true

Size: 913 Bytes

Versions: 3

Compression:

Stored size: 913 Bytes

Contents

# frozen_string_literal: true

module EacRubyBase0
  # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
  class ApplicationXdg
    class << self
      # @return [Pathname]
      def user_home_dir_from_env
        ENV.fetch('HOME').to_pathname
      end
    end

    DIRECTORIES = { cache: '.cache', config: '.config', data: '.local/share',
                    state: '.local/state' }.freeze

    common_constructor :app_name, :user_home_dir, default: [nil] do
      self.user_home_dir ||= self.class.user_home_dir_from_env
    end

    DIRECTORIES.each do |item, subpath|
      xdg_env_method_name = "#{item}_xdg_env"

      define_method xdg_env_method_name do
        ENV["XDG_#{item.upcase}_HOME"].if_present(&:to_pathname)
      end

      define_method "#{item}_dir" do
        (send(xdg_env_method_name) || user_home_dir.join(subpath)).join(app_name)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
avm-tools-0.102.2 vendor/eac_ruby_base0/lib/eac_ruby_base0/application_xdg.rb
avm-tools-0.102.1 vendor/eac_ruby_base0/lib/eac_ruby_base0/application_xdg.rb
eac_ruby_base0-0.8.0 lib/eac_ruby_base0/application_xdg.rb