Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…oria/CukeSalad into remove_operation_duplication
  • Loading branch information...
commit e55bb5204e88e88eeb920a59c8140160827342f1 2 parents 4a6272a + 3f8d971
@antonymarcano antonymarcano authored
View
8 Examples/Calculator/features/lib/default/roles/calculating_individual.rb
@@ -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
View
6 Examples/Calculator/features/lib/default/tasks/reference/calculations.rb
@@ -1,3 +1,5 @@
+require 'calculator_operations'
+
module Calculations
def follow_the_steps_for sum
@@ -5,10 +7,10 @@ def follow_the_steps_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
View
4 Examples/Calculator/lib/calculator_operations.rb
@@ -0,0 +1,4 @@
+module CalculatorOperations
+ OPERATORS = { '+' => :plus, '-' => :minus, '=' => :equals }
+ OPERATIONS = { 'plus' => :+, 'minus' => :-, 'equals' => '=' }
+end
View
29 Examples/Calculator/lib/web_calculator.rb
@@ -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
-<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>
View
7 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>
View
2  Examples/Calculator/views/layout.erb
@@ -0,0 +1,2 @@
+<h1>Calculator is Ready!</h1>
+<%= yield %>
Please sign in to comment.
Something went wrong with that request. Please try again.