Commit bf24235

mo khan <mo@mokhan.ca>
2014-04-26 15:47:36
refactor to template method. (blah)
1 parent 292339b
Changed files (4)
lib/mathy/operations/addition.rb
@@ -1,23 +1,10 @@
 module Mathy
   module Operations
-    class Addition
-      attr_reader :key
-
+    class Addition < Operation
       def initialize(verification)
-        @verifier = verification
-        @key = "+"
-      end
-
-      def play_turn(operands)
-        @verifier.check_answer("#{operands.join(" + ")} = ", calculate(operands))
+        super(verification, "+")
       end
 
-      def matches?(other_key)
-        key == other_key
-      end
-
-      private
-
       def calculate(operands)
         operands.inject(0) do |result, x|
           result + x
lib/mathy/operations/operation.rb
@@ -0,0 +1,20 @@
+module Mathy
+  module Operations
+    class Operation
+      attr_reader :key
+
+      def initialize(verification, key)
+        @verifier = verification
+        @key = key
+      end
+
+      def play_turn(operands)
+        @verifier.check_answer("#{operands.join(" #{key} ")} = ", calculate(operands))
+      end
+
+      def matches?(other_key)
+        key == other_key
+      end
+    end
+  end
+end
lib/mathy/operations/subtraction.rb
@@ -1,23 +1,10 @@
 module Mathy
   module Operations
-    class Subtraction
-      attr_reader :key
-
+    class Subtraction < Operation
       def initialize(verification)
-        @verifier = verification
-        @key = "-"
-      end
-
-      def play_turn(operands)
-        @verifier.check_answer("#{operands.join(" #{key} ")} = ", calculate(operands))
+        super(verification, "-")
       end
 
-      def matches?(other_key)
-        key == other_key
-      end
-
-      private
-
       def calculate(operands)
         operands.inject(0) do |result, x|
           result + x
lib/mathy.rb
@@ -2,6 +2,7 @@ require "mathy/version"
 require 'mathy/console'
 require 'mathy/verification'
 require 'mathy/game'
+require 'mathy/operations/operation'
 require 'mathy/operations/addition'
 require 'mathy/operations/subtraction'