Sha256: cc9309a39294bf83b4ddefee78dadbcce4414346d6effb55b24ef925be17dfb1

Contents?: true

Size: 957 Bytes

Versions: 4

Compression:

Stored size: 957 Bytes

Contents

# String additions
class String
  # Truncate method from ActiveSupport.
  # @param truncate_at [Fixnum] number of characters to truncate after
  # @param options [Hash] optional options hash
  # @option options separator [String] truncate text only at a certain separator strings
  # @option options omission [String] string to add at the end to endicated truncated text. Defaults to '...'
  # @return [String] truncated string
  def q_truncate(truncate_at, options = {})
    return dup unless length > truncate_at

    # Default omission to '...'
    options[:omission] ||= '...'

    # Account for the omission string in the truncated length
    truncate_at -= options[:omission].length

    # Calculate end index
    stop = if options[:separator]
      rindex(options[:separator], truncate_at) || truncate_at
    else
      truncate_at
    end

    # Return the trucnated string plus the omission string
    self[0...stop] + options[:omission]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
quesadilla-0.1.3 lib/quesadilla/core_ext/string.rb
quesadilla-0.1.2 lib/quesadilla/core_ext/string.rb
quesadilla-0.1.1 lib/quesadilla/core_ext/string.rb
quesadilla-0.1.0 lib/quesadilla/core_ext/string.rb