Class: FiscalYear::Half

Inherits:
Object
  • Object
show all
Defined in:
lib/fiscal_year/half.rb

Class Method Summary collapse

Class Method Details

.cross_year_in_half?(half) ⇒ Boolean

Returns true if the any month of half are crossed calendar year.

Parameters:

  • half (Array<Integer>)

    the half of the fiscal year

Returns:

  • (Boolean)

    true if the any month of half are crossed calendar year.



75
76
77
# File 'lib/fiscal_year/half.rb', line 75

def cross_year_in_half?(half)
  FiscalYear.cross_year? && half.any? { |month| month == 12 }
end

.firstArray<Integer>

Returns the first half of the fiscal year.

Returns:

  • (Array<Integer>)

    the first half of the fiscal year.



7
8
9
# File 'lib/fiscal_year/half.rb', line 7

def first
  FiscalYear.halfs.first || raise(StandardError)
end

.first?(month) ⇒ Boolean

Returns true if the month is in the first half of the fiscal year.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Boolean)

    true if the month is in the first half of the fiscal year.



18
19
20
# File 'lib/fiscal_year/half.rb', line 18

def first?(month)
  FiscalYear.halfs.first.include?(month)
end

.first_range_by(year) ⇒ Range<Date>

Returns the range of the first half of the fiscal year.

Parameters:

  • year (Integer)

    the calendar year

Returns:

  • (Range<Date>)

    the range of the first half of the fiscal year.



36
37
38
39
40
41
42
43
44
45
# File 'lib/fiscal_year/half.rb', line 36

def first_range_by(year)
  # care Date#parse 2 digit year auto complete.
  # 99 + 1 = 100, but expect 2000 this context.
  year = 1999 if year == 99
  end_month = first.last || raise(StandardError)

  end_year = FiscalYear.increase_year_by_month(year, end_month)

  Date.parse("#{year}/#{first.first}/01")..Date.parse("#{end_year}/#{end_month}/01").end_of_month
end

.months(month) ⇒ Array<Integer>

Returns the half months by the month.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Array<Integer>)

    the half months by the month.



30
31
32
# File 'lib/fiscal_year/half.rb', line 30

def months(month)
  first?(month) ? first : second
end

.passed_month_count_by(date) ⇒ Integer

start by 0.

Parameters:

  • date (Date)

    the date

Returns:

  • (Integer)

    the passed month count from the beginning of the half.

See Also:



84
85
86
# File 'lib/fiscal_year/half.rb', line 84

def passed_month_count_by(date)
  passed_month_count_by_month(date.month)
end

.passed_month_count_by_month(month) ⇒ Integer

start by 0.

Parameters:

  • date (Date)

    the date

Returns:

  • (Integer)

    the passed month count from the beginning of the half.

See Also:



93
94
95
# File 'lib/fiscal_year/half.rb', line 93

def passed_month_count_by_month(month)
  months(month).find_index(month) || raise(StandardError)
end

.range_by(date) ⇒ Range<Date>

Returns the range of the half of the fiscal year by the date.

Parameters:

  • date (Date)

    the date

Returns:

  • (Range<Date>)

    the range of the half of the fiscal year by the date.



64
65
66
67
68
69
70
71
# File 'lib/fiscal_year/half.rb', line 64

def range_by(date)
  month = date.month
  year = date.year
  # if passed crossed year value, normalize to not crossed year value
  year = FiscalYear.decrease_year_by_month(year, month)

  first?(month) ? first_range_by(year) : second_range_by(year)
end

.secondArray<Integer>

Returns the second half of the fiscal year.

Returns:

  • (Array<Integer>)

    the second half of the fiscal year.



12
13
14
# File 'lib/fiscal_year/half.rb', line 12

def second
  FiscalYear.halfs.second || raise(StandardError)
end

.second?(month) ⇒ Boolean

Returns true if the month is in the second half of the fiscal year.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Boolean)

    true if the month is in the second half of the fiscal year.



24
25
26
# File 'lib/fiscal_year/half.rb', line 24

def second?(month)
  !first?(month)
end

.second_range_by(year) ⇒ Range<Date>

Returns the range of the second half of the fiscal year.

Parameters:

  • year (Integer)

    the calendar year

Returns:

  • (Range<Date>)

    the range of the second half of the fiscal year.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fiscal_year/half.rb', line 49

def second_range_by(year)
  # care Date#parse 2 digit year auto complete.
  # 99 + 1 = 100, but expect 2000 this context.
  year = 1999 if year == 99
  first_month = second.first || raise(StandardError)
  end_month = second.last || raise(StandardError)

  start_year = FiscalYear.increase_year_by_month(year, first_month)
  end_year = FiscalYear.increase_year_by_month(year, end_month)

  Date.parse("#{start_year}/#{first_month}/01")..Date.parse("#{end_year}/#{end_month}/01").end_of_month
end