class TrickBag::Numeric::StartAndMax

Provides an object that will tell you if the number exceeds the starting point and if the number is >= the maximum (or if no maximum has been specified). While this functionality can easily be duplicated with more primitive code, this class provides an object whose use will be simpler and more readable.

Attributes

max_count[R]
start_pos[R]

Public Class Methods

new(start_pos = :first, max_count = :infinite) click to toggle source

@param start_pos - the record number, zero offset, at which to begin each() processing; or :first @param max_count - the maximum number of records to be served by each(); or :infinite

# File lib/trick_bag/numeric/start_and_max.rb, line 14
def initialize(start_pos = :first, max_count = :infinite)
  @start_pos = ([nil, :first].include?(start_pos) || start_pos <= 0) ? :first : start_pos
  @max_count = ([nil, :infinite].include?(max_count) || max_count <= 0) ? :infinite : max_count
end

Public Instance Methods

max_count_reached?(count) click to toggle source

If a maximum count has been specified, returns whether or not the specified number >= that count. Else, returns false unconditionally.

# File lib/trick_bag/numeric/start_and_max.rb, line 29
def max_count_reached?(count)
  @max_count != :infinite && (count >= @max_count)
end
start_position_reached?(num) click to toggle source

If a starting position has been specified, returns whether or not the specified number >= that position. Else, returns true unconditionally.

# File lib/trick_bag/numeric/start_and_max.rb, line 22
def start_position_reached?(num)
  @start_pos == :first || num >= @start_pos
end
to_s() click to toggle source

Returns string representation including starting position and maximum count.

# File lib/trick_bag/numeric/start_and_max.rb, line 35
def to_s
  "#{self.class}: start position=#{start_pos.inspect}, max count=#{max_count.inspect}"
end