Class: Toys::ToolNameSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/tool_name_splitter.rb

Overview

Splits tool names into words, according to a set of delimiter characters.

A tool name is a series of words. On the command line, and in various places in the DSL, a name can be written as a single string with its words separated by delimiters. Whitespace is always a delimiter; additional characters can be configured when a splitter is created.

A splitter is immutable, and can be shared by any number of objects that need to interpret tool names the same way.

Constant Summary collapse

DEFAULT =

A splitter that recognizes only whitespace as a delimiter. This is the splitter used when no other is configured.

new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extra_delimiters = "") ⇒ ToolNameSplitter

Create a splitter.

Parameters:

  • extra_delimiters (String) (defaults to: "")

    A string containing characters that can function as delimiters in a tool name, in addition to whitespace. Defaults to empty. Allowed characters are period, colon, and slash.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/toys/tool_name_splitter.rb', line 23

def initialize(extra_delimiters = "")
  unless %r{^[[:space:]./:]*$}.match?(extra_delimiters)
    raise ::ArgumentError, "Illegal delimiters in #{extra_delimiters.inspect}"
  end
  @extra_delimiters = -extra_delimiters
  # Whitespace is always a delimiter, so any whitespace in the extra
  # delimiters is dropped here rather than duplicated in the character
  # class, which Ruby warns about.
  chars = ::Regexp.escape(extra_delimiters.chars.uniq.grep_v(/[[:space:]]/).join)
  @delimiters = ::Regexp.new("[[:space:]#{chars}]")
  @trailing_word = ::Regexp.new("\\A(.+#{@delimiters})(.*)\\z", ::Regexp::MULTILINE)
  freeze
end

Instance Attribute Details

#extra_delimitersString (readonly)

The extra delimiters this splitter was created with, as given. Whitespace is a delimiter whether or not it appears here.

Returns:

  • (String)


43
44
45
# File 'lib/toys/tool_name_splitter.rb', line 43

def extra_delimiters
  @extra_delimiters
end

Instance Method Details

#inspectString

Returns a description of this splitter's delimiters.

Returns:

  • (String)

    a description of this splitter's delimiters



82
83
84
# File 'lib/toys/tool_name_splitter.rb', line 82

def inspect
  "#<Toys::ToolNameSplitter extra_delimiters=#{@extra_delimiters.inspect}>"
end

#split(name) ⇒ Array<String>

Splits the given tool name into words. You may pass either an array, whose elements are copied as strings without being split further, or a single string or symbol possibly delimited by this splitter's delimiters. Always returns a new array of strings.

Parameters:

  • name (String, Symbol, Array<String,Symbol>)

    The name to split.

Returns:

  • (Array<String>)


54
55
56
57
# File 'lib/toys/tool_name_splitter.rb', line 54

def split(name)
  return name.map(&:to_s) if name.is_a?(::Array)
  name.to_s.split(@delimiters)
end

#split_partial(str) ⇒ Array(String,String)

Splits a partially typed name, such as a fragment being completed, into the portion that names a path and the trailing partial word.

Returns a two-element array. The first element is the leading portion of the string through its final delimiter, or the empty string if there is none; pass it to #split to get the path words. The second element is the text following that delimiter.

A delimiter is recognized as a separator only if at least one character precedes it, so a string that begins with a delimiter is not split.

Parameters:

  • str (String)

    The partial name to split.

Returns:

  • (Array(String,String))


74
75
76
77
# File 'lib/toys/tool_name_splitter.rb', line 74

def split_partial(str)
  match = @trailing_word.match(str)
  match ? [match[1], match[2]] : ["", str]
end