Sha256: 8158e6d2e6d67b9ec301b1a0db9c4beb6c99865f46bd205c09ebe0c9a68d1cda

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

# -*- encoding : utf-8 -*-
class String
  #LUCENE_ESCAPE_REGEX = /(\+|-|&&|\|\||!|\(|\)|{|}|\[|\]|`|"|~|\?|:|\\)/
  LUCENE_ESCAPE_REGEX = /(\+|-|&&|\|\||!|\(|\)|{|}|\[|\]|`|"|~|\?|:|\\|\s)/

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

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

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

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

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

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

  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

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

  def words_count
		frequencies = Hash.new(0)  
		downcase.scan(/(\w+([-'.]\w+)*)/) { |word, ignore| frequencies[word] += 1 }
		frequencies
	end
	
	def self.randomize(length = 8)
	  Array.new(length) { (rand(122-97) + 97).chr }.join
  end

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

end

unless ''.respond_to?(:each)
  String.class_eval do
    def each &block
      self.lines &block
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ab_admin-0.2.1 lib/ab_admin/core_ext/string.rb
ab_admin-0.2.0 lib/ab_admin/core_ext/string.rb