Class: HustleAndFlow::Utils::UrlSlug

Inherits:
Object
  • Object
show all
Defined in:
lib/hustle_and_flow/utils/url_slug.rb

Instance Method Summary collapse

Constructor Details

#initialize(original_string) ⇒ UrlSlug

Returns a new instance of UrlSlug.



7
8
9
# File 'lib/hustle_and_flow/utils/url_slug.rb', line 7

def initialize(original_string)
  self.original_string = original_string || ''
end

Instance Method Details

#to_sObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hustle_and_flow/utils/url_slug.rb', line 11

def to_s
  original_string.
    downcase.                 # Lowercase the string
    strip.                    # Remove all leading and trailing spaces
    gsub(/-+\z/, '').         # Remove any dashes at the end
    gsub(/\A-+/, '').         # Remove any dashes at the beginning
    gsub(/[^\w\-\s]|_/, '').  # Remove any non-alphanumerics, dashes or spaces
    gsub(/\s+/, '-').         # Consolidate remaining consecutive space and
                              # convert to dashes
    gsub(/-+/, '-')           # Consolidate consecutive dashes into one dash
end