Class: DoubleEntry::Reporting::WeekRange
- Defined in:
- lib/double_entry/reporting/week_range.rb
Overview
We use a particularly crazy week numbering system: week 1 of any given year is the first week with any days that fall into that year.
So, for example, week 1 of 2011 starts on 27 Dec 2010.
Instance Attribute Summary collapse
-
#week ⇒ Object
readonly
Returns the value of attribute week.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Attributes inherited from TimeRange
#day, #finish, #hour, #month, #range_type, #start
Class Method Summary collapse
- .current ⇒ Object
- .from_time(time) ⇒ Object
-
.reportable_weeks(options = {}) ⇒ Array<WeekRange>
Obtain a sequence of WeekRanges from the given start to the current week.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #all_time ⇒ Object
-
#initialize(options = {}) ⇒ WeekRange
constructor
A new instance of WeekRange.
- #next ⇒ Object
- #previous ⇒ Object
- #to_s ⇒ Object
Methods inherited from TimeRange
#human_readable_name, #include?, #key, make, range_from_time_for_period
Constructor Details
#initialize(options = {}) ⇒ WeekRange
Returns a new instance of WeekRange.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/double_entry/reporting/week_range.rb', line 58 def initialize( = {}) super if .present? @week = [:week] @start = week_and_year_to_time(@week, @year) @finish = @start.end_of_week @start = earliest_week.start if [:range_type] == :all_time end end |
Instance Attribute Details
#week ⇒ Object (readonly)
Returns the value of attribute week.
56 57 58 |
# File 'lib/double_entry/reporting/week_range.rb', line 56 def week @week end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
56 57 58 |
# File 'lib/double_entry/reporting/week_range.rb', line 56 def year @year end |
Class Method Details
.current ⇒ Object
24 25 26 |
# File 'lib/double_entry/reporting/week_range.rb', line 24 def current from_time(Time.now) end |
.from_time(time) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/double_entry/reporting/week_range.rb', line 10 def from_time(time) date = time.to_date week = date.cweek year = date.end_of_week.year if date.beginning_of_week.year != year week = 1 elsif date.beginning_of_year.cwday > Date::DAYNAMES.index('Thursday') week += 1 end new(:year => year, :week => week) end |
.reportable_weeks(options = {}) ⇒ Array<WeekRange>
Obtain a sequence of WeekRanges from the given start to the current week.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/double_entry/reporting/week_range.rb', line 34 def reportable_weeks( = {}) week = [:from] ? from_time([:from]) : earliest_week last_in_sequence = current [week].tap do |weeks| while week != last_in_sequence week = week.next weeks << week end end end |
Instance Method Details
#==(other) ⇒ Object
79 80 81 82 |
# File 'lib/double_entry/reporting/week_range.rb', line 79 def ==(other) week == other.week && year == other.year end |
#all_time ⇒ Object
84 85 86 |
# File 'lib/double_entry/reporting/week_range.rb', line 84 def all_time self.class.new(:year => year, :week => week, :range_type => :all_time) end |
#next ⇒ Object
75 76 77 |
# File 'lib/double_entry/reporting/week_range.rb', line 75 def next from_time(@start + 1.week) end |
#previous ⇒ Object
71 72 73 |
# File 'lib/double_entry/reporting/week_range.rb', line 71 def previous from_time(@start - 1.week) end |
#to_s ⇒ Object
88 89 90 |
# File 'lib/double_entry/reporting/week_range.rb', line 88 def to_s "#{year}, Week #{week}" end |