Class: WordCountAnalyzer::Slash

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

Constant Summary collapse

FORWARD_SLASH_REGEX =
/(?<=\s)(\S+\/)+\S+|(?<=\A)(\S+\/)+\S+/
BACKSLASH_REGEX =
/\S+\\\S+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string:, **args) ⇒ Slash

Returns a new instance of Slash.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/word_count_analyzer/slash.rb', line 10

def initialize(string:, **args)
  @string = string
  @date = args[:date] || nil
  @xhtml = args[:xhtml] || nil
  @hyperlink = args[:hyperlink] || nil
  if date.eql?('no_special_treatment')
    if xhtml.eql?('keep')
      if hyperlink.eql?('no_special_treatment') || hyperlink.eql?('split_at_period')
        @processed_string = string
      else
        @processed_string = WordCountAnalyzer::Hyperlink.new(string: string).replace
      end
    else
      if hyperlink.eql?('no_special_treatment') || hyperlink.eql?('split_at_period')
        @processed_string = WordCountAnalyzer::Xhtml.new(string: string).replace
      else
        @processed_string = WordCountAnalyzer::Xhtml.new(string: WordCountAnalyzer::Hyperlink.new(string: string).replace).replace
      end
    end
  else
    if xhtml.eql?('keep')
      if hyperlink.eql?('no_special_treatment') || hyperlink.eql?('split_at_period')
        @processed_string = WordCountAnalyzer::Date.new(string: string).replace
      else
        @processed_string = WordCountAnalyzer::Date.new(string: WordCountAnalyzer::Hyperlink.new(string: string).replace).replace
      end
    else
      if hyperlink.eql?('no_special_treatment') || hyperlink.eql?('split_at_period')
        @processed_string = WordCountAnalyzer::Date.new(string: WordCountAnalyzer::Xhtml.new(string: string).replace).replace
      else
        @processed_string = WordCountAnalyzer::Date.new(string: WordCountAnalyzer::Xhtml.new(string: WordCountAnalyzer::Hyperlink.new(string: string).replace).replace).replace
      end
    end
  end
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/word_count_analyzer/slash.rb', line 9

def date
  @date
end

Returns the value of attribute hyperlink.



9
10
11
# File 'lib/word_count_analyzer/slash.rb', line 9

def hyperlink
  @hyperlink
end

#processed_stringObject (readonly)

Returns the value of attribute processed_string.



9
10
11
# File 'lib/word_count_analyzer/slash.rb', line 9

def processed_string
  @processed_string
end

#stringObject (readonly)

Returns the value of attribute string.



9
10
11
# File 'lib/word_count_analyzer/slash.rb', line 9

def string
  @string
end

#xhtmlObject (readonly)

Returns the value of attribute xhtml.



9
10
11
# File 'lib/word_count_analyzer/slash.rb', line 9

def xhtml
  @xhtml
end

Instance Method Details

#backslash_occurencesObject



73
74
75
# File 'lib/word_count_analyzer/slash.rb', line 73

def backslash_occurences
  processed_string.scan(BACKSLASH_REGEX).size
end

#forward_slash_occurencesObject



54
55
56
# File 'lib/word_count_analyzer/slash.rb', line 54

def forward_slash_occurences
  processed_string.scan(FORWARD_SLASH_REGEX).size
end

#includes_backslash?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/word_count_analyzer/slash.rb', line 50

def includes_backslash?
  !(processed_string !~ BACKSLASH_REGEX)
end

#includes_forward_slash?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/word_count_analyzer/slash.rb', line 46

def includes_forward_slash?
  !(processed_string !~ FORWARD_SLASH_REGEX)
end

#replace_backslashesObject



77
78
79
80
81
82
# File 'lib/word_count_analyzer/slash.rb', line 77

def replace_backslashes
  return processed_string if processed_string !~ BACKSLASH_REGEX
  processed_string.gsub!(BACKSLASH_REGEX).each do |match|
    ' word ' * match.split(/\\+/).length
  end
end

#replace_forward_slashesObject



58
59
60
61
62
63
# File 'lib/word_count_analyzer/slash.rb', line 58

def replace_forward_slashes
  return processed_string if processed_string !~ FORWARD_SLASH_REGEX
  processed_string.gsub!(FORWARD_SLASH_REGEX).each do |match|
    match.split(/\/+/).join(' ')
  end
end

#replace_forward_slashes_except_datesObject



65
66
67
68
69
70
71
# File 'lib/word_count_analyzer/slash.rb', line 65

def replace_forward_slashes_except_dates
  return processed_string if processed_string !~ FORWARD_SLASH_REGEX
  except_date_string = WordCountAnalyzer::Date.new(string: processed_string).replace_number_only_date
  except_date_string.gsub!(FORWARD_SLASH_REGEX).each do |match|
    match.split(/\/+/).join(' ')
  end
end