Module: Trenni::Formatters::TruncatedText

Defined in:
lib/trenni/formatters/truncated_text.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.truncate_text(text, truncate_at, omission: nil, separator: nil, **options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/trenni/formatters/truncated_text.rb', line 37

def self.truncate_text(text, truncate_at, omission: nil, separator: nil, **options)
	return text.dup unless text.length > truncate_at
	
	omission ||= '...'
	
	length_with_room_for_omission = truncate_at - omission.length
	
	stop = nil
	
	if separator
		stop = text.rindex(separator, length_with_room_for_omission)
	end
	
	stop ||= length_with_room_for_omission
	
	"#{text[0...stop]}#{omission}"
end

Instance Method Details

#truncated_text(content, length: 30, **options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/trenni/formatters/truncated_text.rb', line 29

def truncated_text(content, length: 30, **options)
	if content
		content = TruncatedText.truncate_text(content, length, **options)
		
		return self.format(content)
	end
end