2013-09-07 12:57:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
module Mutant
|
2013-09-07 17:12:03 -04:00
|
|
|
class Predicate
|
|
|
|
# Base class for predicates on object attributes
|
2013-09-07 12:57:06 -04:00
|
|
|
class Attribute < self
|
|
|
|
include Concord.new(:attribute_name, :expectation)
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Return value for object
|
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [Object]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def value(object)
|
|
|
|
object.public_send(attribute_name)
|
|
|
|
end
|
|
|
|
|
2013-09-07 17:12:03 -04:00
|
|
|
# Regexp based attribute predicate
|
2013-09-07 13:16:21 -04:00
|
|
|
class Regexp < self
|
|
|
|
|
|
|
|
# Test for match
|
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# if attribute value matches expectation
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def match?(object)
|
|
|
|
!!(expectation =~ value(object))
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Regexp
|
|
|
|
|
2013-09-07 17:12:03 -04:00
|
|
|
# Equality based attribute predicate
|
2013-09-07 12:57:06 -04:00
|
|
|
class Equality < self
|
|
|
|
|
|
|
|
# Test for match
|
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# if attribute value matches expectation
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def match?(object)
|
2013-09-07 13:16:21 -04:00
|
|
|
expectation.eql?(value(object))
|
2013-09-07 12:57:06 -04:00
|
|
|
end
|
|
|
|
|
2013-09-09 01:26:23 -04:00
|
|
|
PATTERN = /\A(code):([[:xdigit:]]{1,6})\z/.freeze
|
2013-09-07 12:57:06 -04:00
|
|
|
|
|
|
|
# Test if class handles string
|
|
|
|
#
|
|
|
|
# @param [String] notation
|
|
|
|
#
|
|
|
|
# @return [Filter]
|
|
|
|
# if notation matches pattern
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.handle(notation)
|
|
|
|
match = PATTERN.match(notation)
|
|
|
|
return unless match
|
|
|
|
new(match[1].to_sym, match[2])
|
|
|
|
end
|
|
|
|
|
2013-09-07 13:16:21 -04:00
|
|
|
end # Equality
|
2013-09-07 12:57:06 -04:00
|
|
|
end # Attribute
|
|
|
|
end # Filter
|
|
|
|
end # Mutant
|