This class holds a set of limits. Each limit can be created individually and must have unique name. The Limit objects are created when an upper or lower limit is set. All upper or lower limits can be tested with a single function call.
Create a new Limits object. If an argument is passed, it acts as a copy contructor.
# File lib/Limits.rb, line 122 122: def initialize(limits = nil) 123: if limits.nil? 124: # Normal initialization 125: @limits = [] 126: @project = nil 127: else 128: # Deep copy content from other instance. 129: @limits = [] 130: limits.limits.each do |name, limit| 131: @limits << limit.copy 132: end 133: @project = limits.project 134: end 135: end
This function increases the counters for all limits for a specific interval identified by date.
# File lib/Limits.rb, line 169 169: def inc(date, resource = nil) 170: @limits.each do |limit| 171: limit.inc(date, resource) 172: end 173: end
Check all upper limits and return true if none is exceeded. If a date is specified only the counters for that specific period are tested. Otherwise all periods are tested. If resource is nil, only non-resource-specific counters are checked, otherwise only the ones that match the resource.
# File lib/Limits.rb, line 180 180: def ok?(date = nil, upper = true, resource = nil) 181: @limits.each do |limit| 182: return false unless limit.ok?(date, upper, resource) 183: end 184: true 185: end
Reset all counter for all limits.
# File lib/Limits.rb, line 147 147: def reset 148: @limits.each { |limit| limit.reset } 149: end
Call this function to create or change a limit. The limit is uniquely identified by the combination of name, interval and resource. value is the new limit value (in time slots). In case the interval is nil, the complete project time frame is used.
# File lib/Limits.rb, line 155 155: def setLimit(name, value, interval = nil, resource = nil) 156: iv = interval || Interval.new(@project['start'], @project['end']) 157: # If we have already a limit for the name + interval + resource 158: # combination, we delete it first. 159: @limits.delete_if do |l| 160: l.name == name && l.interval.start == iv.start && 161: l.interval.end == iv.end && l.resource == resource 162: end 163: 164: newLimit(name, value, iv, resource) 165: end
The objects need access to some project specific data like the project period.
# File lib/Limits.rb, line 139 139: def setProject(project) 140: unless @limits.empty? 141: raise "Cannot change project after limits have been set!" 142: end 143: @project = project 144: end
This function creates a new Limit identified by name. In case name is none of the predefined intervals (e. g. dailymax, weeklymin, monthlymax) a the whole interval is used for the period length.
# File lib/Limits.rb, line 192 192: def newLimit(name, value, interval, resource) 193: # The known intervals are aligned to start at their respective start. 194: interval.start = interval.start.midnight 195: interval.end = interval.end.midnight 196: case name 197: when 'dailymax' 198: period = 60 * 60 * 24 199: upper = true 200: when 'dailymin' 201: period = 60 * 60 * 24 202: upper = false 203: when 'weeklymax' 204: interval.start = interval.start.beginOfWeek( 205: @project['weekStartsMonday']) 206: interval.end = interval.end.beginOfWeek(@project['weekStartsMonday']) 207: period = 60 * 60 * 24 * 7 208: upper = true 209: when 'weeklymin' 210: interval.start = interval.start.beginOfWeek( 211: @project['weekStartsMonday']) 212: interval.end = interval.end.beginOfWeek(@project['weekStartsMonday']) 213: period = 60 * 60 * 24 * 7 214: upper = false 215: when 'monthlymax' 216: interval.start = interval.start.beginOfMonth 217: interval.end = interval.end.beginOfMonth 218: # We use 30 days intervals here. This will cause the interval to drift 219: # away from calendar months. But it's better than using 30.4167 which 220: # does not align with day boundaries. 221: period = 60 * 60 * 24 * 30 222: upper = true 223: when 'monthlymin' 224: interval.start = interval.start.beginOfMonth 225: interval.end = interval.end.beginOfMonth 226: # We use 30 days intervals here. This will cause the interval to drift 227: # away from calendar months. But it's better than using 30.4167 which 228: # does not align with day boundaries. 229: period = 60 * 60 * 24 * 30 230: upper = false 231: when 'maximum' 232: period = interval.end - interval.start 233: upper = true 234: when 'minimum' 235: period = interval.end - interval.start 236: upper = false 237: else 238: raise "Limit period undefined" 239: end 240: 241: @limits << Limit.new(name, interval, period, value, upper, resource) 242: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.