I'm parsing a file made up of various sections. I have a current_section
variable that tracks which portion of the file I'm currently processing and the following check_for_next_section
method that returns either the next section based on the contents of the current line or current_section
if there are no matches based on the contents of the current line.
This method is getting flagged by rubocop for both 'perceived complexity' and 'cyclomatic complexity'. How can I reduce these?
SECTION_HEADERS = {
hawaii_1: '"[2] For Hawaii, the following Postal Codes are Zone 44 for Ground',
hawaii_2: '"For Hawaii, the following Postal Codes are Zone 46 for Ground',
alaska_1: '"[3] For Alaska, the following Postal Codes are Zone 44 for Ground',
alaska_2: '"For Alaska, the following Postal Codes are Zone 46 for Ground'
}.freeze
def check_for_next_section(line, current_section)
return :zones if current_section == :preamble && line.starts_with?('Dest. ZIP')
return :hawaii_1 if current_section == :zones && !line.blank? && line.starts_with?(SECTION_HEADERS[:hawaii_1])
return :hawaii_2 if current_section == :hawaii_1 && line.starts_with?(SECTION_HEADERS[:hawaii_2])
return :alaska_1 if current_section == :hawaii_2 && line.starts_with?(SECTION_HEADERS[:alaska_1])
return :alaska_2 if current_section == :alaska_1 && line.starts_with?(SECTION_HEADERS[:alaska_2])
current_section
end