Class: String

Inherits:
Object show all
Includes:
WithoutAccents
Defined in:
lib/core-extensions/string_ext.rb,
lib/core-extensions/uids.rb,
lib/core-extensions/string_ext.rb,
lib/core-extensions/string_ext.rb,
lib/core-extensions/string_ext.rb,
lib/core-extensions/string_ext.rb

Overview

– decode HTML entities —————————————————–

Defined Under Namespace

Modules: HtmlDecoder

Constant Summary

Constants included from WithoutAccents

WithoutAccents::ACCENTS_MAPPING, WithoutAccents::ACCENTS_MAPPING_REGEXPS

Instance Method Summary collapse

Methods included from WithoutAccents

#without_accents

Instance Method Details

#crc32Object



4
5
6
# File 'lib/core-extensions/uids.rb', line 4

def crc32
  Zlib.crc32(self)
end

#ends_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/core-extensions/string_ext.rb', line 56

def ends_with?(other)
  length >= other.length && self[length - other.length .. -1] == other
end

#md5Object



8
9
10
# File 'lib/core-extensions/uids.rb', line 8

def md5
  Digest::MD5.hexdigest(self)
end

#sortkeyObject



41
42
43
44
45
46
47
48
# File 'lib/core-extensions/string_ext.rb', line 41

def sortkey
  # Convert the key into an sortable ascii string
  self.without_accents.                               # remove accents
    downcase.
    gsub(/^\s*(der|die|das|the|a|ein)\b\s*/, "").     # remove leading stop words
    gsub(/[0-9]+/) { |s| "%03d" % s.to_i }.           # fill in leading zeroes
    gsub(/[^a-z0-9]/, "")                             # keep only letters and digits
end

#starts_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/core-extensions/string_ext.rb', line 52

def starts_with?(other)
  length >= other.length && self[0, other.length] == other
end

#to_utf8Object



21
22
23
24
25
26
27
28
29
# File 'lib/core-extensions/string_ext.rb', line 21

def to_utf8
  # require "charguess"
  # encoding = CharGuess.guess(self)
  # puts "encoding: #{encoding.inspect}"
  # return self if !encoding || encoding == "UTF-8"

  encoding = 'ISO-8859-1'
  Iconv.conv('utf-8', encoding, self)
end

#uid64Object

return a 64 bit uid



13
14
15
# File 'lib/core-extensions/uids.rb', line 13

def uid64
  md5.unpack("LL").inject { |a,b| (a << 31) + b }
end

#unhtmlObject



12
13
14
# File 'lib/core-extensions/string_ext.rb', line 12

def unhtml
  HtmlDecoder.instance.decode self
end