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

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/utils/format_string.rb

Overview

The syntax of a format sequence is as follows.

“‘ %[flags][.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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match) ⇒ FormatSequence

Returns a new instance of FormatSequence.



47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/utils/format_string.rb', line 47

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

#begin_posObject (readonly)

Returns the value of attribute begin_pos.



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

def begin_pos
  @begin_pos
end

#end_posObject (readonly)

Returns the value of attribute end_pos.



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

def end_pos
  @end_pos
end

#flagsObject (readonly)

Returns the value of attribute flags.



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

def flags
  @flags
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#precisionObject (readonly)

Returns the value of attribute precision.



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

def precision
  @precision
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#annotated?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rubocop/cop/utils/format_string.rb', line 62

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

#arityObject

Number of arguments required for the format sequence



71
72
73
# File 'lib/rubocop/cop/utils/format_string.rb', line 71

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

#max_digit_dollar_numObject



75
76
77
78
79
# File 'lib/rubocop/cop/utils/format_string.rb', line 75

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

#percent?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rubocop/cop/utils/format_string.rb', line 58

def percent?
  type == '%'
end

#styleObject



81
82
83
84
85
86
87
88
89
# File 'lib/rubocop/cop/utils/format_string.rb', line 81

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

#template?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rubocop/cop/utils/format_string.rb', line 66

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