module NRSER::Sys::Env
Tools for dealing with the system (POSIX-like) environment.
Constants
- COMMON_EXPORT_NAMES
Common and/or important ENV var names that we don't really want to end up auto-generating.
@see pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html @see archive.is/fmBRH
@return [Set<String>]
- VAR_NAME_RE
Regular expression mathcing strict and portable ENV var name rules: only `A-Z`, `0-9` and `_`; can not start with digit.
@return [RegExp]
Public Class Methods
Is ENV var name?
Must be {String} and match {VAR_NAME_RE}.
@param [Object] name
The name. No chance of `true` unless it's a {String}.
@return [Boolean]
`true` if it passes our mustard.
# File lib/nrser/sys/env.rb, line 147 def self.var_name? name String === name && VAR_NAME_RE =~ name end
Attempt to munge any string into a decent-looking and legal ENV var name.
We follow a strict, very portable (should be find in `sh`) guideline:
> Environment variable names […] consist solely of uppercase letters, > digits, and the '_' (underscore) […] and do not begin with a digit.
@see pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html @see archive.is/fmBRH
@param [String] string
Take a guess.
@return [nil]
If we didn't end up with a legal ENV var name.
@return [String]
If we were able to munge a legal ENV var name.
# File lib/nrser/sys/env.rb, line 171 def self.varize string, prohibit_common_exports: false candidate = string. # 1. Use {ActiveSupport}'s {String#underscore} to produce a nicely # underscore-separated name for common strings like class names. underscore. # 2. Convert lower-case letters to upper case. upcase. # 3. Smush all contiguous runs of anything not `A-Z0-9` into a `_`. gsub( /[^A-Z0-9]+/, '_' ). # 4. Bite off any leading digits. sub( /\A\d+/, '' ). # 5. Chomp off any trailing `_`. Just for good looks :) chomp( '_' ) # Return `nil` if we didn't get a legal ENV var name return nil unless var_name?( candidate ) # If `prohibit_common_exports` is `true` and the name we made is in # that list then return `nil`. if prohibit_common_exports && COMMON_EXPORT_NAMES.include?( candidate ) return nil end # If we got here, we're good! return candidate end