Skip to content

Instantly share code, notes, and snippets.

@graemeboy
Last active April 9, 2016 19:20
Show Gist options
  • Save graemeboy/77ab7f4ec7bde4272a619734116d320b to your computer and use it in GitHub Desktop.
Save graemeboy/77ab7f4ec7bde4272a619734116d320b to your computer and use it in GitHub Desktop.
Example after refactor
class NewRental
def identifier
'a latest movie'
end
def price(days_rented)
NewRentalPrice.new(days_rented).amount
end
end
class ClassicRental
def identifier
'an oldie'
end
def price(days_rented)
ClassicalRentalPrice.new(days_rented).amount
end
end
class DocumentaryRental
def identifier
'a documentary'
end
def price(days_rented)
DocumentaryRentalPrice.new(days_rented).amount
end
end
class CustomerStatement
attr_reader :price, :days_rented, :rental_type
def initialize(price, days_rented, rental_type)
@price = price
@days_rented = days_rented
@rental_type = rental_type
end
def movie_identifier
rental_type.identifier
end
def to_s
"Thanks for renting #{movie_identifier} from Fayes! " \
"Your total charge is $#{price} for #{days_rented} days."
end
end
class Rental
attr_reader :days_rented, :movie_type
def initialize(days_rented, movie_type)
@days_rented = days_rented
@movie_type = movie_type
end
def statement
@statement = CustomerStatement.new(price, days_rented, rental_type)
end
def price
rental_type.price
end
private
def rental_type(movie_type)
if movie_type == 'documentary'
DocumentaryRental.new(days_rented)
elsif movie_type == 'new'
NewRental.new(days_rented)
elsif movie_type == 'classic'
ClassicRental.new(days_rented)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment