Class: DoubleEntry::Reporting::MonthRange

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

Instance Attribute Summary collapse

Attributes inherited from TimeRange

#day, #finish, #hour, #range_type, #start, #week

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 = {}) ⇒ MonthRange

Returns a new instance of MonthRange.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/double_entry/reporting/month_range.rb', line 38

def initialize(options = {})
  super options

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

    month_start = Time.local(year, options[:month], 1)
    @start = month_start
    @finish = month_start.end_of_month

    @start = MonthRange.earliest_month.start if options[:range_type] == :all_time
  end
end

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



36
37
38
# File 'lib/double_entry/reporting/month_range.rb', line 36

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



36
37
38
# File 'lib/double_entry/reporting/month_range.rb', line 36

def year
  @year
end

Class Method Details

.currentObject



10
11
12
# File 'lib/double_entry/reporting/month_range.rb', line 10

def current
  from_time(Time.now)
end

.earliest_monthObject



31
32
33
# File 'lib/double_entry/reporting/month_range.rb', line 31

def earliest_month
  from_time(Reporting.configuration.start_of_business)
end

.from_time(time) ⇒ Object



6
7
8
# File 'lib/double_entry/reporting/month_range.rb', line 6

def from_time(time)
  new(:year => time.year, :month => time.month)
end

.reportable_months(options = {}) ⇒ Array<MonthRange>

Obtain a sequence of MonthRanges from the given start to the current month.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :from (Time)

    Time of the first in the returned sequence of MonthRanges.

Returns:



20
21
22
23
24
25
26
27
28
29
# File 'lib/double_entry/reporting/month_range.rb', line 20

def reportable_months(options = {})
  month = options[:from] ? from_time(options[:from]) : earliest_month
  last = current
  [month].tap do |months|
    while month != last
      month = month.next
      months << month
    end
  end
end

Instance Method Details

#<=>(other) ⇒ Object



76
77
78
# File 'lib/double_entry/reporting/month_range.rb', line 76

def <=>(other)
  start <=> other.start
end

#==(other) ⇒ Object



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

def ==(other)
  month == other.month &&
    year == other.year
end

#all_timeObject



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

def all_time
  MonthRange.new(:year => year, :month => month, :range_type => :all_time)
end

#beginning_of_financial_yearObject



68
69
70
71
72
# File 'lib/double_entry/reporting/month_range.rb', line 68

def beginning_of_financial_year
  first_month_of_financial_year = Reporting.configuration.first_month_of_financial_year
  year = (month >= first_month_of_financial_year) ? @year : (@year - 1)
  MonthRange.new(:year => year, :month => first_month_of_financial_year)
end

#nextObject Also known as: succ



60
61
62
63
64
65
66
# File 'lib/double_entry/reporting/month_range.rb', line 60

def next
  if month >= 12
    MonthRange.new :year => year + 1, :month => 1
  else
    MonthRange.new :year => year, :month => month + 1
  end
end

#previousObject



52
53
54
55
56
57
58
# File 'lib/double_entry/reporting/month_range.rb', line 52

def previous
  if month <= 1
    MonthRange.new :year => year - 1, :month => 12
  else
    MonthRange.new :year => year, :month => month - 1
  end
end

#to_sObject



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

def to_s
  start.strftime('%Y, %b')
end