Class: Sparsify::Separator Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Deprecations

deprecate

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.

Parameters:

  • separator (String)

    single-character string



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.

Parameters:

  • separator_character (String)

    (see #initialize)



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.

Parameters:

  • pre_escaped_prefix (String)
  • new_part (String)
    • will be escaped before joining

Returns:

  • (String)


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

Parameters:

  • str (String)

Returns:

  • (Array<String>)


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