-
-
Save esdras/776485 to your computer and use it in GitHub Desktop.
Add schedule capabilities to a resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ice_cube' | |
require 'active_support/all' | |
include IceCube | |
class Resource | |
attr_accessor :availability | |
def available_since(t = nil) | |
@available_since = t if t | |
@available_since ||= Time.now.beginning_of_year | |
end | |
def always_available! | |
@availability = Schedule.new(available_since, :duration => 1.day) | |
@availability.add_recurrence_rule(Rule.daily.until(Time.now.end_of_year)) | |
end | |
def schedules | |
@schedules ||= [] | |
end | |
def available?(time = Time.now) | |
return true if schedules.empty? | |
availability.occurring_at?(time) && !schedules.any? {|s| s.occurring_at?(time) } | |
end | |
def weekly_occupied_on(day, hour, minute, options = {}) | |
sc = Schedule.new(available_since + hour.hours + minute.minutes, options) | |
sc.add_recurrence_rule(Rule.weekly.day(day).until(Time.now.end_of_year)) | |
schedules << sc | |
end | |
def allocations | |
schedules.map(&:all_occurrences).flatten | |
end | |
end | |
cls = Resource.new | |
cls.always_available! | |
cls.weekly_occupied_on(:monday, 13, 30, :duration => 2.hours) | |
puts cls.allocations | |
t = Time.new(2010, 10, 4, 15, 31, 0) | |
puts t | |
puts cls.available?(t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment