From a8f4f427e8e5bb2261c9dbb78e36f9b9fe1b44cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mota?= Date: Thu, 2 May 2013 21:02:49 +0100 Subject: [PATCH] Form Template Method. --- form-template-method/lib/after.rb | 28 ++++++++++++++++++++++++++++ form-template-method/lib/before.rb | 20 ++++++++++++++++++++ form-template-method/test/test.rb | 15 +++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/form-template-method/lib/after.rb b/form-template-method/lib/after.rb index e69de29..ed75630 100644 --- a/form-template-method/lib/after.rb +++ b/form-template-method/lib/after.rb @@ -0,0 +1,28 @@ +class Ticket + attr_reader :price + + def initialize + @price = 2.0 + end + + def price *args + @price * discount + end + + def discount + 1 + end + +end + +class SeniorTicket < Ticket + def discount + 0.75 + end +end + +class JuniorTicket < Ticket + def discount + 0.5 + end +end diff --git a/form-template-method/lib/before.rb b/form-template-method/lib/before.rb index e69de29..ecb30c0 100644 --- a/form-template-method/lib/before.rb +++ b/form-template-method/lib/before.rb @@ -0,0 +1,20 @@ +class Ticket + attr_reader :price + + def initialize + @price = 2.0 + end + +end + +class SeniorTicket < Ticket + def price + @price * 0.75 + end +end + +class JuniorTicket < Ticket + def price + @price * 0.5 + end +end diff --git a/form-template-method/test/test.rb b/form-template-method/test/test.rb index a8f6974..355d826 100644 --- a/form-template-method/test/test.rb +++ b/form-template-method/test/test.rb @@ -3,3 +3,18 @@ require 'before' if ENV["BEFORE"] require 'after' unless ENV["BEFORE"] + +describe Ticket do + it "has a calculated price" do + Ticket.new.price.must_equal 2.0 + end + + it "costs less if a senior" do + SeniorTicket.new.price.must_equal 1.5 + end + + it "costs less if a junior" do + JuniorTicket.new.price.must_equal 1.0 + end + +end