Module: Pwsh::Util
- Defined in:
- lib/pwsh/util.rb
Overview
Various helper methods
Class Method Summary collapse
-
.custom_powershell_property(name, expression) ⇒ String
Return the representative string of a PowerShell hash for a custom object property to be used in selecting or filtering.
-
.escape_quotes(text) ⇒ String
Ensure that quotes inside a passed string will continue to be passed.
-
.format_powershell_value(object) ⇒ String
Convert a ruby value into a string to be passed along to PowerShell for interpolation in a command Handles: - Strings - Numbers - Booleans - Symbols - Arrays - Hashes.
-
.invalid_directories?(path_collection) ⇒ Bool
Verify paths specified are valid directories.
-
.on_windows? ⇒ Bool
Verifies whether or not the current context is running on a Windows node.
-
.pascal_case(object) ⇒ String
Return a string or symbol converted to PascalCase.
-
.pascal_case_hash_keys(object) ⇒ Hash
Iterate through a hashes keys, PascalCasing them.
-
.snake_case(object) ⇒ String
Return a string or symbol converted to snake_case.
-
.snake_case_hash_keys(object) ⇒ Hash
Iterate through a hashes keys, snake_casing them.
-
.symbolize_hash_keys(object) ⇒ Hash
Ensure that all keys in a hash are symbols, not strings.
Class Method Details
.custom_powershell_property(name, expression) ⇒ String
Return the representative string of a PowerShell hash for a custom object property to be used in selecting or filtering. The script block for the expression must be passed as the string you want interpolated into the hash; this method does not do any of the additional work of interpolation for you as the type sits inside a code block inside a hash.
136 137 138 |
# File 'lib/pwsh/util.rb', line 136 def custom_powershell_property(name, expression) "@{Name = '#{name}'; Expression = {#{expression}}}" end |
.escape_quotes(text) ⇒ String
Ensure that quotes inside a passed string will continue to be passed
80 81 82 |
# File 'lib/pwsh/util.rb', line 80 def escape_quotes(text) text.gsub("'", "''") end |
.format_powershell_value(object) ⇒ String
Convert a ruby value into a string to be passed along to PowerShell for interpolation in a command Handles:
-
Strings
-
Numbers
-
Booleans
-
Symbols
-
Arrays
-
Hashes
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/pwsh/util.rb', line 115 def format_powershell_value(object) if %i[true false].include?(object) || %w[trueclass falseclass].include?(object.class.name.downcase) "$#{object}" elsif object.instance_of?(Symbol) || object.class.ancestors.include?(Numeric) object.to_s elsif object.instance_of?(String) "'#{escape_quotes(object)}'" elsif object.instance_of?(Array) "@(#{object.collect { |item| format_powershell_value(item) }.join(', ')})" elsif object.instance_of?(Hash) "@{#{object.collect { |k, v| "#{format_powershell_value(k)} = #{format_powershell_value(v)}" }.join('; ')}}" else raise "unsupported type #{object.class} of value '#{object}'" end end |
.invalid_directories?(path_collection) ⇒ Bool
Verify paths specified are valid directories. Skips paths which do not exist.
21 22 23 24 25 26 27 28 |
# File 'lib/pwsh/util.rb', line 21 def invalid_directories?(path_collection) return false if path_collection.nil? || path_collection.empty? delimiter = on_windows? ? ';' : ':' paths = path_collection.split(delimiter) paths.any? { |path| !path.empty? && File.exist?(path) && !File.directory?(path) } end |
.on_windows? ⇒ Bool
Verifies whether or not the current context is running on a Windows node. Implementation copied from ‘facets`: github.com/rubyworks/facets/blob/main/lib/standard/facets/rbconfig.rb
13 14 15 16 |
# File 'lib/pwsh/util.rb', line 13 def on_windows? host_os = RbConfig::CONFIG['host_os'] !!(host_os =~ /mswin|mingw/) end |
.pascal_case(object) ⇒ String
Return a string or symbol converted to PascalCase
60 61 62 63 64 65 66 67 |
# File 'lib/pwsh/util.rb', line 60 def pascal_case(object) should_symbolize = object.is_a?(Symbol) raise "snake_case method only handles strings and symbols, passed a #{object.class}: #{object}" unless should_symbolize || object.is_a?(String) # Break word boundaries to snake case first text = snake_case(object.to_s).split('_').collect(&:capitalize).join should_symbolize ? text.to_sym : text end |
.pascal_case_hash_keys(object) ⇒ Hash
Iterate through a hashes keys, PascalCasing them
72 73 74 75 |
# File 'lib/pwsh/util.rb', line 72 def pascal_case_hash_keys(object) pascal_case_proc = proc { |key| pascal_case(key) } apply_key_mutator(object, pascal_case_proc) end |
.snake_case(object) ⇒ String
Return a string or symbol converted to snake_case
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pwsh/util.rb', line 33 def snake_case(object) # Implementation copied from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb # gsub(/::/, '/'). should_symbolize = object.is_a?(Symbol) raise "snake_case method only handles strings and symbols, passed a #{object.class}: #{object}" unless should_symbolize || object.is_a?(String) text = object.to_s .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .gsub(/\s/, '_') .gsub(/__+/, '_') .downcase should_symbolize ? text.to_sym : text end |
.snake_case_hash_keys(object) ⇒ Hash
Iterate through a hashes keys, snake_casing them
52 53 54 55 |
# File 'lib/pwsh/util.rb', line 52 def snake_case_hash_keys(object) snake_case_proc = proc { |key| snake_case(key) } apply_key_mutator(object, snake_case_proc) end |
.symbolize_hash_keys(object) ⇒ Hash
Ensure that all keys in a hash are symbols, not strings.
87 88 89 90 |
# File 'lib/pwsh/util.rb', line 87 def symbolize_hash_keys(object) symbolize_proc = proc(&:to_sym) apply_key_mutator(object, symbolize_proc) end |