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
  • Backticking unknown doc tags so they are not parsed as YARD tags

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>)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gapic/formatting_utils.rb', line 60

def format_doc_lines api, lines, disable_xrefs: false, transport: nil
  transport ||= api&.default_transport || :grpc
  lines = rejoin_split_urls lines
  in_fence = in_code_span = false
  in_block = nil
  base_indent = 0
  (lines - @omit_lines).map do |line|
    if line =~ /^\s*(?:```|~~~)/
      in_fence = !in_fence
      in_code_span = false
      in_block = nil
    elsif !in_fence
      indent = line_indent line
      if indent.nil?
        in_block = nil
        in_code_span = false
      else
        in_block, base_indent = update_indent_state in_block, base_indent, line, indent
        if in_block == false
          line, in_code_span = format_line_content line, in_code_span
          line = format_line_xrefs api, line, disable_xrefs, transport
        end
      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)


96
97
98
99
100
101
102
103
104
# File 'lib/gapic/formatting_utils.rb', line 96

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