module Adhearsion
  module MenuDSL
    class MatchCalculator

      class << self

        def build_with_pattern(pattern, match_payload, &block)
          class_for_pattern_type(pattern.class.name).new(pattern, match_payload, &block)
        end

        def inherited(klass)
          subclasses << klass
        end

        private

        def class_for_pattern_type(pattern_type)
          sought_class_name = "Adhearsion::MenuDSL::#{pattern_type.camelize}MatchCalculator"
          subclasses.find { |klass| klass.name == sought_class_name }
        end

        def subclasses
          @@subclasses ||= []
        end

      end

      attr_reader :pattern, :match_payload, :block

      def initialize(pattern, match_payload, &block)
        @pattern, @match_payload = pattern, match_payload
        @block = block
      end

      protected

      def new_calculated_match(options)
        CalculatedMatch.new({:pattern => pattern, :match_payload => match_payload, :block => block}.merge(options))
      end

      def coerce_to_numeric(victim)
        victim.kind_of?(Numeric) ? victim : (victim.to_s =~ /^\d+$/ ? victim.to_s.to_i : nil )
      end

    end # class MatchCalculator

    class RangeMatchCalculator < MatchCalculator; end
    class FixnumMatchCalculator < MatchCalculator; end
    class StringMatchCalculator < MatchCalculator; end
  end
end

module Adhearsion
  module MenuDSL
    class MatchCalculator

      class << self

        def build_with_pattern(pattern, match_payload, &block)
          class_for_pattern(pattern).new(pattern, match_payload, &block)
        end

        private

        def class_for_pattern(pattern)
          MenuDSL.const_get "#{pattern.class.name.camelize}MatchCalculator"
        end
      end

      attr_reader :pattern, :match_payload, :block

      def initialize(pattern, match_payload, &block)
        @pattern, @match_payload = pattern, match_payload
        @block = block
      end

      protected

      def new_calculated_match(options)
        CalculatedMatch.new({:pattern => pattern, :match_payload => match_payload, :block => block}.merge(options))
      end

      def coerce_to_numeric(victim)
        victim.kind_of?(Numeric) ? victim : (victim.to_s =~ /^\d+$/ ? victim.to_s.to_i : nil )
      end

    end # class MatchCalculator
  end
end

Simplify MatchCalculator child loading, and avoid dodgy interaction with autoloading
[CS] Simplify MatchCalculator child loading, and avoid dodgy interaction with autoloading