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.
- #blank? ⇒ Boolean
- #constantize ⇒ Object
- #css_to_hash ⇒ Object
-
#decolorize ⇒ Object
remomove colorize gem string colors.
- #ends_with?(suffix) ⇒ Boolean
- #escape ⇒ Object
- #first ⇒ Object
- #fix_ut8 ⇒ Object
- #last(num = 1) ⇒ Object
- #parameterize ⇒ Object
- #parse_erb ⇒ Object
- #sanitize ⇒ Object
- #span_green ⇒ Object
- #span_red ⇒ Object
- #starts_with?(prefix) ⇒ Boolean
- #string_id ⇒ Object
- #to_a ⇒ Object
-
#to_html(opts = {}) ⇒ Object
convert escaped strings, remove scritpts.
- #to_url ⇒ Object
- #trim(len) ⇒ Object
- #unescape ⇒ Object
- #wrap(node_name, opts = {}) ⇒ Object
Instance Method Details
#as_html ⇒ Object
simple markdown
7 8 9 |
# File 'lib/overload/string.rb', line 7 def as_html self.gsub($/, '<br />') end |
#blank? ⇒ Boolean
61 62 63 64 65 |
# File 'lib/overload/blank.rb', line 61 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 |
#css_to_hash ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/overload/string.rb', line 58 def css_to_hash self.split('&').inject({}) do |h,line| el = line.split('=', 2) h[el[0]] = el[1] h end end |
#decolorize ⇒ Object
remomove colorize gem string colors
92 93 94 |
# File 'lib/overload/string.rb', line 92 def decolorize self.gsub(/\[0;\d\d;\d\dm([^\[]*)\[0m/) { $1 } end |
#ends_with?(suffix) ⇒ Boolean
74 75 76 |
# File 'lib/overload/string.rb', line 74 def ends_with? suffix suffix.is_a?(String) && self[-suffix.length, suffix.length] == suffix && self != suffix end |
#escape ⇒ Object
96 97 98 |
# File 'lib/overload/string.rb', line 96 def escape CGI::escape self end |
#first ⇒ Object
25 26 27 |
# File 'lib/overload/string.rb', line 25 def first self[0,1] end |
#fix_ut8 ⇒ Object
38 39 40 |
# File 'lib/overload/string.rb', line 38 def fix_ut8 self.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '?') end |
#last(num = 1) ⇒ Object
78 79 80 81 |
# File 'lib/overload/string.rb', line 78 def last(num=1) len = self.length self[len-num, len] end |
#parameterize ⇒ Object
46 47 48 |
# File 'lib/overload/string.rb', line 46 def parameterize self.downcase.gsub(/[^\w]+/,'-') end |
#parse_erb ⇒ Object
42 43 44 |
# File 'lib/overload/string.rb', line 42 def parse_erb self.gsub(/<%=([^%]+)%>/) { eval $1; } end |
#sanitize ⇒ Object
29 30 31 |
# File 'lib/overload/string.rb', line 29 def sanitize Sanitize.clean(self, :elements=>%w[span ul ol li b bold i italic u underline hr br p], :attributes=>{'span'=>['style']} ) end |
#span_green ⇒ Object
83 84 85 |
# File 'lib/overload/string.rb', line 83 def span_green %[<span style="color: #080;">#{self}</span>] end |
#span_red ⇒ Object
87 88 89 |
# File 'lib/overload/string.rb', line 87 def span_red %[<span style="color: #800;">#{self}</span>] end |
#starts_with?(prefix) ⇒ Boolean
70 71 72 |
# File 'lib/overload/string.rb', line 70 def starts_with? prefix prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix end |
#string_id ⇒ Object
46 47 48 |
# File 'lib/common/string_base.rb', line 46 def string_id StringBase.decode self end |
#to_a ⇒ Object
66 67 68 |
# File 'lib/overload/string.rb', line 66 def to_a self.split(/\s*,\s*/) end |
#to_html(opts = {}) ⇒ Object
convert escaped strings, remove scritpts
12 13 14 15 16 17 |
# File 'lib/overload/string.rb', line 12 def to_html opts={} value = self.gsub(/</, '<').gsub(/>/, '>').gsub(/&/,'&') value = value.gsub(/<script/,'<script') unless opts[:script] value = value.gsub(/<link/,'<link') unless opts[:link] value end |
#to_url ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/overload/string.rb', line 50 def to_url str_from = 'šđč枊ĐČĆŽäÄéeöÖüüÜß' str_to = 'sdcczSDCCZaAeeoOuuUs' str = self.downcase.gsub(/\s+/,'-').tr(str_from, str_to) # self.downcase.gsub(/\s+/,'-').tr(str_from, str_to).gsub(/[^\w\-]/,'') str.sub(/\.$/, '').gsub('&',' and ').gsub('.',' dot ').parameterize.gsub('-dot-','.').downcase[0, 50].sub(/[\.\-]$/,'') end |
#trim(len) ⇒ Object
19 20 21 22 23 |
# File 'lib/overload/string.rb', line 19 def trim(len) return self if self.length<len data = self.dup[0,len]+'…' data end |
#unescape ⇒ Object
100 101 102 |
# File 'lib/overload/string.rb', line 100 def unescape CGI::unescape self end |
#wrap(node_name, opts = {}) ⇒ Object
33 34 35 36 |
# File 'lib/overload/string.rb', line 33 def wrap node_name, opts={} return self unless node_name opts.tag(node_name, self) end |