Class: String
Constant Summary collapse
- LUCENE_ESCAPE_REGEX =
/(\+|-|&&|\|\||!|\(|\)|{|}|\[|\]|`|"|~|\?|:|\\)/
- KEYBOARDS =
LUCENE_ESCAPE_REGEX = /(+|-|&&||||!|(|)|{|}|[|]|‘|“|~|?|:|\|s)/
{ en: 'qwertyuiop[]asdfghjkl;\'zxcvbnm,./', ru: 'йцукенгшщзхъфывапролджэячсмитьбю/' }
Class Method Summary collapse
Instance Method Summary collapse
- #capitalize_first ⇒ Object
- #clean_text ⇒ Object
- #count_words ⇒ Object
- #is_int? ⇒ Boolean
- #is_number? ⇒ Boolean
- #lucene_escape ⇒ Object
- #no_html ⇒ Object
- #to_utc ⇒ Object
- #tr_lang(from = nil, to = nil) ⇒ Object
- #words_count ⇒ Object
Class Method Details
Instance Method Details
#capitalize_first ⇒ Object
15 16 17 |
# File 'lib/ab_admin/core_ext/string.rb', line 15 def capitalize_first self.mb_chars[0].capitalize + self.mb_chars[1..-1] end |
#clean_text ⇒ Object
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 |
#count_words ⇒ Object
59 60 61 |
# File 'lib/ab_admin/core_ext/string.rb', line 59 def count_words clean_text.scan(/(\p{Alnum}+([-'.]\p{Alnum}+)*)/u).size end |
#is_int? ⇒ Boolean
19 20 21 |
# File 'lib/ab_admin/core_ext/string.rb', line 19 def is_int? self =~ /^[-+]?[0-9]+$/ end |
#is_number? ⇒ Boolean
23 24 25 |
# File 'lib/ab_admin/core_ext/string.rb', line 23 def is_number? true if Float(self) rescue false end |
#lucene_escape ⇒ Object
11 12 13 |
# File 'lib/ab_admin/core_ext/string.rb', line 11 def lucene_escape self.gsub(LUCENE_ESCAPE_REGEX, "\\\\\\1") end |
#no_html ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/ab_admin/core_ext/string.rb', line 35 def no_html str = self.dup str.gsub!(/<\/?[^>]*>/, '') str.strip! str.gsub!(' ', ' ') str end |
#to_utc ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/ab_admin/core_ext/string.rb', line 27 def to_utc begin Time.zone.parse(self).utc rescue Time.now.utc end end |
#tr_lang(from = nil, to = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ab_admin/core_ext/string.rb', line 43 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 |