Class: Renalware::WeekPeriod
- Inherits:
-
Object
- Object
- Renalware::WeekPeriod
- Defined in:
- lib/renalware/week_period.rb
Instance Attribute Summary collapse
-
#date_on_first_day_of_week ⇒ Object
readonly
Returns the value of attribute date_on_first_day_of_week.
-
#week_number ⇒ Object
readonly
Returns the value of attribute week_number.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(week_number:, year:) ⇒ WeekPeriod
constructor
A new instance of WeekPeriod.
-
#last_day_of_week ⇒ Object
The date of last day of the week (a Sunday).
- #next ⇒ Object
- #previous ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #validate_week_number ⇒ Object
- #validate_year ⇒ Object
Constructor Details
#initialize(week_number:, year:) ⇒ WeekPeriod
Returns a new instance of WeekPeriod.
12 13 14 15 16 17 18 |
# File 'lib/renalware/week_period.rb', line 12 def initialize(week_number:, year:) @week_number = week_number.to_i @year = year.to_i validate_week_number validate_year @date_on_first_day_of_week = Date.commercial(@year, @week_number) end |
Instance Attribute Details
#date_on_first_day_of_week ⇒ Object (readonly)
Returns the value of attribute date_on_first_day_of_week.
5 6 7 |
# File 'lib/renalware/week_period.rb', line 5 def date_on_first_day_of_week @date_on_first_day_of_week end |
#week_number ⇒ Object (readonly)
Returns the value of attribute week_number.
5 6 7 |
# File 'lib/renalware/week_period.rb', line 5 def week_number @week_number end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
5 6 7 |
# File 'lib/renalware/week_period.rb', line 5 def year @year end |
Class Method Details
.from_date(date) ⇒ Object
7 8 9 10 |
# File 'lib/renalware/week_period.rb', line 7 def self.from_date(date) date = date.to_date new(week_number: date.cweek, year: date.cwyear) end |
Instance Method Details
#last_day_of_week ⇒ Object
The date of last day of the week (a Sunday)
38 39 40 |
# File 'lib/renalware/week_period.rb', line 38 def last_day_of_week @last_day_of_week ||= (date_on_first_day_of_week + 1.week - 1.minute).to_date end |
#next ⇒ Object
20 21 22 |
# File 'lib/renalware/week_period.rb', line 20 def next self.class.from_date(date_on_first_day_of_week + 1.week) end |
#previous ⇒ Object
24 25 26 |
# File 'lib/renalware/week_period.rb', line 24 def previous self.class.from_date(date_on_first_day_of_week - 1.week) end |
#to_a ⇒ Object
28 29 30 |
# File 'lib/renalware/week_period.rb', line 28 def to_a [week_number, year] end |
#to_h ⇒ Object
52 53 54 55 56 57 |
# File 'lib/renalware/week_period.rb', line 52 def to_h { year: year, week_number: week_number } end |
#to_s ⇒ Object
32 33 34 35 |
# File 'lib/renalware/week_period.rb', line 32 def to_s "#{I18n.l(date_on_first_day_of_week, format: :long)} " \ "to #{I18n.l(last_day_of_week, format: :long)}" end |
#validate_week_number ⇒ Object
42 43 44 45 46 |
# File 'lib/renalware/week_period.rb', line 42 def validate_week_number if week_number < 1 || week_number > 53 raise(ArgumentError, "invalid date: week_number must be 1-53") end end |
#validate_year ⇒ Object
48 49 50 |
# File 'lib/renalware/week_period.rb', line 48 def validate_year raise(ArgumentError, "invalid date year must be >= 2000") if year <= 2000 end |