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.

Parameters:

  • api (Gapic::Schema::Api)
  • lines (Enumerable<String>)
  • disable_xrefs (Boolean) (defaults to: false)

    (default is false) Disable linking to cross-references, and render them simply as text. This can be used if it is known that the targets are not present in the current library.

  • transport (:grpc, :rest) (defaults to: nil)

    Whether xref links should go to REST or gRPC client classes. Uses the default transport if not provided.

Returns:

  • (Enumerable<String>)


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.

Parameters:

  • value (Numeric)

Returns:

  • (String)


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