Class: ReeString::Truncate

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

Constant Summary collapse

DEFAULT_OMISSION =
"..."

Instance Method Summary collapse

Instance Method Details

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate.rb', line 38

def call(str, truncate_at, **opts)
  return str.dup unless str.length > truncate_at

  omission = opts[:omission] || DEFAULT_OMISSION
  length_with_room_for_omission = truncate_at - omission.length
  
  stop = if opts[:separator]
    str.rindex(opts[:separator], length_with_room_for_omission) || length_with_room_for_omission
  else
    length_with_room_for_omission
  end

  +"#{str[0, stop]}#{omission}"
end