Skip to content

Commit

Permalink
Form Template Method.
Browse files Browse the repository at this point in the history
  • Loading branch information
josemotanet committed Jun 4, 2013
1 parent 8017008 commit a8f4f42
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
28 changes: 28 additions & 0 deletions form-template-method/lib/after.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions form-template-method/lib/before.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions form-template-method/test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a8f4f42

Please sign in to comment.