class String

Constants

ADDR_SPEC
ATEXT
DCONTENT
DOMAIN
DOMAIN_LITERAL
DOT_ATOM
DTEXT
LOCAL_PART
QCONTENT
QTEXT
QUOTED_PAIR
QUOTED_STRING
TEXT

Public Instance Methods

blank?() click to toggle source
# File lib/dgaff/string.rb, line 28
def blank?
  # 1.8 does not takes [:space:] properly
  if encoding_aware?
    begin
      self !~ /[^[:space:]]/
    rescue
      p self
      false
    end
  else
    self !~ NON_WHITESPACE_REGEXP
  end
end
camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) click to toggle source
# File lib/dgaff/string.rb, line 82
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  end
end
classify() click to toggle source
# File lib/dgaff/string.rb, line 70
def classify
  if self.split(//).last == "s"
    if self.split(//)[self.split(//).length-3..self.split(//).length].join == "ies"
      camelize(self.split(//)[0..self.split(//).length-4].join("")+"y")
    else
      camelize(self.sub(/.*\./, '').chop)
    end
  else
    camelize(self.sub(/.*\./, ''))
  end
end
constantize() click to toggle source
# File lib/dgaff/string.rb, line 88
def constantize
  return Object.const_defined?(self) ? Object.const_get(self) : Object.const_missing(self)
end
linkify() click to toggle source
# File lib/dgaff/string.rb, line 63
def linkify
  self.gsub!(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/, '<a href="\1" target="_blank">\1</a>')
  self.gsub!(/(#\w*)/, '<a href="https://twitter.com/search?q=\1" target="_blank">\1</a>')
  self.gsub!(/@(\w*)/, '<a href="https://twitter.com/\1" target="_blank">@\1</a>')
  self
end
to_class() click to toggle source
# File lib/dgaff/string.rb, line 92
def to_class
  return self.classify.constantize
end
to_url() click to toggle source
# File lib/dgaff/string.rb, line 42
def to_url
  url = URI.parse(URI.encode(self.strip)).to_s
  url
end
truncate(opts={}) click to toggle source
# File lib/dgaff/string.rb, line 51
def truncate(opts={})
  opts = Hashie::Mash[opts]
  opts[:length] ||= 30
  opts.ending_character ||= ""
  set = ""
  text = self.split(" ")
  text.each do |word|
    set = "#{set}#{word} " if opts[:length] >= (set.length + word.length)
  end
  set+opts.ending_character
end
underscore_to_pretty() click to toggle source
# File lib/dgaff/string.rb, line 47
def underscore_to_pretty
  self.split("_").collect(&:capitalize).join(" ")
end
url?() click to toggle source
# File lib/dgaff/string.rb, line 19
def url?
  return !(self =~ URI::regexp).nil?
end
valid_email_address?() click to toggle source
# File lib/dgaff/string.rb, line 23
def valid_email_address?
  result = (self =~ ADDR_SPEC)
  return !result.nil?
end