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
Constructor Details
permalink #initialize(match) ⇒ FormatSequence
Returns a new instance of FormatSequence.
47 48 49 50 51 52 53 54 55 56 57 |
# 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] @arg_number = match[:arg_number] end |
Instance Attribute Details
permalink #arg_number ⇒ Object (readonly)
Returns the value of attribute arg_number.
45 46 47 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 45 def arg_number @arg_number end |
permalink #begin_pos ⇒ Object (readonly)
Returns the value of attribute begin_pos.
45 46 47 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 45 def begin_pos @begin_pos end |
permalink #end_pos ⇒ Object (readonly)
Returns the value of attribute end_pos.
45 46 47 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 45 def end_pos @end_pos end |
permalink #flags ⇒ Object (readonly)
Returns the value of attribute flags.
45 46 47 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 45 def flags @flags end |
permalink #name ⇒ Object (readonly)
Returns the value of attribute name.
45 46 47 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 45 def name @name end |
permalink #precision ⇒ Object (readonly)
Returns the value of attribute precision.
45 46 47 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 45 def precision @precision end |
permalink #type ⇒ Object (readonly)
Returns the value of attribute type.
45 46 47 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 45 def type @type end |
permalink #width ⇒ Object (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
permalink #annotated? ⇒ Boolean
63 64 65 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 63 def annotated? name && @source.include?('<') end |
permalink #arity ⇒ Object
Number of arguments required for the format sequence
72 73 74 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 72 def arity @source.scan('*').count + 1 end |
permalink #max_digit_dollar_num ⇒ Object
[View source]
76 77 78 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 76 def max_digit_dollar_num @source.scan(DIGIT_DOLLAR).map { |(digit_dollar_num)| digit_dollar_num.to_i }.max end |
permalink #percent? ⇒ Boolean
59 60 61 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 59 def percent? type == '%' end |
permalink #style ⇒ Object
[View source]
80 81 82 83 84 85 86 87 88 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 80 def style if annotated? :annotated elsif template? :template else :unannotated end end |
permalink #template? ⇒ Boolean
67 68 69 |
# File 'lib/rubocop/cop/utils/format_string.rb', line 67 def template? name && @source.include?('{') end |