123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Utils::FormatString::FormatSequence

Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/cop/utils/format_string.rb

Overview

The syntax of a format sequence is as follows.

%[flags][width][.precision]type

A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character.

For more complex formatting, Ruby supports a reference by name.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(match) ⇒ FormatSequence

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 46

def initialize(match)
  @source = match[0]
  @begin_pos = match.begin(0)
  @end_pos = match.end(0)
  @flags = match[:flags].to_s + match[:more_flags].to_s
  @width = match[:width]
  @precision = match[:precision]
  @name = match[:name]
  @type = match[:type]
end

Instance Attribute Details

#annotated?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 61

def annotated?
  name && @source.include?('<')
end

#begin_pos (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 44

attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type

#end_pos (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 44

attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type

#flags (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 44

attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type

#name (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 44

attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type

#percent?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 57

def percent?
  type == '%'
end

#precision (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 44

attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type

#template?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 65

def template?
  name && @source.include?('{')
end

#type (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 44

attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type

#width (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 44

attr_reader :begin_pos, :end_pos, :flags, :width, :precision, :name, :type

Instance Method Details

#arity

Number of arguments required for the format sequence

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 70

def arity
  @source.scan('*').count + 1
end

#max_digit_dollar_num

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 74

def max_digit_dollar_num
  @source.scan(DIGIT_DOLLAR).map { |(digit_dollar_num)| digit_dollar_num.to_i }.max
end

#style

[ GitHub ]

  
# File 'lib/rubocop/cop/utils/format_string.rb', line 78

def style
  if annotated?
    :annotated
  elsif template?
    :template
  else
    :unannotated
  end
end