Class: Mutant::CLI::Classifier

Inherits:
Object
  • Object
show all
Includes:
AbstractType, Adamantium::Flat
Defined in:
lib/mutant/cli/classifier.rb,
lib/mutant/cli/classifier/method.rb,
lib/mutant/cli/classifier/namespace.rb

Overview

A classifier for input strings

Direct Known Subclasses

Method, Namespace

Defined Under Namespace

Classes: Method, Namespace

Constant Summary collapse

SCOPE_NAME_PATTERN =
/[A-Za-z][A-Za-z\d_]*/.freeze
SCOPE_OPERATOR =
'::'.freeze
CBASE_PATTERN =
/\A#{SCOPE_OPERATOR}/.freeze
METHOD_NAME_PATTERN =
Regexp.union(
  /[A-Za-z_][A-Za-z\d_]*[!?=]?/,
  *OPERATOR_METHODS.map(&:to_s)
).freeze
SCOPE_PATTERN =
/
  (?:#{SCOPE_OPERATOR})?#{SCOPE_NAME_PATTERN}
  (?:#{SCOPE_OPERATOR}#{SCOPE_NAME_PATTERN})*
/x.freeze
REGISTRY =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.constant_lookup(location) ⇒ Class|Module

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.

Return constant

Parameters:

  • location (String)

Returns:

  • (Class|Module)


46
47
48
49
50
51
# File 'lib/mutant/cli/classifier.rb', line 46

def self.constant_lookup(location)
  location.sub(CBASE_PATTERN, EMPTY_STRING).split(SCOPE_OPERATOR)
    .reduce(Object) do |parent, name|
    parent.const_get(name, nil)
  end
end

.run(cache, pattern) ⇒ Matcher

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.

Return matchers for input

Parameters:

  • cache (Cache)
  • pattern (String)

Returns:

  • (Matcher)

    if a classifier handles the input

Raises:

  • (RuntimeError)

    otherwise



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mutant/cli/classifier.rb', line 66

def self.run(cache, pattern)
  matches = find(pattern)
  case matches.length
  when 0
    raise Error, "No matcher handles: #{pattern.inspect}"
  when 1
    klass, match = matches.first
    klass.new(cache, match).matcher
  else
    raise Error, "More than one matcher found for: #{pattern.inspect}"
  end
end

Instance Method Details

#each(&block) ⇒ self, Enumerator<Subject>

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.

Enumerate subjects

Returns:

  • (self)

    if block given

  • (Enumerator<Subject>)

    otherwise



105
106
107
108
109
# File 'lib/mutant/cli/classifier.rb', line 105

def each(&block)
  return to_enum unless block_given?
  matcher.each(&block)
  self
end

#identifierString

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.

Return identifier

Returns:

  • (String)


117
118
119
# File 'lib/mutant/cli/classifier.rb', line 117

def identifier
  match.to_s
end