class NameTamer::Text

Attributes

args[R]
string[R]

Public Class Methods

new(string, args = {}) click to toggle source
# File lib/name_tamer/text.rb, line 38
def initialize(string, args = {})
  @string = string
  @args = args
end

Public Instance Methods

neighbours() click to toggle source
# File lib/name_tamer/text.rb, line 30
def neighbours
  @neighbours ||= NameTamer[string].array.neighbours.map { |a| a.join('-') }
end
parameterize() click to toggle source

The string as a slug

# File lib/name_tamer/text.rb, line 17
def parameterize
  @parameterize ||= (
    string
    .dup
    .whitespace_to!(separator)
    .invalid_chars_to!(separator)
    .strip_unwanted!(filter)
    .fix_separators!(separator)
    .approximate_latin_chars!
    .presence || +'_'
  ).downcase
end
segments() click to toggle source

Split the string into segments (e.g. sentences)

# File lib/name_tamer/text.rb, line 12
def segments
  string.split(%r{(?:[\.\?,:;!]|[[:space:]][/-])[[:space:]]})
end
slugs() click to toggle source

All the potential slugs from the string e.g. 'lorem ipsum dolor' -> ['lorem', 'ipsum' ,'dolor', 'lorem-ipsum', 'ipsum-dolor', 'lorem-ipsum-dolor']

# File lib/name_tamer/text.rb, line 7
def slugs
  @slugs ||= segments.flat_map { |s| self.class.new(s).neighbours }.uniq
end

Private Instance Methods

filter() click to toggle source
# File lib/name_tamer/text.rb, line 51
def filter
  @filter ||= args[:filter] || (rfc3987 ? FILTER_RFC3987 : FILTER_COMPAT)
end
rfc3987() click to toggle source
# File lib/name_tamer/text.rb, line 47
def rfc3987
  @rfc3987 ||= args[:rfc3987] || false
end
separator() click to toggle source
# File lib/name_tamer/text.rb, line 43
def separator
  @separator ||= args[:sep] || SLUG_DELIMITER
end