Class: RuboCop::Cop::Utils::FormatString::FormatSequence
- Inherits:
-
Object
- Object
- RuboCop::Cop::Utils::FormatString::FormatSequence
- 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
-
#arg_number ⇒ Object
readonly
Returns the value of attribute arg_number.
-
#begin_pos ⇒ Object
readonly
Returns the value of attribute begin_pos.
-
#end_pos ⇒ Object
readonly
Returns the value of attribute end_pos.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #annotated? ⇒ Boolean
-
#arity ⇒ Object
Number of arguments required for the format sequence.
-
#initialize(match) ⇒ FormatSequence
constructor
A new instance of FormatSequence.
- #max_digit_dollar_num ⇒ Object
- #percent? ⇒ Boolean
- #style ⇒ Object
- #template? ⇒ Boolean
- #variable_width? ⇒ Boolean
- #variable_width_argument_number ⇒ Object
Constructor Details
#initialize(match) ⇒ FormatSequence
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 50 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] @arg_number = match[:arg_number] end |
Instance Attribute Details
#arg_number ⇒ Object (readonly)
Returns the value of attribute arg_number.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def arg_number @arg_number end |
#begin_pos ⇒ Object (readonly)
Returns the value of attribute begin_pos.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def begin_pos @begin_pos end |
#end_pos ⇒ Object (readonly)
Returns the value of attribute end_pos.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def end_pos @end_pos end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def flags @flags end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def name @name end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def precision @precision end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def type @type end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
48 49 50 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 48 def width @width end |
Instance Method Details
#annotated? ⇒ Boolean
66 67 68 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 66 def annotated? name && @source.include?('<') end |
#arity ⇒ Object
Number of arguments required for the format sequence
85 86 87 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 85 def arity @source.scan('*').count + 1 end |
#max_digit_dollar_num ⇒ Object
89 90 91 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 89 def max_digit_dollar_num @source.scan(DIGIT_DOLLAR).map { |(digit_dollar_num)| digit_dollar_num.to_i }.max end |
#percent? ⇒ Boolean
62 63 64 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 62 def percent? type == '%' end |
#style ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 93 def style if annotated? :annotated elsif template? :template else :unannotated end end |
#template? ⇒ Boolean
70 71 72 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 70 def template? name && @source.include?('{') end |
#variable_width? ⇒ Boolean
74 75 76 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 74 def variable_width? !!width&.start_with?('*') end |
#variable_width_argument_number ⇒ Object
78 79 80 81 82 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 78 def variable_width_argument_number return unless variable_width? width == '*' ? 1 : width.match(DIGIT_DOLLAR)['arg_number'].to_i end |