Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/core_ext/string.rb
Constant Summary collapse
- POSTGRESQL_RESERVED_WORDS =
%W{ ALL ANALYSE ANALYZE AND ANY ARRAY AS ASC ASYMMETRIC AUTHORIZATION BETWEEN BINARY BOTH CASE CAST CHECK COLLATE COLUMN CONSTRAINT CREATE CROSS CURRENT_DATE CURRENT_ROLE CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER DEFAULT DEFERRABLE DESC DISTINCT DO ELSE END EXCEPT FALSE FOR FOREIGN FREEZE FROM FULL GRANT GROUP HAVING ILIKE IN INITIALLY INNER INTERSECT INTO IS ISNULL JOIN LEADING LEFT LIKE LIMIT LOCALTIME LOCALTIMESTAMP NATURAL NEW NOT NOTNULL NULL OFF OFFSET OLD ON ONLY OR ORDER OUTER OVERLAPS PLACING PRIMARY REFERENCES RIGHT SELECT SESSION_USER SIMILAR SOME SYMMETRIC TABLE THEN TO TRAILING TRUE UNION UNIQUE USER USING VERBOSE WHEN WHERE }
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.random(length = 10) ⇒ Object
18 19 20 |
# File 'lib/core_ext/string.rb', line 18 def self.random(length=10) ('a'..'z').sort_by {rand}[0,length].join end |
Instance Method Details
#blank? ⇒ Boolean
14 15 16 |
# File 'lib/core_ext/string.rb', line 14 def blank? self !~ /\S/ end |
#normalize ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/core_ext/string.rb', line 22 def normalize str = self.downcase return '' if str.blank? n = str.force_encoding("UTF-8") n.gsub!(/[àáâãäåāă]/, 'a') n.gsub!(/æ/, 'ae') n.gsub!(/[ďđ]/, 'd') n.gsub!(/[çćčĉċ]/, 'c') n.gsub!(/[èéêëēęěĕė]/, 'e') n.gsub!(/ƒ/, 'f') n.gsub!(/[ĝğġģ]/, 'g') n.gsub!(/[ĥħ]/, 'h') n.gsub!(/[ììíîïīĩĭ]/, 'i') n.gsub!(/[įıijĵ]/, 'j') n.gsub!(/[ķĸ]/, 'k') n.gsub!(/[łľĺļŀ]/, 'l') n.gsub!(/[ñńňņʼnŋ]/, 'n') n.gsub!(/[òóôõöøōőŏŏ]/, 'o') n.gsub!(/œ/, 'oe') n.gsub!(/ą/, 'q') n.gsub!(/[ŕřŗ]/, 'r') n.gsub!(/[śšşŝș]/, 's') n.gsub!(/[ťţŧț]/, 't') n.gsub!(/[ùúûüūůűŭũų]/, 'u') n.gsub!(/ŵ/, 'w') n.gsub!(/[ýÿŷ]/, 'y') n.gsub!(/[žżź]/, 'z') n.gsub!(/[ÀÁÂÃÄÅĀĂ]/i, 'A') n.gsub!(/Æ/i, 'AE') n.gsub!(/[ĎĐ]/i, 'D') n.gsub!(/[ÇĆČĈĊ]/i, 'C') n.gsub!(/[ÈÉÊËĒĘĚĔĖ]/i, 'E') n.gsub!(/Ƒ/i, 'F') n.gsub!(/[ĜĞĠĢ]/i, 'G') n.gsub!(/[ĤĦ]/i, 'H') n.gsub!(/[ÌÌÍÎÏĪĨĬ]/i, 'I') n.gsub!(/[IJĴ]/i, 'J') n.gsub!(/[Ķĸ]/i, 'J') n.gsub!(/[ŁĽĹĻĿ]/i, 'L') n.gsub!(/[ÑŃŇŅʼnŊ]/i, 'M') n.gsub!(/[ÒÓÔÕÖØŌŐŎŎ]/i, 'N') n.gsub!(/Œ/i, 'OE') n.gsub!(/Ą/i, 'Q') n.gsub!(/[ŔŘŖ]/i, 'R') n.gsub!(/[ŚŠŞŜȘ]/i, 'S') n.gsub!(/[ŤŢŦȚ]/i, 'T') n.gsub!(/[ÙÚÛÜŪŮŰŬŨŲ]/i, 'U') n.gsub!(/Ŵ/i, 'W') n.gsub!(/[ÝŸŶ]/i, 'Y') n.gsub!(/[ŽŻŹ]/i, 'Z') n end |
#sanitize ⇒ Object
75 76 77 78 79 80 |
# File 'lib/core_ext/string.rb', line 75 def sanitize return if self.blank? self.gsub(/<[^>]+>/m,'').normalize.downcase.gsub(/&.+?;/,'-'). gsub(/[^a-z0-9 _-]/,'-').strip.gsub(/\s+/,'-').gsub(/-+/,'-'). gsub(/-/,' ').strip.gsub(/ /,'-').gsub(/-/,'_') end |
#sanitize_column_name ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/core_ext/string.rb', line 82 def sanitize_column_name temporal_name = self.sanitize if temporal_name !~ /^[a-zA-Z_]/ || POSTGRESQL_RESERVED_WORDS.include?(self.upcase) return '_' + temporal_name else temporal_name end end |