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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vex/base/string/string_ext.rb', line 86

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vex/base/string/string_ext.rb', line 55

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)


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

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

#downcase?Boolean

Returns:

  • (Boolean)


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

def downcase?
  self == downcase
end

#to_uriObject



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

def to_uri
  URI.parse(self)
end

#truncate(*args) ⇒ Object

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vex/base/string/string_ext.rb', line 36

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



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

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

#unhtmlObject



29
30
31
32
33
# File 'lib/vex/base/string/string_ext.rb', line 29

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

#upcase?Boolean

Returns:

  • (Boolean)


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

def upcase?
  self == upcase
end

#uri?Boolean

Returns:

  • (Boolean)


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

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

#word_wrap(line_len = 100) ⇒ Object



81
82
83
84
# File 'lib/vex/base/string/string_ext.rb', line 81

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