Module: CLI::Kit::Util

Defined in:
lib/cli/kit/util.rb

Class Method Summary collapse

Class Method Details

.begin(&block_that_might_raise) ⇒ Object

Must call retry_after on the result in order to execute the block

Example usage:

CLI::Kit::Util.begin do

might_raise_if_costly_prep_not_done()

end.retry_after(ExpectedError) do

costly_prep()

end



161
162
163
# File 'lib/cli/kit/util.rb', line 161

def begin(&block_that_might_raise)
  Retrier.new(block_that_might_raise)
end

.ci?Boolean

Standard way of checking for CI

Returns:

  • (Boolean)


148
149
150
# File 'lib/cli/kit/util.rb', line 148

def ci?
  ENV['CI']
end

.dash_case(camel_case) ⇒ Object



13
14
15
# File 'lib/cli/kit/util.rb', line 13

def dash_case(camel_case)
  snake_case(camel_case, '-')
end

.english_join(array) ⇒ Object

Joins an array with commas and “and”, using the Oxford comma.



50
51
52
53
54
55
# File 'lib/cli/kit/util.rb', line 50

def english_join(array)
  return '' if array.nil?
  return array.join(' and ') if array.length < 3

  "#{array[0..-2].join(", ")}, and #{array[-1]}"
end

.integration_test_session?Boolean

Set only in IntegrationTest#session; indicates that the process was called by ‘session.execute` from an IntegrationTest subclass.

Returns:

  • (Boolean)


143
144
145
# File 'lib/cli/kit/util.rb', line 143

def integration_test_session?
  ENV['INTEGRATION_TEST_SESSION']
end

.snake_case(camel_case, seperator = '_') ⇒ Object



5
6
7
8
9
10
11
# File 'lib/cli/kit/util.rb', line 5

def snake_case(camel_case, seperator = '_')
  camel_case.to_s # MyCoolThing::MyAPIModule
    .gsub(/::/, '/') # MyCoolThing/MyAPIModule
    .gsub(/([A-Z]+)([A-Z][a-z])/, "\\1#{seperator}\\2") # MyCoolThing::MyAPI_Module
    .gsub(/([a-z\d])([A-Z])/, "\\1#{seperator}\\2") # My_Cool_Thing::My_API_Module
    .downcase # my_cool_thing/my_api_module
end

.strip_heredoc(str) ⇒ Object

The following methods is taken from activesupport All credit for this method goes to the original authors. github.com/rails/rails/blob/d66e7835bea9505f7003e5038aa19b6ea95ceea1/activesupport/lib/active_support/core_ext/string/strip.rb

Copyright © 2005-2018 David Heinemeier Hansson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Strips indentation by removing the amount of leading whitespace in the least indented non-empty line in the whole string



45
46
47
# File 'lib/cli/kit/util.rb', line 45

def strip_heredoc(str)
  str.gsub(/^#{str.scan(/^[ \t]*(?=\S)/).min}/, ''.freeze)
end

.testing?Boolean

Standard way of checking for CI / Tests

Returns:

  • (Boolean)


137
138
139
# File 'lib/cli/kit/util.rb', line 137

def testing?
  ci? || ENV['TEST']
end

.to_filesize(bytes, precision: 2, space: false) ⇒ Object

Converts an integer representing bytes into a human readable format



73
74
75
# File 'lib/cli/kit/util.rb', line 73

def to_filesize(bytes, precision: 2, space: false)
  to_si_scale(bytes, 'B', precision: precision, space: space, factor: 1024)
end

.to_si_scale(number, unit = '', factor: 1000, precision: 2, space: false) ⇒ Object

Converts a number to a human readable format on the SI scale

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cli/kit/util.rb', line 79

def to_si_scale(number, unit = '', factor: 1000, precision: 2, space: false)
  raise ArgumentError, 'factor should only be 1000 or 1024' unless [1000, 1024].include?(factor)

  small_scale = ['m', 'µ', 'n', 'p', 'f', 'a', 'z', 'y']
  big_scale = ['k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
  negative = number < 0
  number = number.abs.to_f

  if number == 0 || number.between?(1, factor)
    prefix = ''
    scale = 0
  else
    scale = Math.log(number, factor).floor
    if number < 1
      index = [-scale - 1, small_scale.length].min
      scale = -(index + 1)
      prefix = small_scale[index]
    else
      index = [scale - 1, big_scale.length].min
      scale = index + 1
      prefix = big_scale[index]
    end
  end

  divider = (factor**scale)
  fnum = (number / divider).round(precision)

  # Trim useless decimal
  fnum = fnum.to_i if (fnum.to_i.to_f * divider) == number

  fnum = -fnum if negative
  prefix = ' ' + prefix if space

  "#{fnum}#{prefix}#{unit}"
end

.with_dir(dir) ⇒ Object

Dir.chdir, when invoked in block form, complains when we call chdir again recursively. There’s no apparent good reason for this, so we simply implement our own block form of Dir.chdir here.



118
119
120
121
122
123
124
# File 'lib/cli/kit/util.rb', line 118

def with_dir(dir)
  prev = Dir.pwd
  Dir.chdir(dir)
  yield
ensure
  Dir.chdir(prev)
end

.with_environment(environment, value) ⇒ Object

Execute a block within the context of a variable enviroment



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cli/kit/util.rb', line 59

def with_environment(environment, value)
  return yield unless environment

  old_env = ENV[environment]
  begin
    ENV[environment] = value
    yield
  ensure
    old_env ? ENV[environment] = old_env : ENV.delete(environment)
  end
end

.with_tmp_dirObject



126
127
128
129
130
131
132
133
134
# File 'lib/cli/kit/util.rb', line 126

def with_tmp_dir
  require 'fileutils'
  dir = Dir.mktmpdir
  with_dir(dir) do
    yield(dir)
  end
ensure
  FileUtils.remove_entry(dir)
end