Class: ReeString::TruncateWords

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb

Constant Summary collapse

DEFAULT_OMISSION =
"..."

Instance Method Summary collapse

Instance Method Details

#call(str, words_count, **opts) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb', line 34

def call(str, words_count, **opts)
  str = str.dup
  sep = opts[:separator] || /\s+/
  sep = Regexp.escape(sep.to_s) unless Regexp === sep

  if str =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
    $1 + (opts[:omission] || "...")
  else
    str
  end
end