Skip to content

Commit

Permalink
Merge branch 'remove_operation_duplication' of git://github.com/bishb…
Browse files Browse the repository at this point in the history
…oria/CukeSalad into remove_operation_duplication
  • Loading branch information
antonymarcano committed Sep 11, 2011
2 parents 4a6272a + 3f8d971 commit e55bb52
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
@@ -1,4 +1,5 @@
require 'calculator'
require 'calculator_operations'

module CalculatingIndividual

Expand All @@ -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
Expand All @@ -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

Expand Down
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions Examples/Calculator/lib/calculator_operations.rb
@@ -0,0 +1,4 @@
module CalculatorOperations
OPERATORS = { '+' => :plus, '-' => :minus, '=' => :equals }
OPERATIONS = { 'plus' => :+, 'minus' => :-, 'equals' => '=' }
end
29 changes: 10 additions & 19 deletions Examples/Calculator/lib/web_calculator.rb
Expand Up @@ -3,8 +3,7 @@

$:.unshift(File.dirname(__FILE__), '.')
require 'calculator'

Sinatra::Base.enable :inline_templates
require 'calculator_operations'

class WebCalculator < Sinatra::Base

Expand All @@ -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
Expand All @@ -25,6 +28,7 @@ def persist calc

def display_result_from calc
@display = calc.display
@operations = available_operations
erb :index
end

Expand All @@ -38,6 +42,7 @@ def selected_operator

def display_result_from_session
@display = session[:calc].display ||= 0
@operations = available_operations
erb :index
end

Expand Down Expand Up @@ -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
<h1>Calculator is Ready!</h1>
<%= yield %>

@@ index
<form method="POST" action="/">
<input type="text" disabled name="display" id="display" value="<%=@display %>" />
<input type="text" name="number" id="number" />
<input type="submit" name="operator" id="minus" text="-" value="minus" />
<input type="submit" name="operator" id="plus" text="+" value="plus" />
<input type="submit" name="operator" id="equals" text="=" value="equals" />
</form>
7 changes: 7 additions & 0 deletions Examples/Calculator/views/index.erb
@@ -0,0 +1,7 @@
<form method="POST" action="/">
<input type="text" disabled name="display" id="display" value="<%=@display %>" />
<input type="text" name="number" id="number" />
<% @operations.each do |key, value| %>
<input type="submit" name="operator" id="<%=key%>" text="<%=value%>" value="<%=key%>" />
<% end %>
</form>
2 changes: 2 additions & 0 deletions Examples/Calculator/views/layout.erb
@@ -0,0 +1,2 @@
<h1>Calculator is Ready!</h1>
<%= yield %>

0 comments on commit e55bb52

Please sign in to comment.