Class: DoubleEntry::Reporting::WeekRange Private

Inherits:
TimeRange
  • Object
show all
Defined in:
lib/double_entry/reporting/week_range.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Attributes inherited from TimeRange

#day, #finish, #hour, #month, #range_type, #start

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TimeRange

#human_readable_name, #include?, #key, make, range_from_time_for_period

Constructor Details

#initialize(options = {}) ⇒ WeekRange

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of WeekRange.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/double_entry/reporting/week_range.rb', line 60

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

Instance Attribute Details

#weekObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/double_entry/reporting/week_range.rb', line 58

def week
  @week
end

#yearObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/double_entry/reporting/week_range.rb', line 58

def year
  @year
end

Class Method Details

.currentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/double_entry/reporting/week_range.rb', line 26

def current
  from_time(Time.now)
end

.from_time(time) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/double_entry/reporting/week_range.rb', line 12

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>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :from (Time)

    Time of the first in the returned sequence of WeekRanges.

Returns:



36
37
38
39
40
41
42
43
44
45
# File 'lib/double_entry/reporting/week_range.rb', line 36

def reportable_weeks(options = {})
  week = options[:from] ? from_time(options[:from]) : earliest_week
  last_in_sequence = self.current
  [week].tap do |weeks|
    while week != last_in_sequence
      week = week.next
      weeks << week
    end
  end
end

Instance Method Details

#==(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
# File 'lib/double_entry/reporting/week_range.rb', line 81

def ==(other)
  (self.week == other.week) && (self.year == other.year)
end

#all_timeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
# File 'lib/double_entry/reporting/week_range.rb', line 85

def all_time
  self.class.new(:year => year, :week => week, :range_type => :all_time)
end

#nextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
# File 'lib/double_entry/reporting/week_range.rb', line 77

def next
  from_time(@start + 1.week)
end

#previousObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
# File 'lib/double_entry/reporting/week_range.rb', line 73

def previous
  from_time(@start - 1.week)
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
# File 'lib/double_entry/reporting/week_range.rb', line 89

def to_s
  "#{year}, Week #{week}"
end