Class: FiscalYear::Quarter

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

Class Method Summary collapse

Class Method Details

.cross_year_in_quarter?(quarter) ⇒ Boolean

Returns true if the quarter is crossed calendar year.

Parameters:

  • quarter (Array<Integer>)

    the quarter

Returns:

  • (Boolean)

    true if the quarter is crossed calendar year.



73
74
75
# File 'lib/fiscal_year/quarter.rb', line 73

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

.firstArray<Integer>

Returns the first quarter of the fiscal year.

Returns:

  • (Array<Integer>)

    the first quarter of the fiscal year.



# File 'lib/fiscal_year/quarter.rb', line 6

.first?(month) ⇒ Boolean

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

Parameters:

  • month (Integer)

    the month

Returns:

  • (Boolean)

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



25
26
27
# File 'lib/fiscal_year/quarter.rb', line 25

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

.fourthArray<Integer>

Returns the fourth quarter of the fiscal year.

Returns:

  • (Array<Integer>)

    the fourth quarter of the fiscal year.



17
18
19
20
21
# File 'lib/fiscal_year/quarter.rb', line 17

%i[first second third fourth].each do |method_name|
  define_method(method_name) do
    FiscalYear.quarters.public_send(method_name)
  end
end

.fourth?(month) ⇒ Boolean

Returns true if the month is in the fourth quarter of the fiscal year.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Boolean)

    true if the month is in the fourth quarter of the fiscal year.



43
44
45
# File 'lib/fiscal_year/quarter.rb', line 43

def fourth?(month)
  FiscalYear.quarters.fourth.include?(month)
end

.months(month) ⇒ Array<Integer>

Returns the quarter months by the month.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Array<Integer>)

    the quarter months by the month.



49
50
51
# File 'lib/fiscal_year/quarter.rb', line 49

def months(month)
  FiscalYear.quarters.find { |a| a.include?(month) } || raise(StandardError)
end

.passed_month_count_by(date) ⇒ Integer

start by 0.

Parameters:

  • date (Date)

    the date

Returns:

  • (Integer)

    the passed quarter count by the date.



81
82
83
# File 'lib/fiscal_year/quarter.rb', line 81

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:

  • month (Integer)

    the month

Returns:

  • (Integer)

    the passed quarter count by the month.



89
90
91
# File 'lib/fiscal_year/quarter.rb', line 89

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

.quarter_num(month) ⇒ Integer

Returns the quarter number by the month.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Integer)

    the quarter number by the month.



65
66
67
68
69
# File 'lib/fiscal_year/quarter.rb', line 65

def quarter_num(month)
  rindex = FiscalYear.quarters.rindex(months(month))

  rindex.nil? ? 0 : (rindex + 1)
end

.range_by(date) ⇒ Range<Date>

Returns the range of the quarter by the date.

Parameters:

  • date (Date)

    the date

Returns:

  • (Range<Date>)

    the range of the quarter by the date.



55
56
57
58
59
60
61
# File 'lib/fiscal_year/quarter.rb', line 55

def range_by(date)
  year = date.year
  this_quarter = months(date.month)
  last_year = cross_year_in_quarter?(this_quarter) ? year + 1 : year

  Date.parse("#{year}/#{this_quarter.first}/01")..Date.parse("#{last_year}/#{this_quarter.last}/01").end_of_month
end

.secondArray<Integer>

Returns the second quarter of the fiscal year.

Returns:

  • (Array<Integer>)

    the second quarter of the fiscal year.



# File 'lib/fiscal_year/quarter.rb', line 9

.second?(month) ⇒ Boolean

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

Parameters:

  • month (Integer)

    the month

Returns:

  • (Boolean)

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



31
32
33
# File 'lib/fiscal_year/quarter.rb', line 31

def second?(month)
  FiscalYear.quarters.second.include?(month)
end

.thirdArray<Integer>

Returns the third quarter of the fiscal year.

Returns:

  • (Array<Integer>)

    the third quarter of the fiscal year.



# File 'lib/fiscal_year/quarter.rb', line 12

.third?(month) ⇒ Boolean

Returns true if the month is in the third quarter of the fiscal year.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Boolean)

    true if the month is in the third quarter of the fiscal year.



37
38
39
# File 'lib/fiscal_year/quarter.rb', line 37

def third?(month)
  FiscalYear.quarters.third.include?(month)
end