Class: String
- Defined in:
- lib/overload/blank.rb,
lib/overload/string.rb,
lib/common/string_base.rb
Instance Method Summary collapse
-
#as_html ⇒ Object
simple markdown.
- #attribute_safe ⇒ Object
- #blank? ⇒ Boolean
- #constantize ⇒ Object
-
#constantize? ⇒ Boolean
‘User’.constantize? # User ‘UserFoo’.constantize? # nil.
- #db_safe ⇒ Object
-
#decolorize ⇒ Object
remomove colorize gem string colors.
- #ends_with?(suffix) ⇒ Boolean
- #escape ⇒ Object
- #extract_scripts!(list: false) ⇒ Object
- #first ⇒ Object
- #fix_ut8 ⇒ Object
-
#html_escape(display = false) ⇒ Object
prepare data for storage write, to make it safe to dump on screen without unescape.
-
#html_safe(full = false) ⇒ Object
export html without scripts and styles.
-
#html_unsafe(full = false) ⇒ Object
restore original before storage read.
- #last(num = 1) ⇒ Object
- #md5 ⇒ Object
- #parameterize ⇒ Object (also: #to_url)
- #parse_erb(scope = nil) ⇒ Object
- #qs_to_hash ⇒ Object
- #quick_sanitize ⇒ Object
- #remove_tags ⇒ Object
- #sanitize ⇒ Object
- #sha1 ⇒ Object
- #span_green ⇒ Object
- #span_red ⇒ Object
- #starts_with?(prefix) ⇒ Boolean
- #string_id ⇒ Object
- #to_a ⇒ Object
- #to_slug(len = 80) ⇒ Object
- #trim(len) ⇒ Object
- #unescape ⇒ Object
- #wrap(node_name, opts = {}) ⇒ Object
Instance Method Details
#as_html ⇒ Object
simple markdown
44 45 46 47 48 |
# File 'lib/overload/string.rb', line 44 def as_html self .gsub($/, '<br />') .gsub(/(https?:\/\/[^\s<]+)/) { %[<a href="#{$1}">#{$1.trim(40)}</a>] } end |
#attribute_safe ⇒ Object
116 117 118 |
# File 'lib/overload/string.rb', line 116 def attribute_safe self.gsub('"', '').gsub("'", '') end |
#blank? ⇒ Boolean
66 67 68 69 70 |
# File 'lib/overload/blank.rb', line 66 def blank? return true if self.length == 0 !(self =~ /[^\s]/) end |
#constantize ⇒ Object
2 3 4 |
# File 'lib/overload/string.rb', line 2 def constantize Object.const_get('::' + self) end |
#constantize? ⇒ Boolean
‘User’.constantize? # User ‘UserFoo’.constantize? # nil
8 9 10 |
# File 'lib/overload/string.rb', line 8 def constantize? Object.const_defined?('::' + self) ? constantize : nil end |
#db_safe ⇒ Object
120 121 122 |
# File 'lib/overload/string.rb', line 120 def db_safe self.gsub(/[^0-9a-zA-Z_]/, '') end |
#decolorize ⇒ Object
remomove colorize gem string colors
142 143 144 |
# File 'lib/overload/string.rb', line 142 def decolorize self.gsub(/\[0;\d\d;\d\dm([^\[]*)\[0m/) { $1 } end |
#ends_with?(suffix) ⇒ Boolean
128 129 130 131 |
# File 'lib/overload/string.rb', line 128 def ends_with? suffix return true if suffix == self suffix.is_a?(String) && self[-suffix.length, suffix.length] == suffix && self != suffix end |
#escape ⇒ Object
146 147 148 |
# File 'lib/overload/string.rb', line 146 def escape CGI::escape(self).gsub('+', '%20') end |
#extract_scripts!(list: false) ⇒ Object
162 163 164 165 166 |
# File 'lib/overload/string.rb', line 162 def extract_scripts! list: false scripts = [] self.gsub!(/<script\b[^>]*>(.*?)<\/script>/im) { scripts.push $1; '' } list ? scripts : scripts.map{ "<script>#{_1}</script>"}.join($/) end |
#first ⇒ Object
56 57 58 |
# File 'lib/overload/string.rb', line 56 def first self[0,1] end |
#fix_ut8 ⇒ Object
83 84 85 |
# File 'lib/overload/string.rb', line 83 def fix_ut8 self.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '?') end |
#html_escape(display = false) ⇒ Object
prepare data for storage write, to make it safe to dump on screen without unescape
13 14 15 16 17 18 19 |
# File 'lib/overload/string.rb', line 13 def html_escape display = false # .gsub('<', '<').gsub('>', '>') # .gsub("'", ''').gsub('"', '"') self .gsub('<', display ? '<' : '#LT;') .gsub(/\A^\s+|\s+\z/,'') end |
#html_safe(full = false) ⇒ Object
export html without scripts and styles
37 38 39 40 41 |
# File 'lib/overload/string.rb', line 37 def html_safe full = false html_unsafe(full) .gsub(/<(\/?script)/i,'<\1') .gsub(/<(\/?style)/i,'<\1') end |
#html_unsafe(full = false) ⇒ Object
restore original before storage read
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/overload/string.rb', line 22 def html_unsafe full = false if full self .gsub('#LT;', '<') .gsub('$LT;', '<') .gsub('<', '<') .gsub('>', '>') .gsub(''', "'") .gsub('"', '"') else self.gsub('#LT;', '<') end end |
#last(num = 1) ⇒ Object
60 61 62 63 |
# File 'lib/overload/string.rb', line 60 def last num = 1 len = self.length self[len-num, len] end |
#md5 ⇒ Object
158 159 160 |
# File 'lib/overload/string.rb', line 158 def md5 Digest::MD5.hexdigest self end |
#parameterize ⇒ Object Also known as: to_url
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/overload/string.rb', line 91 def parameterize str_from = 'šđč枊ĐČĆŽäÄéeöÖüüÜß' str_to = 'sdcczSDCCZaAeeoOuuUs' self .tr(str_from, str_to) .sub(/^[^\w+]/, '') .sub(/[^\w+]$/, '') .downcase .gsub(/[^\w+]+/,'-')[0, 50] end |
#parse_erb(scope = nil) ⇒ Object
87 88 89 |
# File 'lib/overload/string.rb', line 87 def parse_erb scope = nil ERB.new(self).result(scope || binding) end |
#qs_to_hash ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/overload/string.rb', line 104 def qs_to_hash self.split('&').inject({}) do |h,line| el = line.split('=', 2) h[el[0]] = el[1] h end end |
#quick_sanitize ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/overload/string.rb', line 70 def quick_sanitize out = self.gsub('<!--{tag}-->', '') out = out.gsub(/\sstyle="([^"]+)"/) do $1.start_with?('text-align:') ? $1 : '' end out end |
#remove_tags ⇒ Object
172 173 174 |
# File 'lib/overload/string.rb', line 172 def self.gsub(/<[^>]+>/, '') end |
#sanitize ⇒ Object
66 67 68 |
# File 'lib/overload/string.rb', line 66 def sanitize Sanitize.clean(self, :elements=>%w[span ul ol li b bold i italic u underline hr br p], :attributes=>{'span'=>['style']} ) end |
#sha1 ⇒ Object
154 155 156 |
# File 'lib/overload/string.rb', line 154 def sha1 Digest::SHA1.hexdigest self end |
#span_green ⇒ Object
133 134 135 |
# File 'lib/overload/string.rb', line 133 def span_green %[<span style="color: #080;">#{self}</span>] end |
#span_red ⇒ Object
137 138 139 |
# File 'lib/overload/string.rb', line 137 def span_red %[<span style="color: #800;">#{self}</span>] end |
#starts_with?(prefix) ⇒ Boolean
124 125 126 |
# File 'lib/overload/string.rb', line 124 def starts_with? prefix prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix end |
#string_id ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/common/string_base.rb', line 81 def string_id begin StringBase.decode self.split('-').last rescue raise ArgumentError.new('Bad ID for string_id') end end |
#to_a ⇒ Object
112 113 114 |
# File 'lib/overload/string.rb', line 112 def to_a self.split(/\s*,\s*/) end |
#to_slug(len = 80) ⇒ Object
168 169 170 |
# File 'lib/overload/string.rb', line 168 def to_slug len = 80 self.downcase.gsub(/[^\w]+/, '_').gsub(/_+/, '-').sub(/\-$/, '')[0, len] end |
#trim(len) ⇒ Object
50 51 52 53 54 |
# File 'lib/overload/string.rb', line 50 def trim len return self if self.length<len data = self.dup[0,len]+'…' data end |
#unescape ⇒ Object
150 151 152 |
# File 'lib/overload/string.rb', line 150 def unescape CGI::unescape self end |
#wrap(node_name, opts = {}) ⇒ Object
78 79 80 81 |
# File 'lib/overload/string.rb', line 78 def wrap node_name, opts={} return self unless node_name opts.tag(node_name, self) end |