Module: NRSER::Sys::Env
- Defined in:
- lib/nrser/sys/env.rb
Overview
Tools for dealing with the system (POSIX-like) environment.
Defined Under Namespace
Classes: Path
Constant Summary collapse
- COMMON_EXPORT_NAMES =
Common and/or important ENV var names that we don’t really want to end up auto-generating.
Set[ "ARFLAGS", "CC", "CDPATH", "CFLAGS", "CHARSET", "COLUMNS", "DATEMSK", "DEAD", "EDITOR", "ENV", "EXINIT", "FC", "FCEDIT", "FFLAGS", "GET", "GFLAGS", "HISTFILE", "HISTORY", "HISTSIZE", "HOME", "IFS", "LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LDFLAGS", "LEX", "LFLAGS", "LINENO", "LINES", "LISTER", "LOGNAME", "LPDEST", "MAIL", "MAILCHECK", "MAILER", "MAILPATH", "MAILRC", "MAKEFLAGS", "MAKESHELL", "MANPATH", "MBOX", "MORE", "MSGVERB", "NLSPATH", "NPROC", "OLDPWD", "OPTARG", "OPTERR", "OPTIND", "PAGER", "PATH", "PPID", "PRINTER", "PROCLANG", "PROJECTDIR", "PS1", "PS2", "PS3", "PS4", "PWD", "RANDOM", "SECONDS", "SHELL", "TERM", "TERMCAP", "TERMINFO", "TMPDIR", "TZ", "USER", "VISUAL", "YACC", "YFLAGS", ].freeze
- VAR_NAME_RE =
Regular expression mathcing strict and portable ENV var name rules: only ‘A-Z`, `0-9` and `_`; can not start with digit.
/\A[A-Z_][A-Z0-9_]+\z/
Class Method Summary collapse
-
.var_name?(name) ⇒ Boolean
Is ENV var name?.
-
.varize(string, prohibit_common_exports: false) ⇒ nil, String
Attempt to munge any string into a decent-looking and legal ENV var name.
Class Method Details
.var_name?(name) ⇒ Boolean
Is ENV var name?
Must be String and match VAR_NAME_RE.
147 148 149 |
# File 'lib/nrser/sys/env.rb', line 147 def self.var_name? name String === name && VAR_NAME_RE =~ name end |
.varize(string, prohibit_common_exports: false) ⇒ nil, String
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.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# 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 |