Module: Gapic::FormattingUtils

Defined in:
lib/gapic/formatting_utils.rb

Overview

Various string formatting utils

Class Method Summary collapse

Class Method Details

.format_doc_lines(api, lines, disable_xrefs: false, transport: nil) ⇒ Enumerable<String>

Given an enumerable of lines, performs yardoc formatting, including:

  • Interpreting cross-references identified as described in AIP 192
  • Escaping literal braces that look like yardoc type links

Tries to be smart about exempting preformatted text blocks.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gapic/formatting_utils.rb', line 46

def format_doc_lines api, lines, disable_xrefs: false, transport: nil
  transport ||= api&.default_transport || :grpc
  # To detect preformatted blocks, this tracks the "expected" base indent
  # according to Markdown. Specifically, this is the effective indent of
  # previous block, which is normally 0 except if we're in a list item.
  # Then, if a block is indented at least 4 spaces past that expected
  # indent (and as long as it remains so), those lines are considered
  # preformatted.
  in_block = nil
  base_indent = 0
  (lines - @omit_lines).map do |line|
    indent = line_indent line
    if indent.nil?
      in_block = nil
    else
      in_block, base_indent = update_indent_state in_block, base_indent, line, indent
      if in_block == false
        line = escape_line_braces line
        line = format_line_xrefs api, line, disable_xrefs, transport
      end
    end
    line
  end
end

.format_number(value) ⇒ String

Given a number, format it in such a way that Rubocop will be happy. Specifically, we add underscores if the magnitude is at least 10_000. This works for both integers and floats.



79
80
81
82
83
84
85
86
87
# File 'lib/gapic/formatting_utils.rb', line 79

def format_number value
  return value.to_s if value.abs < 10_000
  str = value.is_a?(Integer) ? value.to_s : BigDecimal(value.to_f.to_s).to_s("F")
  re = /^(-?\d+)(\d\d\d)([_.][_.\d]+)?$/
  while (m = re.match str)
    str = "#{m[1]}_#{m[2]}#{m[3]}"
  end
  str
end