Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/duffy/string.rb
Overview
Monkey Patch String adding useful methods.
Instance Method Summary collapse
- #gender_human ⇒ Object
-
#md5 ⇒ Object
(also: #md5sum)
Easier way to calculate the md5sum of a string.
- #nl2br ⇒ Object
-
#pretty_committer ⇒ Object
Parse a git username into a friendly name Expects committers to be a hash in this format: { jpd“ => ”Jacob“, /ers|Eric/ => ”Eric“ } All String keys are converted to Regex, explicit Regex is fine too.
- #pretty_phone ⇒ Object
-
#sanitize_ssn ⇒ Object
Sanitize SSN.
-
#smart_titlecase ⇒ Object
Smarter Titlecase function.
- #space2nbsp ⇒ Object
- #to_alpha ⇒ Object
- #to_alpha_numeric ⇒ Object
- #to_numeric ⇒ Object
- #to_ssn ⇒ Object
Instance Method Details
#gender_human ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/duffy/string.rb', line 81 def gender_human case (self.to_s.upcase) when "M" then "Male" when "F" then "Female" when "O" then "Other" else "" end end |
#md5 ⇒ Object Also known as: md5sum
Easier way to calculate the md5sum of a string.
5 6 7 |
# File 'lib/duffy/string.rb', line 5 def md5 Digest::MD5.hexdigest(self) end |
#nl2br ⇒ Object
76 77 78 |
# File 'lib/duffy/string.rb', line 76 def nl2br (self.nil?)? "" : self.to_s.gsub(/(\r)?\n/, "<br />").html_safe end |
#pretty_committer ⇒ Object
Parse a git username into a friendly name Expects committers to be a hash in this format: { jpd“ => ”Jacob“, /ers|Eric/ => ”Eric“ } All String keys are converted to Regex, explicit Regex is fine too.
67 68 69 |
# File 'lib/duffy/string.rb', line 67 def pretty_committer Hash(Duffy.configuration.committers).map{|k,v| v if Regexp.new(k) =~ self}.compact.first or self end |
#pretty_phone ⇒ Object
58 59 60 61 62 |
# File 'lib/duffy/string.rb', line 58 def pretty_phone number = self.to_s.gsub(/[^0-9\+]/, "") return "(" + number[0..2] + ") " + number[3..5] + "-" + number[6..9] if number.length == 10 number end |
#sanitize_ssn ⇒ Object
Sanitize SSN.
“123456789” => “123456789” # Good “123-45-6789” => “123456789” # Good “000-12-3456” => nil # Invalid Area “666123456” => nil # Invalid Area “derp” => nil # Bogus “123456” => nil # Too short ( becomes 000123456: Invalid Area ) “999999999” => nil # Important. Many HRS entries “-” => nil # Important. Many HRS entries
www.ssa.gov/history/ssn/geocard.html en.wikipedia.org/wiki/Social_Security_number area: 001 to 665, 667 to 899 group: 01 to 99. serial: 0001 to 9999
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/duffy/string.rb', line 31 def sanitize_ssn begin ssn = "%09d" % self.to_numeric.to_i raise "Too Long" if ssn.length > 9 area, group, serial = ssn[0..2], ssn[3..4], ssn[5..8] raise "Area Range Invalid" unless ("001" .. "665").cover?(area) or ("667" .. "899").cover?(area) raise "Group Range Invalid" unless ("01" .. "99").cover?(group) raise "Serial Range Invalid" unless ("0001" .. "9999").cover?(serial) rescue return nil end ssn end |
#smart_titlecase ⇒ Object
Smarter Titlecase function. Examples: “foo” => “Foo” “IMPORTANT STUFF” => “Important Stuff” “123 main st.” => “123 Main St.” “a tale of two cities” => “A Tale of Two Cities”
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/duffy/string.rb', line 97 def smart_titlecase small_re = %w( is a an and as at but by en for if in of on or the to v[.]? via vs[.]? ).join('|') # Original Perl version by: John Gruber (daringfireball.net) 2008-05-10 # Adapted to Ruby by: Marshall Elfstrand (vengefulcow.com/ 2008-05-21 # Improved and customized: Jacob Duffy (duffy.jp) 2011-01-25 # License: http://www.opensource.org/licenses/mit-license.php # if source is all uppercase, use all lowercase instead. -- jpd string = (self.upcase == self)? self.downcase : self result = "" string.gsub(/[_-]/, ' ').split(/( [:.;?!][ ] | (?:[ ]|^)[""] )/x).each do |s| s.gsub!(/ \b( [[:alpha:]] [[:lower:].'']* )\b /x) do |w| # Skip words with inresult dots, e.g. "del.icio.us" or "example.com" (w =~ / [[:alpha:]] [.] [[:alpha:]] /x) ? w : w.capitalize end #gsub! # Lowercase our list of small words: s.gsub!(/\b(#{small_re})\b/io) { |w| w.downcase } # If the first word in the title is a small word, then capitalize it: s.gsub!(/\A([[:punct:]]*)(#{small_re})\b/io) { |w| $1 + $2.capitalize } # If the last word in the title is a small word, then capitalize it: s.gsub!(/\b(#{small_re})([[:punct:]]*)\Z/io) { |w| $1.capitalize + $2 } # Append current substring to output result += s end #each # Special Cases: upcase_re = (Array(Duffy.configuration.upcase_custom) + Array(Duffy.configuration.upcase_default)).uniq.join("|") result.gsub!(/ V(s?)\. /, ' v\1. ') # "v." and "vs." result.gsub!(/([''])S\b/, '\1s') # 'S (otherwise you get "the SEC'S decision") result.gsub!(/\b(#{upcase_re})\b/i) { |w| w.upcase } unless upcase_re.blank? result end |
#space2nbsp ⇒ Object
71 72 73 74 |
# File 'lib/duffy/string.rb', line 71 def space2nbsp # turns double space to double nbsp (self.nil?)? "" : self.to_s.gsub(/ {2}/, " ").html_safe end |
#to_alpha ⇒ Object
49 50 51 |
# File 'lib/duffy/string.rb', line 49 def to_alpha self.to_s.gsub(/[^a-zA-Z ]/, "").strip # string with only a-z or A-Z or space, leading+trailing whitespace removed end |
#to_alpha_numeric ⇒ Object
53 54 55 |
# File 'lib/duffy/string.rb', line 53 def to_alpha_numeric self.to_s.gsub(/[^0-9a-zA-Z ]/, "").strip # string with only a-z or A-Z or space, leading+trailing whitespace removed end |
#to_numeric ⇒ Object
45 46 47 |
# File 'lib/duffy/string.rb', line 45 def to_numeric self.to_s.gsub(/[^0-9]/, "") # now it's a string with only 0-9 end |
#to_ssn ⇒ Object
10 11 12 13 |
# File 'lib/duffy/string.rb', line 10 def to_ssn ssn = "%09d" % self.to_numeric.to_i # strips string of non zeros, and pads leading zeros ssn[0..2] + "-" + ssn[3..4] + "-" + ssn[5..8] # now it's like 123-45-6789 end |