Class: String

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

Constant Summary collapse

LUCENE_ESCAPE_REGEX =

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

/(\+|-|&&|\|\||!|\(|\)|{|}|\[|\]|`|"|~|\?|:|\\|\s)/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.randomize(length = 8) ⇒ Object



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

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

Instance Method Details

#capitalize_firstObject



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

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

#clean_textObject



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

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

#is_int?Boolean

Returns:

  • (Boolean)


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

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

#is_number?Boolean

Returns:

  • (Boolean)


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

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

#lucene_escapeObject



6
7
8
# File 'lib/ab_admin/core_ext/string.rb', line 6

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

#no_htmlObject



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

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

#to_utcObject



22
23
24
25
26
27
28
# File 'lib/ab_admin/core_ext/string.rb', line 22

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

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



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

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

  keyboard = {}
  keyboard[:en] = %{qwertyuiop[]asdfghjkl;'zxcvbnm,./}
  keyboard[:ru] = %{йцукенгшщзхъфывапролджэячсмитьбю/}

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

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

#words_countObject



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

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