Class: UkFinancialYear
- Inherits:
-
Object
- Object
- UkFinancialYear
- Includes:
- Comparable
- Defined in:
- lib/uk_financial_year.rb
Overview
Makes working with the UK financial or fiscal year easier.
Class Method Summary collapse
-
.from_s(s) ⇒ UkFinancialYear
creates a new UkFinancialYear from a string in the form ‘2000/01’ of the first year as four digits, a ‘/’, then the last year as two digits.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
lesser financial years are those which occur earliest.
-
#==(other) ⇒ Object
equality method.
- #adjacent?(other_financial_year) ⇒ Boolean
-
#after?(date_or_fy) ⇒ Boolean
tells if the given date or financial year is after this one.
-
#before?(date_or_fy) ⇒ Boolean
tells if the given date or financial year is before this one.
-
#first_day ⇒ Date
returns the date of the first day of the financial year.
-
#include?(date) ⇒ Boolean
tells if the financial year includes the given date.
-
#initialize(date = Date.today) ⇒ UkFinancialYear
constructor
creates a new UkFinancialYear as the financial year which includes the given date If missing returns the current financial year.
-
#last_day ⇒ Date
returns the date of the last day of the financial year.
-
#next ⇒ UkFinancialYear
returns the next financial year.
-
#period_before(date) ⇒ Range<Date>
returns the period before this date in the financial year financial year.
-
#previous ⇒ UkFinancialYear
returns the previous financial year.
-
#to_s ⇒ String
returns string representation of the financial year in the form ‘2000/01’.
Constructor Details
#initialize(date = Date.today) ⇒ UkFinancialYear
creates a new UkFinancialYear as the financial year which includes the given date If missing returns the current financial year
12 13 14 15 |
# File 'lib/uk_financial_year.rb', line 12 def initialize date=Date.today start_date = start_date date @range = start_date...start_date.next_year end |
Class Method Details
.from_s(s) ⇒ UkFinancialYear
creates a new UkFinancialYear from a string in the form ‘2000/01’ of the first year as four digits, a ‘/’, then the last year as two digits
31 32 33 34 |
# File 'lib/uk_financial_year.rb', line 31 def UkFinancialYear.from_s s check_format s new(Date.new s.to(3).to_i, 4, 6) end |
Instance Method Details
#<=>(other) ⇒ Object
lesser financial years are those which occur earliest
102 103 104 |
# File 'lib/uk_financial_year.rb', line 102 def <=> other self.first_day <=> other.first_day end |
#==(other) ⇒ Object
equality method
97 98 99 |
# File 'lib/uk_financial_year.rb', line 97 def == other self.first_day == other.first_day end |
#adjacent?(other_financial_year) ⇒ Boolean
67 68 69 |
# File 'lib/uk_financial_year.rb', line 67 def adjacent? other_financial_year other_financial_year.last_day.tomorrow == self.first_day or other_financial_year.first_day.yesterday == self.last_day end |
#after?(date_or_fy) ⇒ Boolean
tells if the given date or financial year is after this one
81 82 83 |
# File 'lib/uk_financial_year.rb', line 81 def after? date_or_fy self.first_day > date_to_compare(date_or_fy) end |
#before?(date_or_fy) ⇒ Boolean
tells if the given date or financial year is before this one
74 75 76 |
# File 'lib/uk_financial_year.rb', line 74 def before? date_or_fy self.first_day < date_to_compare(date_or_fy) end |
#first_day ⇒ Date
returns the date of the first day of the financial year
38 39 40 |
# File 'lib/uk_financial_year.rb', line 38 def first_day @range.min end |
#include?(date) ⇒ Boolean
tells if the financial year includes the given date
51 52 53 |
# File 'lib/uk_financial_year.rb', line 51 def include? date @range.include? date end |
#last_day ⇒ Date
returns the date of the last day of the financial year
44 45 46 |
# File 'lib/uk_financial_year.rb', line 44 def last_day @range.max end |
#next ⇒ UkFinancialYear
returns the next financial year
57 58 59 |
# File 'lib/uk_financial_year.rb', line 57 def next UkFinancialYear.new self.first_day.next_year end |
#period_before(date) ⇒ Range<Date>
returns the period before this date in the financial year financial year
89 90 91 92 93 94 |
# File 'lib/uk_financial_year.rb', line 89 def period_before date raise "#{date} is before FY #{to_s} which starts on #{first_day}" if date < first_day raise "#{date} is after FY #{to_s} which ends on #{last_day}" if date > last_day first_day...date end |
#previous ⇒ UkFinancialYear
returns the previous financial year
63 64 65 |
# File 'lib/uk_financial_year.rb', line 63 def previous UkFinancialYear.new self.first_day.prev_year end |
#to_s ⇒ String
returns string representation of the financial year in the form ‘2000/01’. This is the form HMRC use. digits, a ‘/’, then the last year as two digits
21 22 23 |
# File 'lib/uk_financial_year.rb', line 21 def to_s "#{first_day.year}/#{last_day.year.to_s.from 2}" end |