Class: TrickBag::Numeric::StartAndMax

Inherits:
Object
  • Object
show all
Defined in:
lib/trick_bag/numeric/start_and_max.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_pos = :first, max_count = :infinite) ⇒ StartAndMax

Returns a new instance of StartAndMax.

Parameters:

  • start_pos (defaults to: :first)
    • the record number, zero offset, at which to begin each() processing; or :first

  • max_count (defaults to: :infinite)
    • the maximum number of records to be served by each(); or :infinite



14
15
16
17
# 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

Instance Attribute Details

#max_countObject (readonly)

Returns the value of attribute max_count.



10
11
12
# File 'lib/trick_bag/numeric/start_and_max.rb', line 10

def max_count
  @max_count
end

#start_posObject (readonly)

Returns the value of attribute start_pos.



10
11
12
# File 'lib/trick_bag/numeric/start_and_max.rb', line 10

def start_pos
  @start_pos
end

Instance Method Details

#max_count_reached?(count) ⇒ Boolean

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

Returns:

  • (Boolean)


29
30
31
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/trick_bag/numeric/start_and_max.rb', line 22

def start_position_reached?(num)
  @start_pos == :first || num >= @start_pos
end

#to_sObject

Returns string representation including starting position and maximum count.



35
36
37
# 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