Class: String

Inherits:
Object show all
Defined in:
lib/ab_admin/core_ext/string.rb

Constant Summary collapse

KEYBOARDS =

lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html LUCENE_ESCAPE_REGEX = /(+|-|&&||||!|(|)|{|}|[|]|‘|“|~|?|:|\|/)/ LUCENE_ESCAPE_REGEX = /(+|-|&&||||!|(|)|{|}|[|]|`|”|~|?|:|\|s)/

{
    en: 'qwertyuiop[]asdfghjkl;\'zxcvbnm,./',
    ru: 'йцукенгшщзхъфывапролджэячсмитьбю/'
}
LUCENE_ESCAPE_REGEX =
/(\+|-|&&|\|\||!|\(|\)|{|}|\[|\]|\^|"|~|\*|\?|:|\\|\/)/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.randomize(length = 8) ⇒ Object



75
76
77
# File 'lib/ab_admin/core_ext/string.rb', line 75

def self.randomize(length = 8)
 Array.new(length) { (rand(122-97) + 97).chr }.join
end

Instance Method Details

#capitalize_firstObject



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

def capitalize_first
  self.mb_chars[0].capitalize + self.mb_chars[1..-1]
end

#clean_textObject



79
80
81
82
# File 'lib/ab_admin/core_ext/string.rb', line 79

def clean_text
  coder = HTMLEntities.new
  coder.decode(self.no_html)
end

#count_wordsObject



65
66
67
# File 'lib/ab_admin/core_ext/string.rb', line 65

def count_words
  clean_text.scan(/(\p{Alnum}+([-'.]\p{Alnum}+)*)/u).size
end

#is_int?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/ab_admin/core_ext/string.rb', line 24

def is_int?
  self =~ /^[-+]?[0-9]+$/
end

#is_number?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ab_admin/core_ext/string.rb', line 28

def is_number?
  self =~ /^[-+]?[0-9]+(\.[0-9]+)?$/
end

#lucene_escapeObject



15
16
17
# File 'lib/ab_admin/core_ext/string.rb', line 15

def lucene_escape
  self.gsub(LUCENE_ESCAPE_REGEX, "\\\\\\1")
end

#mb_downcaseObject



88
89
90
# File 'lib/ab_admin/core_ext/string.rb', line 88

def mb_downcase
  mb_chars.downcase.to_s
end

#mb_upcaseObject



84
85
86
# File 'lib/ab_admin/core_ext/string.rb', line 84

def mb_upcase
  mb_chars.upcase.to_s
end

#no_htmlObject



40
41
42
43
44
45
46
47
# File 'lib/ab_admin/core_ext/string.rb', line 40

def no_html
  str = self.dup
  str.gsub!(/<br\/?>/, ' ')
  str.gsub!(/<\/?[^>]*>/, '')
  str.strip!
  str.gsub!('&nbsp;', ' ')
  str
end

#to_utcObject



32
33
34
35
36
37
38
# File 'lib/ab_admin/core_ext/string.rb', line 32

def to_utc
  begin
    Time.zone.parse(self).utc
  rescue
    Time.now.utc
  end
end

#tr_lang(from = nil, to = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ab_admin/core_ext/string.rb', line 49

def tr_lang(from=nil, to=nil)
  return '' if self.blank?

  unless from || to
    if KEYBOARDS[:en].index(self[0])
      from, to = :en, :ru
    elsif KEYBOARDS[:ru].index(self[0])
      from, to = :ru, :en
    else
      from, to = :en, :ru
    end
  end

  self.tr(KEYBOARDS[from], KEYBOARDS[to])
end

#words_countObject



69
70
71
72
73
# File 'lib/ab_admin/core_ext/string.rb', line 69

def words_count
	frequencies = Hash.new(0)  
	downcase.scan(/(\w+([-'.]\w+)*)/) { |word, ignore| frequencies[word] += 1 }
	frequencies
end