Class: TrickBag::Numeric::StartAndMax
- Inherits:
-
Object
- Object
- TrickBag::Numeric::StartAndMax
- 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
-
#max_count ⇒ Object
readonly
Returns the value of attribute max_count.
-
#start_pos ⇒ Object
readonly
Returns the value of attribute start_pos.
Instance Method Summary collapse
-
#initialize(start_pos = :first, max_count = :infinite) ⇒ StartAndMax
constructor
A new instance of StartAndMax.
-
#max_count_reached?(count) ⇒ Boolean
If a maximum count has been specified, returns whether or not the specified number >= that count.
-
#start_position_reached?(num) ⇒ Boolean
If a starting position has been specified, returns whether or not the specified number >= that position.
-
#to_s ⇒ Object
Returns string representation including starting position and maximum count.
Constructor Details
#initialize(start_pos = :first, max_count = :infinite) ⇒ StartAndMax
Returns a new instance of StartAndMax.
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_count ⇒ Object (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_pos ⇒ Object (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.
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.
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_s ⇒ Object
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 |