Class: AhlScraper::ParameterizeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ahl_scraper/helpers/parameterize_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, separator: "-", preserve_case: false) ⇒ ParameterizeHelper

Returns a new instance of ParameterizeHelper.



7
8
9
10
11
# File 'lib/ahl_scraper/helpers/parameterize_helper.rb', line 7

def initialize(string, separator: "-", preserve_case: false)
  @string = string
  @separator = separator
  @preserve_case = preserve_case
end

Instance Attribute Details

#preserve_caseObject (readonly)

Returns the value of attribute preserve_case.



5
6
7
# File 'lib/ahl_scraper/helpers/parameterize_helper.rb', line 5

def preserve_case
  @preserve_case
end

#separatorObject (readonly)

Returns the value of attribute separator.



5
6
7
# File 'lib/ahl_scraper/helpers/parameterize_helper.rb', line 5

def separator
  @separator
end

#stringObject (readonly)

Returns the value of attribute string.



5
6
7
# File 'lib/ahl_scraper/helpers/parameterize_helper.rb', line 5

def string
  @string
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ahl_scraper/helpers/parameterize_helper.rb', line 13

def call
  parameterized_string = string.dup.to_s

  parameterized_string.gsub!(/[^a-zA-Z0-9\-_]+/, separator)

  unless separator.nil? || separator.empty?
    if separator == "-"
      re_duplicate_separator        = /-{2,}/
      re_leading_trailing_separator = /^-|-$/
    else
      re_sep = Regexp.escape(separator)
      re_duplicate_separator = /#{re_sep}{2,}/
      re_leading_trailing_separator = /^#{re_sep}|#{re_sep}$/
    end
    parameterized_string.gsub!(re_duplicate_separator, separator)
    parameterized_string.gsub!(re_leading_trailing_separator, "")
  end

  parameterized_string.downcase! unless preserve_case
  parameterized_string
end