Class: String

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

Constant Summary collapse

LUCENE_ESCAPE_REGEX =

LUCENE_ESCAPE_REGEX = /(+|-|&&||||!|(|)|{|}|[|]|‘|“|~|?|:|\)/

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.randomize(length = 8) ⇒ Object



68
69
70
# File 'lib/ab_admin/core_ext/string.rb', line 68

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

Instance Method Details

#capitalize_firstObject



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

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

#clean_textObject



72
73
74
75
# File 'lib/ab_admin/core_ext/string.rb', line 72

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

#count_wordsObject



58
59
60
# File 'lib/ab_admin/core_ext/string.rb', line 58

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

#is_int?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ab_admin/core_ext/string.rb', line 18

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

#is_number?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ab_admin/core_ext/string.rb', line 22

def is_number?
  true if Float(self) rescue false
end

#lucene_escapeObject



10
11
12
# File 'lib/ab_admin/core_ext/string.rb', line 10

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

#no_htmlObject



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

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

#to_utcObject



26
27
28
29
30
31
32
# File 'lib/ab_admin/core_ext/string.rb', line 26

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

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ab_admin/core_ext/string.rb', line 42

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



62
63
64
65
66
# File 'lib/ab_admin/core_ext/string.rb', line 62

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