Module: Lazier::String
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/string.rb
Overview
Extensions for the String class.
Instance Method Summary (collapse)
-
- (String) ensure_valid_utf8(replacement = "")
Makes sure the string only contains valid UTF-8 sequences.
-
- (Object) remove_accents
Removes accents from the string, normalizing to the normal letter.
-
- (String) replace_ampersands
Returns the string with all
&
replaced with&
. -
- (Array) split_tokens(no_blanks = true, strip = true, uniq = false, pattern = /\s*,\s*/)
Splits a string containing tokens using a specified pattern and applying some sanitizations.
-
- (String) untitleize
Returns the tagged version of a string.
-
- (String) value
Returns the string itself for use in form helpers.
Instance Method Details
- (String) ensure_valid_utf8(replacement = "")
Makes sure the string only contains valid UTF-8 sequences.
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 encode("utf-16", invalid: :replace, undef: :replace, replace: replacement).encode("utf-8") end |
- (Object) remove_accents
Removes accents from the string, normalizing to the normal letter.
ruby
"èòàù".remove_accents
# => "eoau"
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 |
- (String) replace_ampersands
Returns the string with all &
replaced with &
.
50 51 52 |
# File 'lib/lazier/string.rb', line 50 def replace_ampersands gsub(/&(\S+);/, "&\\1;") end |
- (Array) split_tokens(no_blanks = true, strip = true, uniq = false, pattern = /\s*,\s*/)
Splits a string containing tokens using a specified pattern and applying some sanitizations.
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 = split(pattern) rv.map!(&:strip) if strip rv.select!(&:present?) if no_blanks rv.uniq! if uniq rv end |
- (String) untitleize
Returns the tagged version of a string.
The string is downcased and spaces are substituted with -
.
ruby
"ABC cde".untitleize
# => "abc-cde"
43 44 45 |
# File 'lib/lazier/string.rb', line 43 def untitleize downcase.gsub(" ", "-") end |
- (String) value
Returns the string itself for use in form helpers.
57 58 59 |
# File 'lib/lazier/string.rb', line 57 def value self end |