Module: StringExt

Defined in:
lib/vex/base/string/string_ext.rb

Defined Under Namespace

Modules: Etest

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.word_wrap(line, line_len) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vex/base/string/string_ext.rb', line 81

def self.word_wrap(line, line_len)
  r = []
  while line.length > line_len
    # find last space in the first line_len characters. Failing that we
    # take the *first* (sic!) space in the entire line.
    firstline = line[0..line_len]
    space_idx = firstline.rindex(/\s/) || line.index(/\s/, line_len)

    if !space_idx
      r << line
      line = ""
    else
      r << line[0...space_idx]
      line = line[space_idx+1..-1]
    end
  end

  r << line unless line.blank?
  r.join("\n")
end

Instance Method Details

#_truncate(opts) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vex/base/string/string_ext.rb', line 50

def _truncate(opts)
  opts = { :length => opts, :omission => "" } unless opts.is_a?(Hash)
  max_length = opts[:length] || 30
  omission = opts[:omission] || ""

  #
  # Treat multibytes differently
  if respond_to?(:mb_chars)
    l = max_length - omission.mb_chars.length
    if mb_chars.length > max_length
      return (mb_chars[0...l] + omission).to_s
    end
  else
    l = max_length - omission.length
    if length > max_length
      return self[0...l] + omission
    end
  end

  self
end

#constantize?Boolean

Returns:

  • (Boolean)

2
3
4
5
6
# File 'lib/vex/base/string/string_ext.rb', line 2

def constantize?
  constantize
rescue LoadError, NameError
  STDERR.puts $!.to_s
end

#downcase?Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/vex/base/string/string_ext.rb', line 12

def downcase?
  self == downcase
end

#to_uriObject


20
21
22
# File 'lib/vex/base/string/string_ext.rb', line 20

def to_uri
  URI.parse(self)
end

#truncate(*args) ⇒ Object

truncate :length => 30, :omission => “…”


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vex/base/string/string_ext.rb', line 31

def truncate(*args)
  opts = if args.length == 1 && args.first.is_a?(Hash)
    args.first
  else
    args.inject({}) do |hash, arg|
      key = case arg
      when Fixnum then :length
      when String then :omission
      end
    
      invalid_argument!(arg) if key.nil? || hash[key]
    
      hash.update key => arg
    end
  end
  
  _truncate(opts)
end

#truncate!(opts = {}) ⇒ Object


72
73
74
# File 'lib/vex/base/string/string_ext.rb', line 72

def truncate!(opts = {})
  replace truncate(opts)
end

#unhtmlObject


24
25
26
27
28
# File 'lib/vex/base/string/string_ext.rb', line 24

def unhtml
  return self if blank?
  s = Sanitize.clean(self)
  HTMLEntities.new.decode(s)
end

#upcase?Boolean

Returns:

  • (Boolean)

8
9
10
# File 'lib/vex/base/string/string_ext.rb', line 8

def upcase?
  self == upcase
end

#uri?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/vex/base/string/string_ext.rb', line 16

def uri?
  !!(self =~ /^[a-z][a-z]+:/)
end

#word_wrap(line_len = 100) ⇒ Object


76
77
78
79
# File 'lib/vex/base/string/string_ext.rb', line 76

def word_wrap(line_len=100)
  lines = split("\n")
  lines.map { |line| StringExt.word_wrap(line, line_len) }.join("\n")
end