2013-01-21 14:08:30 -05:00
|
|
|
module Mutant
|
|
|
|
class CLI
|
|
|
|
# A classifier for input strings
|
|
|
|
class Classifier < Matcher
|
2013-06-27 16:18:07 -04:00
|
|
|
include AbstractType, Adamantium::Flat, Concord.new(:cache, :match)
|
|
|
|
|
|
|
|
include Equalizer.new(:identifier)
|
2013-01-21 14:08:30 -05:00
|
|
|
|
|
|
|
SCOPE_NAME_PATTERN = /[A-Za-z][A-Za-z_0-9]*/.freeze
|
2013-06-27 17:28:49 -04:00
|
|
|
OPERATOR_PATTERN = Regexp.union(*OPERATOR_METHODS.map(&:to_s)).freeze
|
2013-07-28 13:00:13 -04:00
|
|
|
METHOD_NAME_PATTERN =
|
|
|
|
/([_A-Za-z][A-Za-z0-9_]*[!?=]?|#{OPERATOR_PATTERN})/.freeze
|
|
|
|
SCOPE_PATTERN =
|
|
|
|
/(?:::)?#{SCOPE_NAME_PATTERN}(?:::#{SCOPE_NAME_PATTERN})*/.freeze
|
2013-07-21 16:19:41 -04:00
|
|
|
CBASE_PATTERN = /\A::/.freeze
|
|
|
|
SCOPE_OPERATOR = '::'.freeze
|
2013-07-28 15:16:45 -04:00
|
|
|
SINGLETON_PATTERN = /\A(#{SCOPE_PATTERN})\z/.freeze
|
2013-01-21 14:08:30 -05:00
|
|
|
|
2013-04-20 20:36:21 -04:00
|
|
|
REGISTRY = []
|
|
|
|
|
|
|
|
# Register classifier
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.register
|
|
|
|
REGISTRY << self
|
|
|
|
end
|
|
|
|
private_class_method :register
|
|
|
|
|
2013-04-17 23:31:21 -04:00
|
|
|
# Return constant
|
2013-01-21 14:08:30 -05:00
|
|
|
#
|
|
|
|
# @param [String] location
|
|
|
|
#
|
|
|
|
# @return [Class|Module]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.constant_lookup(location)
|
2013-07-28 13:00:13 -04:00
|
|
|
location
|
|
|
|
.gsub(CBASE_PATTERN, EMPTY_STRING)
|
|
|
|
.split(SCOPE_OPERATOR)
|
|
|
|
.reduce(Object) do |parent, name|
|
|
|
|
parent.const_get(name)
|
|
|
|
end
|
2013-01-21 14:08:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return matchers for input
|
|
|
|
#
|
|
|
|
# @return [Classifier]
|
|
|
|
# if a classifier handles the input
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-27 16:18:07 -04:00
|
|
|
def self.build(*arguments)
|
2013-04-20 20:36:21 -04:00
|
|
|
classifiers = REGISTRY.map do |descendant|
|
2013-06-27 16:18:07 -04:00
|
|
|
descendant.run(*arguments)
|
2013-01-21 14:08:30 -05:00
|
|
|
end.compact
|
|
|
|
|
|
|
|
raise if classifiers.length > 1
|
|
|
|
|
|
|
|
classifiers.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run classifier
|
|
|
|
#
|
2013-06-27 16:18:07 -04:00
|
|
|
# @param [Cache] cache
|
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
#
|
2013-01-21 14:08:30 -05:00
|
|
|
# @return [Classifier]
|
|
|
|
# if input is handled by classifier
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-27 16:18:07 -04:00
|
|
|
def self.run(cache, input)
|
2013-01-21 14:08:30 -05:00
|
|
|
match = self::REGEXP.match(input)
|
|
|
|
return unless match
|
|
|
|
|
2013-06-27 16:18:07 -04:00
|
|
|
new(cache, match)
|
2013-01-21 14:08:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# No protected_class_method in ruby :(
|
|
|
|
class << self; protected :run; end
|
|
|
|
|
|
|
|
# Enumerate subjects
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# if block given
|
|
|
|
#
|
|
|
|
# @return [Enumerator<Subject>]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def each(&block)
|
|
|
|
return to_enum unless block_given?
|
|
|
|
matcher.each(&block)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-06-27 16:18:07 -04:00
|
|
|
# Return identifier
|
2013-01-21 14:08:30 -05:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-27 16:18:07 -04:00
|
|
|
def identifier
|
2013-01-21 14:08:30 -05:00
|
|
|
match.to_s
|
|
|
|
end
|
2013-06-27 16:18:07 -04:00
|
|
|
memoize :identifier
|
2013-01-21 14:08:30 -05:00
|
|
|
|
|
|
|
# Return matcher
|
|
|
|
#
|
|
|
|
# @return [Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
abstract_method :matcher
|
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # Classifier
|
|
|
|
end # CLI
|
|
|
|
end # Mutant
|