Module: StringExt
- Defined in:
- lib/vex/base/string/string_ext.rb
Defined Under Namespace
Modules: Etest
Class Method Summary collapse
Instance Method Summary collapse
- #_truncate(opts) ⇒ Object
- #constantize? ⇒ Boolean
-
#truncate(*args) ⇒ Object
truncate :length => 30, :omission => “…”.
- #truncate!(opts = {}) ⇒ Object
- #unhtml ⇒ Object
- #uri? ⇒ Boolean
- #word_wrap(line_len = 100) ⇒ Object
Class Method Details
.word_wrap(line, line_len) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vex/base/string/string_ext.rb', line 69 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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vex/base/string/string_ext.rb', line 38 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
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 |
#truncate(*args) ⇒ Object
truncate :length => 30, :omission => “…”
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vex/base/string/string_ext.rb', line 19 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
60 61 62 |
# File 'lib/vex/base/string/string_ext.rb', line 60 def truncate!(opts = {}) replace truncate(opts) end |
#unhtml ⇒ Object
12 13 14 15 16 |
# File 'lib/vex/base/string/string_ext.rb', line 12 def unhtml return self if blank? s = Sanitize.clean(self) HTMLEntities.new.decode(s) end |
#uri? ⇒ Boolean
8 9 10 |
# File 'lib/vex/base/string/string_ext.rb', line 8 def uri? !!(self =~ /^[a-z][a-z]+:/) end |