Class: DoubleEntry::WeekRange

Inherits:
TimeRange show all
Defined in:
lib/double_entry/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

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

Returns a new instance of WeekRange.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/double_entry/week_range.rb', line 52

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)

Returns the value of attribute week.



50
51
52
# File 'lib/double_entry/week_range.rb', line 50

def week
  @week
end

#yearObject (readonly)

Returns the value of attribute year.



50
51
52
# File 'lib/double_entry/week_range.rb', line 50

def year
  @year
end

Class Method Details

.currentObject



18
19
20
# File 'lib/double_entry/week_range.rb', line 18

def current
  from_time(Time.now)
end

.from_time(time) ⇒ Object



11
12
13
14
15
16
# File 'lib/double_entry/week_range.rb', line 11

def from_time(time)
  time = time.to_time if time.is_a? Date
  year = time.end_of_week.year
  week = ((time.beginning_of_week - start_of_year(year)) / 1.week).floor + 1
  new(:year => year, :week => week)
end

.reportable_weeks(options = {}) ⇒ Array<WeekRange>

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:



28
29
30
31
32
33
34
35
36
37
# File 'lib/double_entry/week_range.rb', line 28

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



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

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

#all_timeObject



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

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

#nextObject



69
70
71
# File 'lib/double_entry/week_range.rb', line 69

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

#previousObject



65
66
67
# File 'lib/double_entry/week_range.rb', line 65

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

#to_sObject



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

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