Class: Sparsify::Separator Private
- Inherits:
-
Object
- Object
- Sparsify::Separator
- Includes:
- Deprecations
- Defined in:
- lib/sparsify/separator.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Container for a separator and functionality for splitting & joining with proper escaping.
Class Method Summary collapse
-
.[](separator_character) ⇒ Object
private
Returns a memoized Separator object for the given separator_character Evicts a member at random if memoized pool grows beyond 100.
Instance Method Summary collapse
-
#initialize(separator) ⇒ Separator
constructor
private
A new instance of Separator.
-
#join(pre_escaped_prefix, new_part) ⇒ String
private
Joins a pre-escaped string with a not-yet escaped string on our separator, escaping the new part before joining.
-
#split(str) ⇒ Array<String>
private
Splits a string by our separator into non-escaped parts.
Methods included from Deprecations
Constructor Details
#initialize(separator) ⇒ Separator
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.
Returns a new instance of Separator.
26 27 28 29 30 31 32 33 |
# File 'lib/sparsify/separator.rb', line 26 def initialize(separator) unless separator.kind_of?(String) && separator.size > 0 fail ArgumentError, "separator must be a non-empty String " + "got #{separator.inspect}" end deprecate('multi-character separator', '2.0') unless separator.size == 1 @separator = separator end |
Class Method Details
.[](separator_character) ⇒ 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.
Returns a memoized Separator object for the given separator_character Evicts a member at random if memoized pool grows beyond 100.
18 19 20 21 22 23 |
# File 'lib/sparsify/separator.rb', line 18 def self.[](separator_character) @separators[separator_character] ||= begin @separators.delete(@separators.keys.sample) if @separators.size > 100 new(separator_character) end end |
Instance Method Details
#join(pre_escaped_prefix, new_part) ⇒ String
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.
Joins a pre-escaped string with a not-yet escaped string on our separator, escaping the new part before joining.
40 41 42 |
# File 'lib/sparsify/separator.rb', line 40 def join(pre_escaped_prefix, new_part) [pre_escaped_prefix, escape(new_part)].compact.join(@separator) end |
#split(str) ⇒ Array<String>
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.
Splits a string by our separator into non-escaped parts
47 48 49 50 51 52 53 54 55 |
# File 'lib/sparsify/separator.rb', line 47 def split(str) @unescaped_separator ||= /(?<!\\)(#{Regexp.escape(@separator)})/ # String#split(<Regexp>) on non zero-width matches yields the match # as the even entries in the array. parts = str.split(@unescaped_separator).each_slice(2).map(&:first) parts.map do |part| unescape(part) end end |