Class: Octopolo::Week
- Inherits:
-
Object
- Object
- Octopolo::Week
- Defined in:
- lib/octopolo/week.rb
Constant Summary collapse
- DATE_STRING_FORMAT =
"%Y-%m-%d"
- InvalidType =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#number ⇒ Object
Returns the value of attribute number.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
-
.current ⇒ Object
Public: Instantiate a Week for the current week.
-
.last(count) ⇒ Object
Public: A list of the last N weeks, excluding the current week.
-
.parse(dateish) ⇒ Object
Public: Parse the date from a date-ish object.
-
.prior ⇒ Object
Public: Instantiate a Week for the prior week.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Public: Whether the week is the same as another week.
-
#end_date ⇒ Object
Public: Sunday that ends the week.
-
#initialize(year, number) ⇒ Week
constructor
Public: Instantiate a new Week.
-
#inspect ⇒ Object
Public: Programmer-friendly representation of the week.
-
#next ⇒ Object
Public: The next week.
-
#previous ⇒ Object
Public: The previous week.
-
#start_date ⇒ Object
Public: Monday that starts the week.
-
#to_s ⇒ Object
Public: Friendly string representation of the week.
Constructor Details
#initialize(year, number) ⇒ Week
Public: Instantiate a new Week
year - Year the week occurs in number - The number of the week in the given year
12 13 14 15 |
# File 'lib/octopolo/week.rb', line 12 def initialize year, number self.year = year self.number = number end |
Instance Attribute Details
#number ⇒ Object
Returns the value of attribute number.
4 5 6 |
# File 'lib/octopolo/week.rb', line 4 def number @number end |
#year ⇒ Object
Returns the value of attribute year.
3 4 5 |
# File 'lib/octopolo/week.rb', line 3 def year @year end |
Class Method Details
.current ⇒ Object
Public: Instantiate a Week for the current week
Returns an instance of Week
36 37 38 39 |
# File 'lib/octopolo/week.rb', line 36 def self.current today = Date.today Week.new today.cwyear, today.cweek end |
.last(count) ⇒ Object
Public: A list of the last N weeks, excluding the current week
count - An integer listing the number of weeks to return
Returns an Array of Week instances
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/octopolo/week.rb', line 53 def self.last count # holy hell this is gnarly # # essentially, create an array with the current week in it, then loop # through and tack on the previous week for whichever week is last; this # way, we keep tacking on ever more previous weeks as many times as is # asked for. out = [prior] (count - 1).times do out << out.last.previous end out end |
.parse(dateish) ⇒ Object
Public: Parse the date from a date-ish object
dateish - A Date, Time, or String representing a date or time
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/octopolo/week.rb', line 20 def self.parse dateish # TODO i'm not 100% happy with this implementation, but it does what i want and the tests pass if dateish.respond_to? :to_date date = dateish.to_date new date.year, date.cweek else date = Date.parse(dateish.to_s) new date.year, date.cweek end rescue ArgumentError raise InvalidType end |
.prior ⇒ Object
Public: Instantiate a Week for the prior week
Returns an instance of Week
44 45 46 |
# File 'lib/octopolo/week.rb', line 44 def self.prior current.previous end |
Instance Method Details
#==(other) ⇒ Object
Public: Whether the week is the same as another week
Returns a Boolean
84 85 86 87 88 |
# File 'lib/octopolo/week.rb', line 84 def == other year == other.year && number == other.number rescue false end |
#end_date ⇒ Object
Public: Sunday that ends the week
Returns an instance of Date
124 125 126 |
# File 'lib/octopolo/week.rb', line 124 def end_date Date.commercial year, number, 7 end |
#inspect ⇒ Object
Public: Programmer-friendly representation of the week
Returns a String containing object details
77 78 79 |
# File 'lib/octopolo/week.rb', line 77 def inspect "#<#{self.class} #{to_s} (year=#{year} number=#{number})>" end |
#next ⇒ Object
Public: The next week
Returns an instance of Week
105 106 107 108 109 110 111 112 |
# File 'lib/octopolo/week.rb', line 105 def next if number == 52 # carry the 1 Week.new year + 1, 1 else Week.new year, number + 1 end end |
#previous ⇒ Object
Public: The previous week
Returns an instance of Week
93 94 95 96 97 98 99 100 |
# File 'lib/octopolo/week.rb', line 93 def previous if number == 1 # carry the 1 Week.new year - 1, 52 else Week.new year, number - 1 end end |
#start_date ⇒ Object
Public: Monday that starts the week
Returns an instance of Date
117 118 119 |
# File 'lib/octopolo/week.rb', line 117 def start_date Date.commercial year, number end |
#to_s ⇒ Object
Public: Friendly string representation of the week
Returns a String of the start date
70 71 72 |
# File 'lib/octopolo/week.rb', line 70 def to_s start_date.strftime(DATE_STRING_FORMAT) end |