Skip to content

Instantly share code, notes, and snippets.

@esdras
Forked from anonymous/gist:776484
Created January 12, 2011 17:19
Show Gist options
  • Save esdras/776485 to your computer and use it in GitHub Desktop.
Save esdras/776485 to your computer and use it in GitHub Desktop.
Add schedule capabilities to a resource
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