Module: Lazier::String

Extended by:
ActiveSupport::Concern
Defined in:
lib/lazier/string.rb

Overview

Extensions for the String class.

Instance Method Summary collapse

Instance Method Details

#ensure_valid_utf8(replacement = "") ⇒ String

Makes sure the string only contains valid UTF-8 sequences.

Parameters:

  • replacement (String) (defaults to: "")

    The string to use to replace invalid sequences.

Returns:

  • (String)

    The string with any invalid UTF-8 sequences replaced.



28
29
30
31
# File 'lib/lazier/string.rb', line 28

def ensure_valid_utf8(replacement = "")
  # This odd line is because if need to specify a different encoding (without losing infos) to replace invalid bytes and then we go back to utf-8
  !defined?(JRUBY_VERSION) ? encode("utf-16", invalid: :replace, undef: :replace, replace: replacement).encode("utf-8") : raise(RuntimeError.new("Sorry, Lazier::String#ensure_valid_utf8 is not available on JRuby."))
end

#remove_accentsObject

Removes accents from the string, normalizing to the normal letter.

"èòàù".remove_accents
# => "eoau"

Returns:

  • The string with all accents removed.



20
21
22
# File 'lib/lazier/string.rb', line 20

def remove_accents
  silence_warnings { mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, "").to_s }
end

#replace_ampersandsString

Returns the string with all & replaced with &.

Returns:

  • (String)

    The string with all & replaced with &.



50
51
52
# File 'lib/lazier/string.rb', line 50

def replace_ampersands
  gsub(/&(\S+);/, "&\\1;")
end

#split_tokens(no_blanks = true, strip = true, uniq = false, pattern = /\s*,\s*/) ⇒ Array

Splits a string containing tokens using a specified pattern and applying some sanitizations.

Parameters:

  • no_blanks (Boolean) (defaults to: true)

    If filter out blank tokens.

  • strip (Boolean) (defaults to: true)

    If strip single tokens.

  • uniq (Boolean) (defaults to: false)

    If return uniques elements.

  • pattern (String|Regexp) (defaults to: /\s*,\s*/)

    The pattern to use.

Returns:

  • (Array)

    An array of tokens.



68
69
70
71
72
73
74
# File 'lib/lazier/string.rb', line 68

def split_tokens(no_blanks = true, strip = true, uniq = false, pattern = /\s*,\s*/)
  rv = self.split(pattern)
  rv.map!(&:strip) if strip
  rv.select!(&:present?) if no_blanks
  rv.uniq! if uniq
  rv
end

#untitleizeString

Returns the tagged version of a string.

The string is downcased and spaces are substituted with -.

"ABC cde".untitleize
# => "abc-cde"

Returns:

  • (String)

    The untitleized version of the string.



43
44
45
# File 'lib/lazier/string.rb', line 43

def untitleize
  downcase.gsub(" ", "-")
end

#valueString

Returns the string itself for use in form helpers.

Returns:

  • (String)

    The string itself.



57
58
59
# File 'lib/lazier/string.rb', line 57

def value
  self
end