Class: TaskJuggler::Limits::Limit
Overview
This class implements a mechanism that can be used to limit certain events within a certain time period. It supports an upper and a lower limit.
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#upper ⇒ Object
readonly
Returns the value of attribute upper.
Instance Method Summary collapse
-
#copy ⇒ Object
Returns a deep copy of the class instance.
-
#dec(index, resource) ⇒ Object
Decrease the counter if the index matches the @interval.
-
#inc(index, resource) ⇒ Object
Increase the counter if the index matches the @interval.
-
#initialize(name, interval, period, value, upper, resource) ⇒ Limit
constructor
To create a new Limit object, the Interval
interval
and the period duration (period
in seconds) must be specified. -
#ok?(index, upper, resource) ⇒ Boolean
Returns true if the counter for the time slot specified by
index
or all counters are within the limit. -
#reset(index = nil) ⇒ Object
This function can be used to reset the counter for a specific period specified by
index
or to reset all counters.
Constructor Details
#initialize(name, interval, period, value, upper, resource) ⇒ Limit
To create a new Limit object, the Interval interval
and the period duration (period
in seconds) must be specified. This creates a counter for each period within the overall interval. value
is the value of the limit. upper
specifies whether the limit is an upper or lower limit. The limit can also be restricted to certain a Resource specified by resource
.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/taskjuggler/Limits.rb', line 37 def initialize(name, interval, period, value, upper, resource) @name = name @interval = interval @period = period @value = value @upper = upper @resource = resource # To avoid multiple resets of untouched scoreboards we keep this dirty # flag. It's set whenever a counter is increased. @dirty = true reset end |
Instance Attribute Details
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
29 30 31 |
# File 'lib/taskjuggler/Limits.rb', line 29 def interval @interval end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/taskjuggler/Limits.rb', line 29 def name @name end |
#resource ⇒ Object
Returns the value of attribute resource.
28 29 30 |
# File 'lib/taskjuggler/Limits.rb', line 28 def resource @resource end |
#upper ⇒ Object (readonly)
Returns the value of attribute upper.
29 30 31 |
# File 'lib/taskjuggler/Limits.rb', line 29 def upper @upper end |
Instance Method Details
#copy ⇒ Object
Returns a deep copy of the class instance.
52 53 54 |
# File 'lib/taskjuggler/Limits.rb', line 52 def copy Limit.new(@name, @interval, @period, @value, @upper, @resource) end |
#dec(index, resource) ⇒ Object
Decrease the counter if the index matches the @interval. The relationship between @resource and resource is described below. nil inc inc
x - if x==y inc else -
91 92 93 94 95 96 97 98 |
# File 'lib/taskjuggler/Limits.rb', line 91 def dec(index, resource) if @interval.contains?(index) && (@resource.nil? || @resource == resource) # The condition is met, decrement the counter for the interval. @dirty = true @scoreboard[idxToSbIdx(index)] -= 1 end end |
#inc(index, resource) ⇒ Object
Increase the counter if the index matches the @interval. The relationship between @resource and resource is described below. nil inc inc
x - if x==y inc else -
77 78 79 80 81 82 83 84 |
# File 'lib/taskjuggler/Limits.rb', line 77 def inc(index, resource) if @interval.contains?(index) && (@resource.nil? || @resource == resource) # The condition is met, increment the counter for the interval. @dirty = true @scoreboard[idxToSbIdx(index)] += 1 end end |
#ok?(index, upper, resource) ⇒ Boolean
Returns true if the counter for the time slot specified by index
or all counters are within the limit. If upper
is true, only upper limits are checked. If not, only lower limits are checked. The dependency between resource and @resource is described in the matrix below: nil test true
x true if x==y test else true
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/taskjuggler/Limits.rb', line 108 def ok?(index, upper, resource) # if @upper does not match or the provided resource does not match, # we can ignore this limit. return true if @upper != upper || (@resource && @resource != resource) if index.nil? # No index given. We need to check all counters. @scoreboard.each do |i| return false if @upper ? i >= @value : i < @value end return true else # If the index is outside the interval we don't have to check # anything. Everything is ok. return true if !@interval.contains?(index) sbVal = @scoreboard[idxToSbIdx(index)] return @upper ? (sbVal < @value) : (sbVal >= @value) end end |
#reset(index = nil) ⇒ Object
This function can be used to reset the counter for a specific period specified by index
or to reset all counters.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/taskjuggler/Limits.rb', line 58 def reset(index = nil) return unless @dirty if index.nil? @scoreboard = Scoreboard.new(@interval.startDate, @interval.endDate, @period, 0) else return unless @interval.contains?(index) # The scoreboard may be just a subset of the @interval period. @scoreboard[idxToSbIdx(index)] = 0 end @dirty = false end |