Sha256: d539fb6a1d6e06ea048b60cfb5374de1648a2b3c8e35a337cd89f71f2850c6e2

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 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 words_count
		frequencies = Hash.new(0)  
		downcase.scan(/(\w+([-'.]\w+)*)/) { |word, ignore| frequencies[word] += 1 }
		return frequencies
	end
	
	def self.randomize(length = 8)
	  Array.new(length) { (rand(122-97) + 97).chr }.join
  end

end

if Object.const_defined?(:HTMLEntities)
  require 'htmlentities'
  class String
    def clean_text
      coder = HTMLEntities.new
      coder.decode(self.no_html)
    end
  end
end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ab_admin-0.1.2 lib/ab_admin/core_ext/string.rb
ab_admin-0.1.1 lib/ab_admin/core_ext/string.rb
ab_admin-0.1.0 lib/ab_admin/core_ext/string.rb