Skip to content

Instantly share code, notes, and snippets.

@kchens
Created April 7, 2015 04:57
Show Gist options
  • Save kchens/480410262bf3903e6514 to your computer and use it in GitHub Desktop.
Save kchens/480410262bf3903e6514 to your computer and use it in GitHub Desktop.
POODR: Chapter 7 - Sharing Role Behavior With Modules
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Writing the Concrete Code
# 1 -------------------Bad
class Schedule
def scheduled?(schedulable, start_date, end_date)
puts "This #{schedulable.class} " +
"is not scheduled\n" +
" between #{start_date} and #{end_date}"
end
end
class Bicycle
attr_reader :schedule, :size, :chain, :tire_size
# Inject the Schedule and provide a default
def initialize(args={})
@schedule = args[:schedule] || Schedule.new
# ...
end
# Return true if this bicycle is available
# during this (now Bicycle specific) interval.
def schedulable?(start_date, end_date)
!scheduled?(start_date - lead_days, end_date)
end
# Return the number of lead_days before a bicycle
# can be scheduled.
def lead_days
1
end
# ...
end
require 'date'
starting = Date.parse('2015/09/04')
ending = Date.parse('2015/09/10')
b = Bicycle.new
b.schedulable?(starting, ending)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Extracting the Abstraction
# 2 -------------------Good
module Schedulable
attr_writer :schedule
def schedule
@schedule ||= ::Schedule.new
end
def schedulable?(start_date, end_date)
!scheduled?(start_date - lead_days, end_date)
end
def scheduled?(start_date, end_date)
schedule.scheduled?(self, start_date, end_date)
end
# includers may override
def lead_days
0
end
end
class Bicycle
include Schedulable
def lead_days
1
end
# ...
end
require 'date'
starting = Date.parse('2015/09/04')
ending = Date.parse('2015/09/10')
b = Bicycle.new
b.schedulable?(starting, ending)
class Vehicle
include Schedulable
def lead_days
3
end
# ...
end
class Mechanic
include Schedulable
def lead_days
4
end
# ...
end
v = Vehicle.new
v.schedulable?(starting, ending)
# This Vehicle is not scheduled
# between 2015-09-01 and 2015-09-10
# => true
m = Mechanic.new
m.schedulable?(starting, ending)
# This Mechanic is not scheduled
# between 2015-02-29 and 2015-09-10
# => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment