Class: String

Inherits:
Object show all
Defined in:
lib/overload/blank.rb,
lib/overload/string.rb,
lib/common/string_base.rb

Instance Method Summary collapse

Instance Method Details

#as_htmlObject

simple markdown



7
8
9
# File 'lib/overload/string.rb', line 7

def as_html
  self.gsub($/, '<br />')
end

#blank?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/overload/blank.rb', line 61

def blank?
  return true if self.length == 0

  !(self =~ /[^\s]/)
end

#constantizeObject



2
3
4
# File 'lib/overload/string.rb', line 2

def constantize
  Object.const_get('::'+self)
end

#css_to_hashObject



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

#decolorizeObject

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

Returns:

  • (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

#escapeObject



96
97
98
# File 'lib/overload/string.rb', line 96

def escape
  CGI::escape self
end

#firstObject



25
26
27
# File 'lib/overload/string.rb', line 25

def first
  self[0,1]
end

#fix_ut8Object



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

#parameterizeObject



46
47
48
# File 'lib/overload/string.rb', line 46

def parameterize
  self.downcase.gsub(/[^\w]+/,'-')
end

#parse_erbObject



42
43
44
# File 'lib/overload/string.rb', line 42

def parse_erb
  self.gsub(/<%=([^%]+)%>/) { eval $1; }
end

#sanitizeObject



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_greenObject



83
84
85
# File 'lib/overload/string.rb', line 83

def span_green
  %[<span style="color: #080;">#{self}</span>]
end

#span_redObject



87
88
89
# File 'lib/overload/string.rb', line 87

def span_red
  %[<span style="color: #800;">#{self}</span>]
end

#starts_with?(prefix) ⇒ Boolean

Returns:

  • (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_idObject



46
47
48
# File 'lib/common/string_base.rb', line 46

def string_id
  StringBase.decode self
end

#to_aObject



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(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/&amp;/,'&')
  value = value.gsub(/<script/,'&lt;script') unless opts[:script]
  value = value.gsub(/<link/,'&lt;link') unless opts[:link]
  value
end

#to_urlObject



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]+'&hellip;'
  data
end

#unescapeObject



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