Class: WordCountAnalyzer::NumberedList

Inherits:
Object
  • Object
show all
Defined in:
lib/word_count_analyzer/numbered_list.rb

Constant Summary collapse

NUMBERED_LIST_REGEX =
/(?<=\s)\d{1,2}\.(?=\s)|^\d{1,2}\.(?=\s)|(?<=\s)\d{1,2}\.\)|^\d{1,2}\.\)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string:) ⇒ NumberedList

Returns a new instance of NumberedList.



7
8
9
# File 'lib/word_count_analyzer/numbered_list.rb', line 7

def initialize(string:)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



6
7
8
# File 'lib/word_count_analyzer/numbered_list.rb', line 6

def string
  @string
end

Instance Method Details

#includes_numbered_list?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/word_count_analyzer/numbered_list.rb', line 11

def includes_numbered_list?
  !(string !~ NUMBERED_LIST_REGEX) && has_at_least_two_items?
end

#occurencesObject



38
39
40
# File 'lib/word_count_analyzer/numbered_list.rb', line 38

def occurences
  count_list_items_in_array
end

#replaceObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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