Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
@stomar stomar
127 lines (99 sloc) 2.344 kb
############## Page 148 ##############
class Schedule
def scheduled?(schedulable, start_date, end_date)
puts "This #{schedulable.class} " +
"is not scheduled\n" +
" between #{start_date} and #{end_date}"
false
end
end
############## Page 149 ##############
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 schedule's answer
def scheduled?(start_date, end_date)
schedule.scheduled?(self, start_date, 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)
# This Bicycle is not scheduled
# between 2015-09-03 and 2015-09-10
# => true
############## Page 150 ##############
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
############## Page 151 ##############
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)
# This Bicycle is not scheduled
# between 2015-09-03 and 2015-09-10
# => true
############## Page 152 ##############
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-08-31 and 2015-09-10
# => true
Jump to Line
Something went wrong with that request. Please try again.