Class TaskJuggler::Limits::Limit
In: lib/taskjuggler/Limits.rb
Parent: Object

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.

Methods

copy   inc   new   ok?   reset  

Attributes

interval  [R] 
name  [R] 
resource  [RW] 
upper  [R] 

Public Class methods

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.

[Source]

# 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

Public Instance methods

Returns a deep copy of the class instance.

[Source]

# File lib/taskjuggler/Limits.rb, line 52
      def copy
        Limit.new(@name, @interval, @period, @value, @upper, @resource)
      end

Increase the counter if the index matches the @interval. The relationship between @resource and resource is described below. @r \ r nil y nil inc inc

 x         -    if x==y inc else -

[Source]

# 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

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: @r \ r nil y nil test true

 x        true  if x==y test else true

[Source]

# File lib/taskjuggler/Limits.rb, line 94
      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.nil? ^ !resource.nil?) ||
                       (@resource && 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)

          return @upper ? @scoreboard[idxToSbIdx(index)] < @value :
                          @scoreboard[idxToSbIdx(index)] >= @value
        end
      end

This function can be used to reset the counter for a specific period specified by index or to reset all counters.

[Source]

# 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

[Validate]