pax_global_header00006660000000000000000000000064151703170440014514gustar00rootroot0000000000000052 comment=d3151bc9f0fce5b5afff3d758b2cc20f3505e3e3 bkuhlmann-xdg-c366e31/000077500000000000000000000000001517031704400146075ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/.circleci/000077500000000000000000000000001517031704400164425ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/.circleci/config.yml000066400000000000000000000015541517031704400204370ustar00rootroot00000000000000version: 2.1 jobs: build: working_directory: ~/project docker: - image: bkuhlmann/alpine-ruby:latest steps: - checkout - restore_cache: name: Gems Restore keys: - gem-cache-{{.Branch}}-{{checksum "Gemfile"}}-{{checksum "xdg.gemspec"}} - gem-cache- - run: name: Gems Install command: | gem update --system bundle config set path "vendor/bundle" bundle install - save_cache: name: Gems Store key: gem-cache-{{.Branch}}-{{checksum "Gemfile"}}-{{checksum "xdg.gemspec"}} paths: - vendor/bundle - run: name: Rake command: bundle exec rake - store_artifacts: name: SimpleCov Report path: ~/project/coverage destination: coverage bkuhlmann-xdg-c366e31/.config/000077500000000000000000000000001517031704400161325ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/.config/rubocop/000077500000000000000000000000001517031704400176035ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/.config/rubocop/config.yml000066400000000000000000000000471517031704400215740ustar00rootroot00000000000000inherit_gem: caliber: config/all.yml bkuhlmann-xdg-c366e31/.github/000077500000000000000000000000001517031704400161475ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/.github/FUNDING.yml000066400000000000000000000000241517031704400177600ustar00rootroot00000000000000github: [bkuhlmann] bkuhlmann-xdg-c366e31/.github/ISSUE_TEMPLATE/000077500000000000000000000000001517031704400203325ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/.github/ISSUE_TEMPLATE/config.yml000066400000000000000000000004371517031704400223260ustar00rootroot00000000000000blank_issues_enabled: false contact_links: - name: Community url: https://alchemists.io/community about: Please ask questions or discuss specifics here. - name: Security url: https://alchemists.io/policies/security about: Please report security vulnerabilities here. bkuhlmann-xdg-c366e31/.github/ISSUE_TEMPLATE/issue.md000066400000000000000000000011031517031704400217770ustar00rootroot00000000000000--- name: Issue title: "Add|Update|Fix|Remove|Refactor " about: Report an issue. Please use only one of the subject prefixes. --- ## Why ## How ## Notes bkuhlmann-xdg-c366e31/.github/PULL_REQUEST_TEMPLATE.md000066400000000000000000000005331517031704400217510ustar00rootroot00000000000000## Overview ## Screenshots/Screencasts ## Details bkuhlmann-xdg-c366e31/.gitignore000066400000000000000000000000431517031704400165740ustar00rootroot00000000000000*.gem .bundle Gemfile.lock pkg tmp bkuhlmann-xdg-c366e31/.reek.yml000066400000000000000000000001351517031704400163350ustar00rootroot00000000000000exclude_paths: - tmp - vendor detectors: ModuleInitialize: exclude: - "XDG" bkuhlmann-xdg-c366e31/.ruby-version000066400000000000000000000000061517031704400172500ustar00rootroot000000000000004.0.2 bkuhlmann-xdg-c366e31/CITATION.cff000066400000000000000000000012061517031704400165000ustar00rootroot00000000000000cff-version: 1.2.0 message: Please use the following metadata when citing this project in your work. title: XDG abstract: A XDG Base Directory Specification implementation. version: 10.2.0 license: Hippocratic-2.1 date-released: 2026-04-16 authors: - family-names: Kuhlmann given-names: Brooke affiliation: Alchemists orcid: https://orcid.org/0000-0002-5810-6268 keywords: - ruby - command line interface - XDG - base directory specification - cache - configuration - data - runtime repository-code: https://github.com/bkuhlmann/xdg repository-artifact: https://rubygems.org/gems/xdg url: https://alchemists.io/projects/xdg bkuhlmann-xdg-c366e31/Gemfile000066400000000000000000000010211517031704400160740ustar00rootroot00000000000000# frozen_string_literal: true ruby file: ".ruby-version" source "https://rubygems.org" gemspec group :quality do gem "caliber", "~> 0.90" gem "git-lint", "~> 10.0" gem "reek", "~> 6.5", require: false gem "simplecov", "~> 0.22", require: false end group :development do gem "rake", "~> 13.4" end group :test do gem "refinements", "~> 14.0" gem "rspec", "~> 3.13" end group :tools do gem "amazing_print", "~> 2.0" gem "debug", "~> 1.11" gem "irb-kit", "~> 2.0" gem "repl_type_completor", "~> 0.1" end bkuhlmann-xdg-c366e31/LICENSE.adoc000066400000000000000000000201261517031704400165220ustar00rootroot00000000000000= Hippocratic License Version: 2.1.0. Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability. Permission and Conditions. The Licensor grants permission by this license ("License"), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the "Licensee") obtaining a copy of this software and associated documentation files (the "Software"), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions: * Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor. * Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately. * Compliance with Human Rights Principles and Human Rights Laws. [arabic] . Human Rights Principles. [loweralpha] .. Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the "Human Rights Principles"). Licensee shall use the Software in a manner consistent with Human Rights Principles. .. Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the "Rules"); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise. + Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal’s powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply. . Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. "Human Rights Laws" means any applicable laws, regulations, or rules (collectively, "Laws") that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply. . Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles. * Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee. * Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party. * Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES "AS IS," WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM. This Hippocratic License is an link:https://ethicalsource.dev[Ethical Source license] and is offered for use by licensors and licensees at their own risk, on an "AS IS" basis, and with no warranties express or implied, to the maximum extent permitted by Laws. bkuhlmann-xdg-c366e31/README.adoc000066400000000000000000000300731517031704400163770ustar00rootroot00000000000000:toc: macro :toclevels: 5 :figure-caption!: :dotfiles_link: link:https://alchemists.io/projects/dotfiles[Dotfiles] :runcom_link: link:https://alchemists.io/projects/runcom[Runcom] :sod_link: link:https://alchemists.io/projects/sod[Sod] = XDG Provides a Ruby implementation of the link:https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html[XDG Base Directory Specification] for managing common configurations without polluting your {dotfiles_link}. XDG is great for command line interfaces or any application that needs a common configuration, cache, data, state, or runtime. 💡 If you write a lot of Command Line Interfaces (CLIs) and would like additional behavior built atop this gem, then check out the {runcom_link} gem or the more advanced {sod_link} gem. toc::[] == Features * Provides a `XDG` object that adheres to the _XDG Base Directory Specification_ with access to the following environment settings: ** `$XDG_CACHE_HOME` ** `$XDG_CONFIG_HOME` ** `$XDG_CONFIG_DIRS` ** `$XDG_DATA_HOME` ** `$XDG_DATA_DIRS` ** `$XDG_STATE_HOME` == Requirements . https://www.ruby-lang.org[Ruby] == Setup To install _with_ security, run: [source,bash] ---- # 💡 Skip this line if you already have the public certificate installed. gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem) gem install xdg --trust-policy HighSecurity ---- To install _without_ security, run: [source,bash] ---- gem install xdg ---- You can also add the gem directly to your project: [source,bash] ---- bundle add xdg ---- Once the gem is installed, you only need to require it: [source,ruby] ---- require "xdg" ---- == Usage The following describes how to use this implementation. === Objects To get up and running quickly, use `XDG.new` as follows: [source,ruby] ---- xdg = XDG.new xdg.cache_home # Answers computed `$XDG_CACHE_HOME` value. xdg.config_home # Answers computed `$XDG_CONFIG_HOME` value. xdg.config_dirs # Answers computed `$XDG_CONFIG_DIRS` value. xdg.data_home # Answers computed `$XDG_DATA_HOME` value. xdg.data_dirs # Answers computed `$XDG_DATA_DIRS` value. xdg.state_home # Answers computed `$XDG_STATE_HOME` value. ---- Behinds the scenes, use of `XDG.new` wraps `XDG::Environment.new` which provides a unified Object API to the following objects: [source,ruby] ---- cache = XDG::Cache.new config = XDG::Config.new data = XDG::Data.new state = XDG::State.new ---- Generally, use of `XDG.new` is all you need but knowing you can create a specialized instance of any aspect of the XDG specification can be handy for specific use cases. Additionally, the `cache`, `config`, `data`, and `state` objects share the same API which means you can send the following messages: * `#home`: Answers the home directory as computed via the `$XDG_*_HOME` key. * `#directories`: Answers an array directories as computed via the `$XDG_*_DIRS` key. * `#all`: Answers an array of _all_ directories as computed from the combined `$XDG_*_HOME` and `$XDG_*_DIRS` values (with `$XDG_*_HOME` prefixed at the start of the array). * `#to_s`: Answers an _explicit_ string cast for the current environment. * `#to_str`: Answers an _implicit_ string cast for the current environment. * `#inspect`: Answers object inspection complete with object type, object ID, and all environment variables. The _computed_ value of each method is either the user-defined value of the key or the default value, per specification, when the key is not defined or empty. For more on this, scroll down to the _Variable Defaults_ section to learn more. === Examples The following are examples of what you will see when exploring the XDG objects within an IRB console: [source,ruby] ---- require "xdg" # Initialization xdg = XDG.new cache = XDG::Cache.new config = XDG::Config.new data = XDG::Data.new state = XDG::State.new # Paths xdg.cache_home # "#" xdg.config_home # "#" xdg.config_dirs # ["#"] xdg.data_home # "#" xdg.data_dirs # ["#", "#"] xdg.state_home # "#" cache.home # "#" cache.directories # [] cache.all # ["#"] config.home # "#" config.directories # ["#"] config.all # ["#", "#"] data.home # "#" data.directories # ["#", "#"] data.all # ["#", "#", "#"] state.home # "#" state.directories # [] state.all # ["#"] # Casts (explicit and implicit) xdg.to_s # "XDG_CACHE_HOME=/Users/demo/.cache XDG_CONFIG_HOME=/Users/demo/.config XDG_CONFIG_DIRS=/etc/xdg XDG_DATA_HOME=/Users/demo/.local/share XDG_DATA_DIRS=/usr/local/share:/usr/share XDG_STATE_HOME=/Users/demo/.local/state" cache.to_s # "XDG_CACHE_HOME=/Users/demo/.cache" config.to_s # "XDG_CONFIG_HOME=/Users/demo/.config XDG_CONFIG_DIRS=/etc/xdg" data.to_s # "XDG_DATA_HOME=/Users/demo/.local/share XDG_DATA_DIRS=/usr/local/share:/usr/share" state.to_s # "XDG_STATE_HOME=/Users/demo/.local/state" xdg.to_str # "XDG_CACHE_HOME=/Users/demo/.cache XDG_CONFIG_HOME=/Users/demo/.config XDG_CONFIG_DIRS=/etc/xdg XDG_DATA_HOME=/Users/demo/.local/share XDG_DATA_DIRS=/usr/local/share:/usr/share XDG_STATE_HOME=/Users/demo/.local/state" cache.to_str # "XDG_CACHE_HOME=/Users/demo/.cache" config.to_str # "XDG_CONFIG_HOME=/Users/demo/.config XDG_CONFIG_DIRS=/etc/xdg" data.to_str # "XDG_DATA_HOME=/Users/demo/.local/share XDG_DATA_DIRS=/usr/local/share:/usr/share" state.to_str # "XDG_STATE_HOME=/Users/demo/.local/state" # Inspection xdg.inspect # "#" cache.inspect # "#" config.inspect # "#" data.inspect # "#" state.inspect # "#" ---- === Variable Defaults The _XDG Base Directory Specification_ defines environment variables and associated default values when not defined or empty. The following defaults, per specification, are implemented by the `XDG` objects: * `$XDG_CACHE_HOME="$HOME/.cache"` * `$XDG_CONFIG_HOME="$HOME/.config"` * `$XDG_CONFIG_DIRS="/etc/xdg"` * `$XDG_DATA_HOME="$HOME/.local/share"` * `$XDG_DATA_DIRS="/usr/local/share/:/usr/share/"` * `$XDG_RUNTIME_DIR` * `$XDG_STATE_HOME="$HOME/.local/state"` The `$XDG_RUNTIME_DIR` environment variable deserves special mention because it’s not, _currently_, implemented as part of this gem because it is more user/environment specific. Here is how the `$XDG_RUNTIME_DIR` is meant to be used should you choose to use it: * _Must_ reference user-specific non-essential runtime files and other file objects (such as sockets, named pipes, etc.) * _Must_ be owned by the user with _only_ the user having read and write access to it. * _Must_ have a Unix access mode of `0700`. * _Must_ be bound to the user when logging in. * _Must_ be removed when the user logs out. * _Must_ be pointed to the same directory when the user logs in more than once. * _Must_ exist from first login to last logout on the system and not removed in between. * _Must_ not allow files in the directory to survive reboot or a full logout/login cycle. * _Must_ keep the directory on the local file system and not shared with any other file systems. * _Must_ keep the directory fully-featured by the standards of the operating system. Specifically, on Unix-like operating systems AF_UNIX sockets, symbolic links, hard links, proper permissions, file locking, sparse files, memory mapping, file change notifications, a reliable hard link count must be supported, and no restrictions on the file name character set should be imposed. Files in this directory _may_ be subjected to periodic clean-up. To ensure files are not removed, they should have their access time timestamp modified at least once every 6 hours of monotonic time or the '`sticky`' bit should be set on the file. * When not set, applications should fall back to a replacement directory with similar capabilities and print a warning message. Applications should use this directory for communication and synchronization purposes and should not place larger files in it, since it might reside in runtime memory and cannot necessarily be swapped out to disk. === Variable Behavior The behavior of most XDG environment variables can be lumped into two categories: * `$XDG_*_DIRS` * `$XDG_*_HOME` Each is described in detail below. ==== Directories These variables are used to define a colon (`:`) delimited list of directories. Order is important as the first directory defined will take precedent over the following directory and so forth. For example, here is a situation where the `XDG_CONFIG_DIRS` key has a custom value: [source,bash] ---- XDG_CONFIG_DIRS="/demo/one/.config:/demo/two/.settings:/demo/three/.configuration" ---- The above then yields the following, colon delimited, array: [source,ruby] ---- [ "/demo/one/.config", "/demo/two/.settings", "/demo/three/.configuration" ] ---- In the above example, the `"/demo/one/.config"` path takes _highest_ priority since it was defined first. ==== Homes These variables take precedence over the corresponding `$XDG_*_DIRS` environment variables. Using a modified version of the `$XDG_*_DIRS` example, shown above, we could have the following setup: [source,bash] ---- XDG_CONFIG_HOME="/demo/priority" XDG_CONFIG_DIRS="/demo/one/.config:/demo/two/.settings" ---- The above then yields the following, colon delimited, array: [source,ruby] ---- [ "/demo/priority", "/demo/one/.config", "/demo/two/.settings" ] ---- Due to `XDG_CONFIG_HOME` taking precedence over the `XDG_CONFIG_DIRS`, the path with the _highest_ priority is: `"/demo/priority"`. === Variable Priority Path precedence is determined in the following order (with the first taking highest priority): . `$XDG_*_HOME` - Will be used if defined. Otherwise, falls back to specification default. . `$XDG_*_DIRS` - Iterates through directories in order defined (with first taking highest priority). Otherwise, falls back to specification default. == Development To contribute, run: [source,bash] ---- git clone https://github.com/demo/xdg cd xdg bin/setup ---- You can also use the IRB console for direct access to all objects: [source,bash] ---- bin/console ---- Lastly, there is a `bin/demo` script which displays default functionality for quick visual reference. This is the same script used to generate the usage examples shown at the top of this document. [source,bash] ---- bin/demo ---- == Tests To test, run: [source,bash] ---- bin/rake ---- == link:https://alchemists.io/policies/license[License] == link:https://alchemists.io/policies/security[Security] == link:https://alchemists.io/policies/code_of_conduct[Code of Conduct] == link:https://alchemists.io/policies/contributions[Contributions] == link:https://alchemists.io/policies/developer_certificate_of_origin[Developer Certificate of Origin] == link:https://alchemists.io/projects/xdg/versions[Versions] == link:https://alchemists.io/community[Community] == Credits * Built with link:https://alchemists.io/projects/gemsmith[Gemsmith]. * Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann]. * Created by link:https://github.com/trans[Thomas Sawyer] (link:https://github.com/rubyworks[Rubyworks]) in 2008 then transferred to Brooke in 2019. bkuhlmann-xdg-c366e31/Rakefile000066400000000000000000000006311517031704400162540ustar00rootroot00000000000000# frozen_string_literal: true require "bundler/setup" require "git/lint/rake/register" require "reek/rake/task" require "rspec/core/rake_task" require "rubocop/rake_task" Git::Lint::Rake::Register.call Reek::Rake::Task.new RSpec::Core::RakeTask.new { |task| task.verbose = false } RuboCop::RakeTask.new desc "Run code quality checks" task quality: %i[git_lint reek rubocop] task default: %i[quality spec] bkuhlmann-xdg-c366e31/bin/000077500000000000000000000000001517031704400153575ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/bin/console000077500000000000000000000002241517031704400167450ustar00rootroot00000000000000#! /usr/bin/env ruby # frozen_string_literal: true require "bundler/setup" Bundler.require :tools require "irb" require "xdg" IRB.start __FILE__ bkuhlmann-xdg-c366e31/bin/demo000077500000000000000000000055641517031704400162430ustar00rootroot00000000000000#! /usr/bin/env ruby # frozen_string_literal: true require "bundler/setup" Bundler.require :tools require "xdg" home = Dir.home demo = "/Users/demo" xdg = XDG.new cache = XDG::Cache.new config = XDG::Config.new data = XDG::Data.new state = XDG::State.new puts "# Initialization\n\n" puts "xdg = XDG.new" puts "cache = XDG::Cache.new" puts "config = XDG::Config.new" puts "data = XDG::Data.new" puts "state = XDG::State.new" puts "\n# Paths\n\n" puts %(xdg.cache_home # "#{xdg.cache_home.sub(home, demo).inspect}") puts %(xdg.config_home # "#{xdg.config_home.sub(home, demo).inspect}") puts %(xdg.config_dirs # #{xdg.config_dirs.map(&:inspect)}) puts %(xdg.data_home # "#{xdg.data_home.sub(home, demo).inspect}") puts %(xdg.data_dirs # #{xdg.data_dirs.map(&:inspect)}) puts %(xdg.state_home # "#{xdg.state_home.sub(home, demo).inspect}") puts puts %(cache.home # "#{cache.home.sub(home, demo).inspect}") puts %(cache.directories # #{cache.directories.map { |path| path.sub(home, demo).inspect }}) puts %(cache.all # #{cache.all.map { |path| path.sub(home, demo).inspect }}) puts puts %(config.home # "#{config.home.sub(home, demo).inspect}") puts %(config.directories # #{config.directories.map { |path| path.sub(home, demo).inspect }}) puts %(config.all # #{config.all.map { |path| path.sub(home, demo).inspect }}) puts puts %(data.home # "#{data.home.sub(home, demo).inspect}") puts %(data.directories # #{data.directories.map { |path| path.sub(home, demo).inspect }}) puts %(data.all # #{data.all.map { |path| path.sub(home, demo).inspect }}) puts puts %(state.home # "#{state.home.sub(home, demo).inspect}") puts %(state.directories # #{state.directories.map { |path| path.sub(home, demo).inspect }}) puts %(state.all # #{state.all.map { |path| path.sub(home, demo).inspect }}) puts "\n# Casts (explicit and implicit)\n\n" puts %(xdg.to_s # "#{xdg.to_s.gsub home, demo}") puts %(cache.to_s # "#{cache.to_s.gsub home, demo}") puts %(config.to_s # "#{config.to_s.gsub home, demo}") puts %(data.to_s # "#{data.to_s.gsub home, demo}") puts %(state.to_s # "#{state.to_s.gsub home, demo}") puts puts %(xdg.to_str # "#{xdg.to_str.gsub home, demo}") puts %(cache.to_str # "#{cache.to_str.gsub home, demo}") puts %(config.to_str # "#{config.to_str.gsub home, demo}") puts %(data.to_str # "#{data.to_str.gsub home, demo}") puts %(state.to_str # "#{state.to_str.gsub home, demo}") puts "\n# Inspection\n\n" puts %(xdg.inspect # "#{xdg.inspect.gsub home, demo}") puts %(cache.inspect # "#{cache.inspect.gsub home, demo}") puts %(config.inspect # "#{config.inspect.gsub home, demo}") puts %(data.inspect # "#{data.inspect.gsub home, demo}") puts %(state.inspect # "#{state.inspect.gsub home, demo}") bkuhlmann-xdg-c366e31/bin/rake000077500000000000000000000001561517031704400162310ustar00rootroot00000000000000#! /usr/bin/env ruby # frozen_string_literal: true require "bundler/setup" load Gem.bin_path "rake", "rake" bkuhlmann-xdg-c366e31/bin/rspec000077500000000000000000000001651517031704400164230ustar00rootroot00000000000000#! /usr/bin/env ruby # frozen_string_literal: true require "bundler/setup" load Gem.bin_path "rspec-core", "rspec" bkuhlmann-xdg-c366e31/bin/rubocop000077500000000000000000000001641517031704400167570ustar00rootroot00000000000000#! /usr/bin/env ruby # frozen_string_literal: true require "bundler/setup" load Gem.bin_path "rubocop", "rubocop" bkuhlmann-xdg-c366e31/bin/setup000077500000000000000000000006031517031704400164440ustar00rootroot00000000000000#! /usr/bin/env ruby # frozen_string_literal: true require "debug" require "fileutils" APP_ROOT = Pathname(__dir__).join("..").expand_path Runner = lambda do |*arguments, kernel: Kernel| kernel.system(*arguments) || kernel.abort("\nERROR: Command #{arguments.inspect} failed.") end FileUtils.chdir APP_ROOT do puts "Installing dependencies..." Runner.call "bundle install" end bkuhlmann-xdg-c366e31/lib/000077500000000000000000000000001517031704400153555ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/lib/xdg.rb000066400000000000000000000005061517031704400164650ustar00rootroot00000000000000# frozen_string_literal: true require "xdg/cache" require "xdg/config" require "xdg/data" require "xdg/environment" require "xdg/pair" require "xdg/paths/combined" require "xdg/paths/directory" require "xdg/paths/home" require "xdg/state" # Main namespace. module XDG DELIMITER = "=" def self.new = Environment.new end bkuhlmann-xdg-c366e31/lib/xdg/000077500000000000000000000000001517031704400161375ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/lib/xdg/cache.rb000066400000000000000000000011741517031704400175320ustar00rootroot00000000000000# frozen_string_literal: true require "forwardable" require "xdg/pair" module XDG # Provides cache support. class Cache extend Forwardable HOME_PAIR = Pair["XDG_CACHE_HOME", ".cache"].freeze delegate %i[home directories all to_s to_str] => :combined def initialize home: Paths::Home, directories: Paths::Directory, environment: ENV @combined = Paths::Combined.new home.new(HOME_PAIR, environment), directories.new(Pair.new, environment) freeze end def inspect = "#<#{self.class}:#{object_id} #{self}>" private attr_reader :combined end end bkuhlmann-xdg-c366e31/lib/xdg/config.rb000066400000000000000000000013031517031704400177260ustar00rootroot00000000000000# frozen_string_literal: true require "forwardable" require "xdg/pair" module XDG # Provides configuration support. class Config extend Forwardable HOME_PAIR = Pair["XDG_CONFIG_HOME", ".config"].freeze DIRS_PAIR = Pair["XDG_CONFIG_DIRS", "/etc/xdg"].freeze delegate %i[home directories all to_s to_str] => :combined def initialize home: Paths::Home, directories: Paths::Directory, environment: ENV @combined = Paths::Combined.new home.new(HOME_PAIR, environment), directories.new(DIRS_PAIR, environment) freeze end def inspect = "#<#{self.class}:#{object_id} #{self}>" private attr_reader :combined end end bkuhlmann-xdg-c366e31/lib/xdg/data.rb000066400000000000000000000013141517031704400173740ustar00rootroot00000000000000# frozen_string_literal: true require "forwardable" require "xdg/pair" module XDG # Provides data support. class Data extend Forwardable HOME_PAIR = Pair["XDG_DATA_HOME", ".local/share"].freeze DIRS_PAIR = Pair["XDG_DATA_DIRS", "/usr/local/share:/usr/share"].freeze delegate %i[home directories all to_s to_str] => :combined def initialize home: Paths::Home, directories: Paths::Directory, environment: ENV @combined = Paths::Combined.new home.new(HOME_PAIR, environment), directories.new(DIRS_PAIR, environment) freeze end def inspect = "#<#{self.class}:#{object_id} #{self}>" private attr_reader :combined end end bkuhlmann-xdg-c366e31/lib/xdg/environment.rb000066400000000000000000000015601517031704400210320ustar00rootroot00000000000000# frozen_string_literal: true module XDG # A convenience wrapper to all XDG functionality. class Environment def initialize home: Paths::Home, directories: Paths::Directory, environment: ENV @cache = Cache.new(home:, directories:, environment:) @config = Config.new(home:, directories:, environment:) @data = Data.new(home:, directories:, environment:) @state = State.new(home:, directories:, environment:) freeze end def cache_home = cache.home def config_home = config.home def config_dirs = config.directories def data_home = data.home def data_dirs = data.directories def state_home = state.home def to_s = "#{cache} #{config} #{data} #{state}" alias to_str to_s def inspect = "#<#{self.class}:#{object_id} #{self}>" private attr_reader :cache, :config, :data, :state end end bkuhlmann-xdg-c366e31/lib/xdg/pair.rb000066400000000000000000000010771517031704400174240ustar00rootroot00000000000000# frozen_string_literal: true module XDG # A generic key-value pair (KVP). Pair = Data.define :key, :value do def initialize key: nil, value: nil super end def key? = key.to_s.size.positive? def value? = value.to_s.size.positive? def empty? = !(key? && value?) def to_env = {key => value} def to_s = key? || value? ? "#{key}#{DELIMITER}#{value}" : "" alias_method :to_str, :to_s def inspect type = self.class key? || value? ? "#" : "#" end end end bkuhlmann-xdg-c366e31/lib/xdg/paths/000077500000000000000000000000001517031704400172565ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/lib/xdg/paths/combined.rb000066400000000000000000000012611517031704400213630ustar00rootroot00000000000000# frozen_string_literal: true module XDG module Paths # The combined home and directory paths. class Combined def initialize initial_home, initial_directories @initial_home = initial_home @initial_directories = initial_directories freeze end def home = initial_home.dynamic def directories = initial_directories.dynamic def all = directories.prepend(*home) def to_s = [initial_home.to_s, initial_directories.to_s].reject(&:empty?).join " " alias to_str to_s def inspect = "#<#{self.class}:#{object_id} #{self}>" private attr_reader :initial_home, :initial_directories end end end bkuhlmann-xdg-c366e31/lib/xdg/paths/directory.rb000066400000000000000000000020601517031704400216050ustar00rootroot00000000000000# frozen_string_literal: true 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 bkuhlmann-xdg-c366e31/lib/xdg/paths/home.rb000066400000000000000000000014361517031704400205370ustar00rootroot00000000000000# frozen_string_literal: true require "forwardable" module XDG module Paths # A XDG home path. class Home extend Forwardable KEY = "HOME" delegate %i[key value] => :pair def initialize pair, environment = ENV @pair = pair @environment = environment freeze end def default = expand String(value) def dynamic = String(environment[key]).then { |path| path.empty? ? default : expand(path) } def to_s = [pair.key, dynamic].compact.join XDG::DELIMITER alias to_str to_s def inspect = "#<#{self.class}:#{object_id} #{self}>" private attr_reader :pair, :environment def expand(path) = home.join(path).expand_path def home = Pathname environment.fetch(KEY) end end end bkuhlmann-xdg-c366e31/lib/xdg/state.rb000066400000000000000000000012021517031704400175770ustar00rootroot00000000000000# frozen_string_literal: true require "forwardable" require "xdg/pair" module XDG # Provides state support. class State extend Forwardable HOME_PAIR = Pair["XDG_STATE_HOME", ".local/state"].freeze delegate %i[home directories all to_s to_str] => :combined def initialize home: Paths::Home, directories: Paths::Directory, environment: ENV @combined = Paths::Combined.new home.new(HOME_PAIR, environment), directories.new(Pair.new, environment) freeze end def inspect = "#<#{self.class}:#{object_id} #{self}>" private attr_reader :combined end end bkuhlmann-xdg-c366e31/spec/000077500000000000000000000000001517031704400155415ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/spec/lib/000077500000000000000000000000001517031704400163075ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/spec/lib/xdg/000077500000000000000000000000001517031704400170715ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/spec/lib/xdg/cache_spec.rb000066400000000000000000000023141517031704400214730ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Cache do subject(:cache) { described_class.new environment: } let(:environment) { {"HOME" => "/home", **described_class::HOME_PAIR.to_env} } describe "#initialize" do it "is frozen" do expect(cache.frozen?).to be(true) end end describe "#home" do it "answers home directory" do expect(cache.home).to eq(Pathname("/home/.cache")) end end describe "#directories" do it "answers empty array" do expect(cache.directories).to eq([]) end end describe "#all" do it "answers all directories" do expect(cache.all).to contain_exactly(Pathname("/home/.cache")) end end shared_examples "a string" do |message| it "answers environment settings" do expect(cache.public_send(message)).to eq("XDG_CACHE_HOME=/home/.cache") end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do it "answers current environment" do expect(cache.inspect).to match( %r(\A\#<#{described_class}:\d+ XDG_CACHE_HOME=/home/.cache>\Z) ) end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/config_spec.rb000066400000000000000000000026011517031704400216740ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Config do subject(:config) { described_class.new environment: } let :environment do {"HOME" => "/home", **described_class::HOME_PAIR.to_env, **described_class::DIRS_PAIR.to_env} end describe "#initialize" do it "is frozen" do expect(config.frozen?).to be(true) end end describe "#home" do it "answers home directory" do expect(config.home).to eq(Pathname("/home/.config")) end end describe "#directories" do it "answers directories" do expect(config.directories).to contain_exactly(Pathname("/etc/xdg")) end end describe "#all" do it "answers all directories" do expect(config.all).to contain_exactly(Pathname("/home/.config"), Pathname("/etc/xdg")) end end shared_examples "a string" do |message| it "answers environment settings" do expect(config.public_send(message)).to eq( "XDG_CONFIG_HOME=/home/.config XDG_CONFIG_DIRS=/etc/xdg" ) end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do it "answers current environment" do expect(config.inspect).to match( %r(\A\#<#{described_class}:\d+\sXDG_CONFIG_HOME=/home/.config\sXDG_CONFIG_DIRS=/etc/xdg>\Z)x ) end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/data_spec.rb000066400000000000000000000031611517031704400213420ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Data do subject(:data) { described_class.new environment: } let :environment do {"HOME" => "/home", **described_class::HOME_PAIR.to_env, **described_class::DIRS_PAIR.to_env} end describe "#initialize" do it "is frozen" do expect(data.frozen?).to be(true) end end describe "#home" do it "answers home directory" do expect(data.home).to eq(Pathname("/home/.local/share")) end end describe "#directories" do it "answers directories" do expect(data.directories).to contain_exactly( Pathname("/usr/local/share"), Pathname("/usr/share") ) end end describe "#all" do it "answers all directories" do expect(data.all).to contain_exactly( Pathname("/home/.local/share"), Pathname("/usr/local/share"), Pathname("/usr/share") ) end end shared_examples "a string" do |message| it "answers environment settings" do expect(data.public_send(message)).to eq( "XDG_DATA_HOME=/home/.local/share XDG_DATA_DIRS=/usr/local/share:/usr/share" ) end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do it "answers current environment" do expect(data.inspect).to match( %r( \A \#< #{described_class}:\d+\s XDG_DATA_HOME=/home/.local/share\s XDG_DATA_DIRS=/usr/local/share:/usr/share > \Z )x ) end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/environment_spec.rb000066400000000000000000000043711517031704400230010ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Environment do subject(:environment) { described_class.new environment: {"HOME" => "/home"} } describe "#initialize" do it "is frozen" do expect(environment.frozen?).to be(true) end end describe "#cache_home" do it "answers cache home" do expect(environment.cache_home).to eq(Pathname("/home/.cache")) end end describe "config_home" do it "answers config home" do expect(environment.config_home).to eq(Pathname("/home/.config")) end end describe "config_dirs" do it "answers config dirs" do expect(environment.config_dirs).to contain_exactly(Pathname("/etc/xdg")) end end describe "data_home" do it "answers data home" do expect(environment.data_home).to eq(Pathname("/home/.local/share")) end end describe "data_dirs" do it "answers data dirs" do expect(environment.data_dirs).to contain_exactly( Pathname("/usr/local/share"), Pathname("/usr/share") ) end end describe "#state_home" do it "answers state home" do expect(environment.state_home).to eq(Pathname("/home/.local/state")) end end shared_examples "a string" do |message| it "answers current environment" do expect(environment.public_send(message)).to eq( "XDG_CACHE_HOME=/home/.cache " \ "XDG_CONFIG_HOME=/home/.config " \ "XDG_CONFIG_DIRS=/etc/xdg " \ "XDG_DATA_HOME=/home/.local/share " \ "XDG_DATA_DIRS=/usr/local/share:/usr/share " \ "XDG_STATE_HOME=/home/.local/state" ) end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do let :pattern do %r( \A \#< #{described_class}:\d+\s XDG_CACHE_HOME=/home/.cache\s XDG_CONFIG_HOME=/home/.config\s XDG_CONFIG_DIRS=/etc/xdg\s XDG_DATA_HOME=/home/.local/share\s XDG_DATA_DIRS=/usr/local/share:/usr/share\s XDG_STATE_HOME=/home/.local/state > \Z )x end it "answers current environment" do expect(environment.inspect).to match(pattern) end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/pair_spec.rb000066400000000000000000000054011517031704400213630ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Pair do subject(:pair) { described_class["TEST", "example"] } describe "#key" do it "answers key" do expect(pair.key).to eq("TEST") end end describe "#key?" do it "answers true when key is present" do expect(described_class["TEST"].key?).to be(true) end it "answers false when key is missing" do expect(described_class.new.key?).to be(false) end end describe "#value" do it "answers value" do expect(pair.value).to eq("example") end end describe "#value?" do it "answers true when value is present" do expect(described_class["TEST", "example"].value?).to be(true) end it "answers false when value is missing" do expect(described_class["TEST"].value?).to be(false) end end describe "#empty?" do it "answers false when key and value are present" do expect(described_class["TEST", "example"].empty?).to be(false) end it "answers true with key only" do expect(described_class["TEST"].empty?).to be(true) end it "answers true with value only" do expect(described_class[nil, "example"].empty?).to be(true) end it "answers true when both key and value are missing" do expect(described_class[].empty?).to be(true) end end describe "#to_env" do it "answers environment" do expect(pair.to_env).to eq("TEST" => "example") end end shared_examples "a string" do |message| it "answers key, delimiter, and value when both are present" do expect(pair.public_send(message)).to eq("TEST=example") end it "answers key and delimiter with key only" do expect(described_class["TEST"].public_send(message)).to eq("TEST=") end it "answers delimiter and value with value only" do expect(described_class[nil, "example"].public_send(message)).to eq("=example") end it "answers empty string with no key and value" do expect(described_class.new.public_send(message)).to eq("") end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do it "answers key, delimiter, and value when both are present" do expect(pair.inspect).to eq("#") end it "answers key and delimiter with key only" do expect(described_class["TEST"].inspect).to eq("#") end it "answers delimiter and value with value only" do expect(described_class[nil, "example"].inspect).to eq("#") end it "answers with no key and value" do expect(described_class.new.inspect).to eq("#") end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/paths/000077500000000000000000000000001517031704400202105ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/spec/lib/xdg/paths/combined_spec.rb000066400000000000000000000052611517031704400233330ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Paths::Combined do subject(:path) { described_class.new home, directories } let(:home) { XDG::Paths::Home.new XDG::Pair["TEST_HOME", "/one"], environment } let(:directories) { XDG::Paths::Directory.new XDG::Pair["TEST_DIRS", "/two:/three"], environment } let(:environment) { {"HOME" => "/home"} } describe "#initialize" do it "is frozen" do expect(path.frozen?).to be(true) end end describe "#home" do it "answers dynamic home" do expect(path.home).to eq(home.dynamic) end end describe "#directories" do it "answers dynamic directories" do expect(path.directories).to eq(directories.dynamic) end end describe "#all" do it "answers combined directories for unset environment" do expect(path.all).to contain_exactly(Pathname("/one"), Pathname("/two"), Pathname("/three")) end context "with set environment" do let(:home) { XDG::Paths::Home.new XDG::Pair["TEST_HOME", nil], environment } let(:directories) { XDG::Paths::Directory.new XDG::Pair["TEST_DIRS", nil], environment } let :environment do { "HOME" => "/home", "TEST_HOME" => "one", "TEST_DIRS" => "/two" } end it "answers combined directories" do expect(path.all).to contain_exactly(Pathname("/home/one"), Pathname("/two")) end end end shared_examples "a string" do |message| context "with home and directories pairs" do it "answers home and directories paths" do expect(path.public_send(message)).to eq("TEST_HOME=/one TEST_DIRS=/two:/three") end end context "with empty home and directories pairs" do let(:home) { XDG::Paths::Home.new XDG::Pair.new, environment } let(:directories) { XDG::Paths::Directory.new XDG::Pair.new, environment } it "answers path only" do expect(path.public_send(message)).to eq("/home") end end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do it "answers home and directory paths with home and directory pairs" do expect(path.inspect).to match( %r(\A\#<#{described_class}:\d+ TEST_HOME=/one TEST_DIRS=/two:/three>\Z) ) end context "with empty home and directories pairs" do let(:home) { XDG::Paths::Home.new XDG::Pair.new, environment } let(:directories) { XDG::Paths::Directory.new XDG::Pair.new, environment } it "answers path only" do expect(path.inspect).to match(%r(\A\#<#{described_class}:\d+ /home>\Z)) end end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/paths/directory_spec.rb000066400000000000000000000100261517031704400235520ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Paths::Directory do subject(:path) { described_class.new pair, environment } let(:home) { XDG::Pair["HOME", "/home"] } let(:environment) { home.to_env } describe "#initialize" do let(:pair) { XDG::Pair["TEST_DIRS", "/one"] } it "is frozen" do expect(path.frozen?).to be(true) end end describe "#default" do context "with single path" do let(:pair) { XDG::Pair["TEST_DIRS", "/one"] } it "answers array with one path" do expect(path.default).to contain_exactly(Pathname("/one")) end end context "with multiple paths" do let(:pair) { XDG::Pair["TEST_DIRS", "/one:/two:/three"] } it "answers array with multiple paths" do expect(path.default).to contain_exactly( Pathname("/one"), Pathname("/two"), Pathname("/three") ) end end context "with tilda value" do let(:pair) { XDG::Pair["TEST_DIRS", "~/test"] } it "answers expanded paths" do expect(path.default).to contain_exactly(Pathname(File.join(Dir.home, "test"))) end end context "with nil pair value" do let(:pair) { XDG::Pair["TEST_DIRS", nil] } it "answers empty array" do expect(path.default).to eq([]) end end context "with nil pair key and value" do let(:pair) { XDG::Pair[nil, nil] } it "answers empty array" do expect(path.default).to eq([]) end end end describe "#dynamic" do context "with pair unset and environment set" do let(:pair) { XDG::Pair["TEST_DIRS", nil] } let(:environment) { home.to_env.merge "TEST_DIRS" => "/one:/two" } it "answers environment paths" do expect(path.dynamic).to contain_exactly(Pathname("/one"), Pathname("/two")) end end context "with pair set and environment unset" do let(:pair) { XDG::Pair["TEST_DIRS", "/one:/two"] } let(:environment) { home.to_env.merge "TEST_DIRS" => nil } it "answers default paths" do expect(path.dynamic).to contain_exactly(Pathname("/one"), Pathname("/two")) end end context "with empty pair and environment" do let(:pair) { XDG::Pair.new } let(:environment) { {} } it "answers empty array" do expect(path.dynamic).to eq([]) end end context "with duplicate directories" do let(:pair) { XDG::Pair["TEST_DIRS", "/one:/two:/one"] } it "answers unique directories" do expect(path.dynamic).to contain_exactly(Pathname("/one"), Pathname("/two")) end end context "with nil pair value" do let(:pair) { XDG::Pair["TEST_DIRS", nil] } it "answers empty array" do expect(path.dynamic).to eq([]) end end context "with nil pair key and value" do let(:pair) { XDG::Pair[nil, nil] } it "answers empty array" do expect(path.dynamic).to eq([]) end end end shared_examples "a string" do |message| context "with custom pair" do let(:pair) { XDG::Pair["TEST_DIRS", "/one:/two:/three"] } it "answers custom pair" do expect(path.public_send(message)).to eq("TEST_DIRS=/one:/two:/three") end end context "with empty pair" do let(:pair) { XDG::Pair.new } it "answers empty string" do expect(path.public_send(message)).to eq("") end end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do context "with custom pair" do let(:pair) { XDG::Pair["TEST_DIRS", "/one:/two:/three"] } it "answers custom pair" do expect(path.inspect).to match( %r(\A\#<#{described_class}:\d+ TEST_DIRS=/one:/two:/three>\Z) ) end end context "with empty pair" do let(:pair) { XDG::Pair.new } it "answers no key or value" do expect(path.inspect).to match(%r(\A\#<#{described_class}:\d+>\Z)) end end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/paths/home_spec.rb000066400000000000000000000056401517031704400225040ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::Paths::Home do subject(:path) { described_class.new pair, environment } let(:pair) { XDG::Pair["TEST", "test"] } let(:home) { XDG::Pair["HOME", "/home"] } let(:environment) { home.to_env } describe "#initialize" do it "is frozen" do expect(path.frozen?).to be(true) end end describe "#default" do context "with custom environment" do let(:environment) { home.to_env.merge pair.to_env } it "answers path relatve to home" do expect(path.default).to eq(Pathname("/home/test")) end end context "without custom environment value" do let(:environment) { home.to_env.merge pair.key => nil } it "answers path relative to home" do expect(path.default).to eq(Pathname("/home/test")) end end context "without custom environment" do let(:environment) { home.to_env } it "answers path relative to home" do expect(path.default).to eq(Pathname("/home/test")) end end context "with missing pair value" do let(:pair) { XDG::Pair["TEST"] } it "answers home path" do expect(path.default).to eq(Pathname("/home")) end end context "with nil pair value" do let(:pair) { XDG::Pair["TEST", nil] } it "answers home path" do expect(path.default).to eq(Pathname("/home")) end end context "with empty pair" do let(:pair) { XDG::Pair.new } it "answers home path" do expect(path.default).to eq(Pathname("/home")) end end end describe "#dynamic" do context "with default value" do let(:environment) { home.to_env.merge pair.to_env } it "answers default path relative to home" do expect(path.dynamic).to eq(Pathname("/home/test")) end end context "without dynamic environment" do let(:environment) { home.to_env.merge pair.key => "dynamic" } it "answers dynamic path" do expect(path.dynamic).to eq(Pathname("/home/dynamic")) end end end shared_examples "a string" do |message| it "answers key and value with custom pair" do expect(path.public_send(message)).to eq("TEST=/home/test") end context "with empty pair" do let(:pair) { XDG::Pair.new } it "answers value only" do expect(path.public_send(message)).to eq("/home") end end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do it "answers key and value with custom pair" do expect(path.inspect).to match(%r(\A\#<#{described_class}:\d+ TEST=/home/test>\Z)) end context "with empty pair" do let(:pair) { XDG::Pair.new } it "answers value only" do expect(path.inspect).to match(%r(\A\#<#{described_class}:\d+ /home>\Z)) end end end end bkuhlmann-xdg-c366e31/spec/lib/xdg/state_spec.rb000066400000000000000000000023461517031704400215550ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG::State do subject(:state) { described_class.new environment: } let(:environment) { {"HOME" => "/home", **described_class::HOME_PAIR.to_env} } describe "#initialize" do it "is frozen" do expect(state.frozen?).to be(true) end end describe "#home" do it "answers home directory" do expect(state.home).to eq(Pathname("/home/.local/state")) end end describe "#directories" do it "answers empty array" do expect(state.directories).to eq([]) end end describe "#all" do it "answers all directories" do expect(state.all).to contain_exactly(Pathname("/home/.local/state")) end end shared_examples "a string" do |message| it "answers environment settings" do expect(state.public_send(message)).to eq("XDG_STATE_HOME=/home/.local/state") end end describe "#to_s" do it_behaves_like "a string", :to_s end describe "#to_str" do it_behaves_like "a string", :to_str end describe "#inspect" do it "answers current environment" do expect(state.inspect).to match( %r(\A\#<#{described_class}:\d+\sXDG_STATE_HOME=/home/.local/state>\Z)x ) end end end bkuhlmann-xdg-c366e31/spec/lib/xdg_spec.rb000066400000000000000000000003441517031704400204310ustar00rootroot00000000000000# frozen_string_literal: true require "spec_helper" RSpec.describe XDG do describe ".new" do it "answers environment instance" do expect(described_class.new).to be_a(described_class::Environment) end end end bkuhlmann-xdg-c366e31/spec/spec_helper.rb000066400000000000000000000023111517031704400203540ustar00rootroot00000000000000# frozen_string_literal: true require "simplecov" unless ENV["COVERAGE"] == "no" SimpleCov.start do add_filter %r(^/spec/) enable_coverage :branch enable_coverage_for_eval minimum_coverage_by_file line: 95, branch: 95 end end Bundler.require :tools require "refinements" require "xdg" SPEC_ROOT = Pathname(__dir__).realpath.freeze using Refinements::Pathname Pathname.require_tree SPEC_ROOT.join("support/shared_contexts") RSpec.configure do |config| config.color = true config.disable_monkey_patching! config.example_status_persistence_file_path = "./tmp/rspec-examples.txt" config.filter_run_when_matching :focus config.formatter = ENV.fetch("CI", false) == "true" ? :progress : :documentation config.order = :random config.pending_failure_output = :no_backtrace config.shared_context_metadata_behavior = :apply_to_host_groups config.warnings = true config.expect_with :rspec do |expectations| expectations.syntax = :expect expectations.include_chain_clauses_in_custom_matcher_descriptions = true end config.mock_with :rspec do |mocks| mocks.verify_doubled_constant_names = true mocks.verify_partial_doubles = true end Kernel.srand config.seed end bkuhlmann-xdg-c366e31/spec/support/000077500000000000000000000000001517031704400172555ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/spec/support/shared_contexts/000077500000000000000000000000001517031704400224525ustar00rootroot00000000000000bkuhlmann-xdg-c366e31/spec/support/shared_contexts/temp_dir.rb000066400000000000000000000004051517031704400246010ustar00rootroot00000000000000# frozen_string_literal: true RSpec.shared_context "with temporary directory" do using Refinements::Pathname let(:temp_dir) { Bundler.root.join "tmp/rspec" } around do |example| temp_dir.make_path example.run temp_dir.remove_tree end end bkuhlmann-xdg-c366e31/xdg.gemspec000066400000000000000000000017271517031704400167450ustar00rootroot00000000000000# frozen_string_literal: true Gem::Specification.new do |spec| spec.name = "xdg" spec.version = "10.2.0" spec.authors = ["Brooke Kuhlmann"] spec.email = ["brooke@alchemists.io"] spec.homepage = "https://alchemists.io/projects/xdg" spec.summary = "A XDG Base Directory Specification implementation." spec.license = "Hippocratic-2.1" spec.metadata = { "bug_tracker_uri" => "https://github.com/bkuhlmann/xdg/issues", "changelog_uri" => "https://alchemists.io/projects/xdg/versions", "homepage_uri" => "https://alchemists.io/projects/xdg", "funding_uri" => "https://github.com/sponsors/bkuhlmann", "label" => "XDG", "rubygems_mfa_required" => "true", "source_code_uri" => "https://github.com/bkuhlmann/xdg" } spec.signing_key = Gem.default_key_path spec.cert_chain = [Gem.default_cert_path] spec.required_ruby_version = ">= 4.0" spec.files = Dir["*.gemspec", "lib/**/*"] spec.extra_rdoc_files = Dir["README*", "LICENSE*"] end