Skip to content

Commit

Permalink
a little refactoring of custom operator matcher registration and lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Jan 7, 2009
1 parent 62a41f3 commit e944831
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/spec/matchers/operator_matcher.rb
Expand Up @@ -2,16 +2,18 @@ module Spec
module Matchers

class OperatorMatcher
@operator_registry = {}
class << self
def registry
@registry ||= Hash.new {|h,k| h[k] = {}}
end

def self.register(klass, operator, matcher)
@operator_registry[klass] ||= {}
@operator_registry[klass][operator] = matcher
end
def register(klass, operator, matcher)
registry[klass][operator] = matcher
end

def self.get(klass, operator)
return @operator_registry[klass][operator] if @operator_registry[klass]
nil
def get(klass, operator)
registry[klass][operator]
end
end

def initialize(actual)
Expand All @@ -21,7 +23,7 @@ def initialize(actual)
def self.use_custom_matcher_or_delegate(operator)
define_method(operator) do |expected|
if matcher = OperatorMatcher.get(@actual.class, operator)
return @actual.send(::Spec::Matchers.last_should, matcher.new(expected))
@actual.send(::Spec::Matchers.last_should, matcher.new(expected))
else
::Spec::Matchers.last_matcher = self
@operator, @expected = operator, expected
Expand Down

0 comments on commit e944831

Please sign in to comment.