class WordCountAnalyzer::NumberedList

Constants

NUMBERED_LIST_REGEX

Rubular: rubular.com/r/RKmRH9Y4oO

Attributes

string[R]

Public Class Methods

new(string:) click to toggle source
# File lib/word_count_analyzer/numbered_list.rb, line 7
def initialize(string:)
  @string = string
end

Public Instance Methods

includes_numbered_list?() click to toggle source
# File lib/word_count_analyzer/numbered_list.rb, line 11
def includes_numbered_list?
  !(string !~ NUMBERED_LIST_REGEX) && has_at_least_two_items?
end
occurrences() click to toggle source
# File lib/word_count_analyzer/numbered_list.rb, line 38
def occurrences
  count_list_items_in_array
end
replace() click to toggle source
# File lib/word_count_analyzer/numbered_list.rb, line 15
def replace
  new_string = string.dup
  list_array = string.scan(NUMBERED_LIST_REGEX).map(&:to_i)
  skips = 0
  list_array.each_with_index do |a, i|
    if (a + 1).eql?(list_array[i + 1]) ||
                (a - 1).eql?(list_array[i - 1]) ||
                (a.eql?(0) && list_array[i - 1].eql?(9)) ||
                (a.eql?(9) && list_array[i + 1].eql?(0))
      new_string.gsub!(NUMBERED_LIST_REGEX).with_index do |match, index|
        if i.eql?(index + (i - skips)) && match.chomp('.').eql?(a.to_s)
          ''
        else
          match
        end
      end
    else
      skips +=1
    end
  end
  new_string
end

Private Instance Methods

count_list_items_in_array() click to toggle source
# File lib/word_count_analyzer/numbered_list.rb, line 48
def count_list_items_in_array
  list_array = string.scan(NUMBERED_LIST_REGEX).map(&:to_i)
  counter = 0
  list_array.each_with_index do |a, i|
    next unless (a + 1).eql?(list_array[i + 1]) ||
                (a - 1).eql?(list_array[i - 1]) ||
                (a.eql?(0) && list_array[i - 1].eql?(9)) ||
                (a.eql?(9) && list_array[i + 1].eql?(0))
    counter += 1
  end
  counter
end
has_at_least_two_items?() click to toggle source
# File lib/word_count_analyzer/numbered_list.rb, line 44
def has_at_least_two_items?
  count_list_items_in_array >= 2
end