Class: Strings::Truncation
- Inherits:
-
Object
- Object
- Strings::Truncation
- Extended by:
- Forwardable
- Defined in:
- lib/strings/truncation.rb,
lib/strings/truncation/version.rb,
lib/strings/truncation/extensions.rb,
lib/strings/truncation/configuration.rb
Defined Under Namespace
Modules: Extensions Classes: Configuration, Error
Constant Summary collapse
- ANSI_REGEXP =
/#{Strings::ANSI::ANSI_MATCHER}/.freeze
- RESET_REGEXP =
/#{Regexp.escape(Strings::ANSI::RESET)}/.freeze
- END_REGEXP =
/\A(#{Strings::ANSI::ANSI_MATCHER})*\z/.freeze
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.__instance__ ⇒ Object
private
Global instance.
Instance Method Summary collapse
-
#configuration ⇒ Object
Access configuration.
-
#configure(**options) {|Configuration| ... } ⇒ Object
Configure truncation.
-
#initialize(**options) ⇒ Truncation
constructor
Create a Strings::Truncation instance.
-
#truncate(text, truncate_at = configuration.length, length: nil, position: configuration.position, separator: configuration.separator, omission: configuration.omission) ⇒ Object
Truncate a text at a given length (defualts to 30).
Constructor Details
#initialize(**options) ⇒ Truncation
Create a Strings::Truncation instance
47 48 49 |
# File 'lib/strings/truncation.rb', line 47 def initialize(**) configuration.update(**) end |
Class Method Details
.__instance__ ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Global instance
22 23 24 |
# File 'lib/strings/truncation.rb', line 22 def self.__instance__ @__instance__ ||= Truncation.new end |
Instance Method Details
#configuration ⇒ Object
Access configuration
54 55 56 |
# File 'lib/strings/truncation.rb', line 54 def configuration @configuration ||= Configuration.new end |
#configure(**options) {|Configuration| ... } ⇒ Object
Configure truncation
76 77 78 79 80 81 82 |
# File 'lib/strings/truncation.rb', line 76 def configure(**) if block_given? yield configuration else configuration.update(**) end end |
#truncate(text, truncate_at = configuration.length, length: nil, position: configuration.position, separator: configuration.separator, omission: configuration.omission) ⇒ Object
Truncate a text at a given length (defualts to 30)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/strings/truncation.rb', line 117 def truncate(text, truncate_at = configuration.length, length: nil, position: configuration.position, separator: configuration.separator, omission: configuration.omission) truncate_at = length if length return text if truncate_at.nil? || text.bytesize <= truncate_at.to_i return "" if truncate_at.zero? separator = Regexp.new(separator) if separator case position when :start truncate_start(text, truncate_at, omission, separator) when :middle truncate_middle(text, truncate_at, omission, separator) when :ends truncate_ends(text, truncate_at, omission, separator) when :end truncate_from(0, text, truncate_at, omission, separator) when Numeric truncate_from(position, text, truncate_at, omission, separator) else raise Error, "unsupported position: #{position.inspect}" end end |