Module: Kennel::Utils
- Defined in:
- lib/kennel/utils.rb
Defined Under Namespace
Classes: TeeIO
Constant Summary collapse
- COLORS =
{ red: 31, green: 32, yellow: 33, cyan: 36, magenta: 35, default: 0 }.freeze
Class Method Summary collapse
- .all_keys(items) ⇒ Object
- .ask(question) ⇒ Object
- .capture_sh(command) ⇒ Object
- .capture_stderr ⇒ Object
- .capture_stdout ⇒ Object
- .color(color, text) ⇒ Object
- .natural_order(name) ⇒ Object
- .parallel(items, max: 10) ⇒ Object
-
.parameterize(string) ⇒ Object
simplified version of apidock.com/rails/ActiveSupport/Inflector/parameterize.
- .path_to_url(path) ⇒ Object
- .presence(value) ⇒ Object
-
.pretty_inspect(object) ⇒ Object
TODO: use awesome-print or similar, but it has too many monkey-patches github.com/amazing-print/amazing_print/issues/36.
- .retry(*errors, times:) ⇒ Object
- .snake_case(string) ⇒ Object
- .strip_shell_control(text) ⇒ Object
- .tee_output ⇒ Object
Class Method Details
.all_keys(items) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/kennel/utils.rb', line 138 def all_keys(items) case items when Hash then items.keys + items.values.flat_map { |v| all_keys(v) } when Array then items.flat_map { |i| all_keys(i) } else [] end end |
.ask(question) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/kennel/utils.rb', line 38 def ask(question) Kennel.err.printf color(:red, "#{question} - press 'y' to continue: ") begin STDIN.gets.chomp == "y" rescue Interrupt # do not show a backtrace if user decides to Ctrl+C here Kennel.err.print "\n" exit 1 end end |
.capture_sh(command) ⇒ Object
87 88 89 90 91 |
# File 'lib/kennel/utils.rb', line 87 def capture_sh(command) result = `#{command} 2>&1` raise "Command failed:\n#{command}\n#{result}" unless $CHILD_STATUS.success? result end |
.capture_stderr ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/kennel/utils.rb', line 65 def capture_stderr old = Kennel.err Kennel.err = StringIO.new yield Kennel.err.string ensure Kennel.err = old end |
.capture_stdout ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/kennel/utils.rb', line 56 def capture_stdout old = Kennel.out Kennel.out = StringIO.new yield Kennel.out.string ensure Kennel.out = old end |
.color(color, text) ⇒ Object
48 49 50 |
# File 'lib/kennel/utils.rb', line 48 def color(color, text) "\e[#{COLORS.fetch(color)}m#{text}\e[0m" end |
.natural_order(name) ⇒ Object
124 125 126 |
# File 'lib/kennel/utils.rb', line 124 def natural_order(name) name.split(/(\d+)/).each_with_index.map { |x, i| i.odd? ? x.to_i : x } end |
.parallel(items, max: 10) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/kennel/utils.rb', line 101 def parallel(items, max: 10) threads = [items.size, max].min work = items.each_with_index.to_a done = Array.new(items.size) workers = Array.new(threads).map do Thread.new do loop do item, i = work.pop break unless i done[i] = begin yield item rescue StandardError => e work.clear e end end end end workers.each(&:join) done.each { |d| raise d if d.is_a?(StandardError) } end |
.parameterize(string) ⇒ Object
simplified version of apidock.com/rails/ActiveSupport/Inflector/parameterize
26 27 28 29 30 31 32 |
# File 'lib/kennel/utils.rb', line 26 def parameterize(string) string .downcase .gsub(/[^a-z0-9\-_]+/, "-") # remove unsupported .gsub(/-{2,}/, "-") # remove duplicates .gsub(/^-|-$/, "") # remove leading/trailing end |
.path_to_url(path) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/kennel/utils.rb', line 93 def path_to_url(path) if subdomain = ENV["DATADOG_SUBDOMAIN"] "https://#{subdomain}.datadoghq.com#{path}" else path end end |
.presence(value) ⇒ Object
34 35 36 |
# File 'lib/kennel/utils.rb', line 34 def presence(value) value.nil? || value.empty? ? nil : value end |
.pretty_inspect(object) ⇒ Object
TODO: use awesome-print or similar, but it has too many monkey-patches github.com/amazing-print/amazing_print/issues/36
148 149 150 151 152 153 154 155 |
# File 'lib/kennel/utils.rb', line 148 def pretty_inspect(object) string = object.inspect string.gsub!(/:([a-z_]+)=>/, "\\1: ") 10.times do string.gsub!(/{(\S.*?\S)}/, "{ \\1 }") || break end string end |
.retry(*errors, times:) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/kennel/utils.rb', line 128 def retry(*errors, times:) yield rescue *errors => e times -= 1 raise if times < 0 Kennel.err.puts "Error #{e}, #{times} retries left" retry end |
.snake_case(string) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/kennel/utils.rb', line 17 def snake_case(string) string .gsub(/::/, "_") # Foo::Bar -> foo_bar .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') # FOOBar -> foo_bar .gsub(/([a-z\d])([A-Z])/, '\1_\2') # fooBar -> foo_bar .downcase end |
.strip_shell_control(text) ⇒ Object
52 53 54 |
# File 'lib/kennel/utils.rb', line 52 def strip_shell_control(text) text.gsub(/\e\[\d+m(.*?)\e\[0m/, "\\1").gsub(/.#{Regexp.escape("\b")}/, "") end |
.tee_output ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/kennel/utils.rb', line 74 def tee_output old_stdout = Kennel.out old_stderr = Kennel.err capture = StringIO.new Kennel.out = TeeIO.new([capture, Kennel.out]) Kennel.err = TeeIO.new([capture, Kennel.err]) yield capture.string ensure Kennel.out = old_stdout Kennel.err = old_stderr end |