class DoubleEntry::Reporting::WeekRange

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.

Attributes

week[R]
year[R]

Public Class Methods

current() click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 24
def current
  from_time(Time.now)
end
from_time(time) click to toggle source
# 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
new(options = {}) click to toggle source
Calls superclass method DoubleEntry::Reporting::TimeRange::new
# File lib/double_entry/reporting/week_range.rb, line 58
def initialize(options = {})
  super options

  if options.present?
    @week = options[:week]

    @start  = week_and_year_to_time(@week, @year)
    @finish = @start.end_of_week

    @start = earliest_week.start if options[:range_type] == :all_time
  end
end
reportable_weeks(options = {}) click to toggle source

Obtain a sequence of WeekRanges from the given start to the current week.

@option options :from [Time] Time of the first in the returned sequence

of WeekRanges.

@return [Array<WeekRange>]

# File lib/double_entry/reporting/week_range.rb, line 34
def reportable_weeks(options = {})
  week = options[:from] ? from_time(options[:from]) : earliest_week
  last_in_sequence = current
  [week].tap do |weeks|
    while week != last_in_sequence
      week = week.next
      weeks << week
    end
  end
end

Private Class Methods

earliest_week() click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 51
def earliest_week
  from_time(Reporting.configuration.start_of_business)
end
start_of_year(year) click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 47
def start_of_year(year)
  Time.local(year, 1, 1).beginning_of_week
end

Public Instance Methods

==(other) click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 79
def ==(other)
  week == other.week &&
    year == other.year
end
all_time() click to toggle source
# 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() click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 75
def next
  from_time(@start + 1.week)
end
previous() click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 71
def previous
  from_time(@start - 1.week)
end
to_s() click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 88
def to_s
  "#{year}, Week #{week}"
end

Private Instance Methods

earliest_week() click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 98
def earliest_week
  self.class.send(:earliest_week)
end
from_time(time) click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 94
def from_time(time)
  self.class.from_time(time)
end
week_and_year_to_time(week, year) click to toggle source
# File lib/double_entry/reporting/week_range.rb, line 102
def week_and_year_to_time(week, year)
  self.class.send(:start_of_year, year) + (week - 1).weeks
end