Merge branch 'remove_operation_duplication' of git://github.com/bishb…

`…oria/CukeSalad into remove_operation_duplication`
antonymarcano committed Sep 11, 2011
2 parents 4a6272a + 3f8d971 commit e55bb5204e88e88eeb920a59c8140160827342f1
 @@ -1,4 +1,5 @@ require 'calculator' +require 'calculator_operations' module CalculatingIndividual @@ -8,10 +9,7 @@ def role_preparation def switch_on_the_calculator @calculator = Calculator.new - @operate_with = { - plus: :+, - minus: :- - } + @operate_with = CalculatorOperations::OPERATIONS end def enter value @@ -22,7 +20,7 @@ def press next_operator if next_operator == :equals equals else - @calculator.get_ready_to @operate_with[next_operator] + @calculator.get_ready_to @operate_with[next_operator.to_s] end end
 @@ -1,14 +1,16 @@ +require 'calculator_operations' + module Calculations def follow_the_steps_for sum enter_numbers_and_operators_for sum end def enter_numbers_and_operators_for sum - operator = {"+" => :plus, "-" => :minus, "=" => :equals} + operators = CalculatorOperations::OPERATORS sum.each do | token | enter token.to_i if token =~ /\d+/ - press operator[token] if operator.include? token + press operators[token] if operators.include? token end end end
 @@ -0,0 +1,4 @@ +module CalculatorOperations + OPERATORS = { '+' => :plus, '-' => :minus, '=' => :equals } + OPERATIONS = { 'plus' => :+, 'minus' => :-, 'equals' => '=' } +end
 @@ -3,8 +3,7 @@ \$:.unshift(File.dirname(__FILE__), '.') require 'calculator' - -Sinatra::Base.enable :inline_templates +require 'calculator_operations' class WebCalculator < Sinatra::Base @@ -15,8 +14,12 @@ class WebCalculator < Sinatra::Base end helpers do - def operate_with - { 'plus' => :+, 'minus' => :-} + def available_operations + CalculatorOperations::OPERATIONS + end + + def operate_with operator + available_operations[operator] end def persist calc @@ -25,6 +28,7 @@ def persist calc def display_result_from calc @display = calc.display + @operations = available_operations erb :index end @@ -38,6 +42,7 @@ def selected_operator def display_result_from_session @display = session[:calc].display ||= 0 + @operations = available_operations erb :index end @@ -66,23 +71,9 @@ def load_calculator if equals_pressed? calculator.equals else - calculator.get_ready_to operate_with[selected_operator] + calculator.get_ready_to operate_with(selected_operator) end persist calculator display_result_from calculator end end - -__END__ -@@ layout -

-<%= yield %> - -@@ index -
- - - - - -
 @@ -0,0 +1,7 @@ +
+ + + <% @operations.each do |key, value| %> + + <% end %> +
 @@ -0,0 +1,2 @@ +