lib/uk_financial_year.rb in fyuk-0.6.3 vs lib/uk_financial_year.rb in fyuk-0.6.4
- old
+ new
@@ -1,6 +1,6 @@
-require 'date'
+require 'active_support/all'
# Makes working with the UK financial or fiscal year easier.
class UkFinancialYear
include Comparable
@@ -17,30 +17,34 @@
# returns string representation of the financial year in the form '2000/01'.
# This is the form HMRC use.
# @return [String] representation in the form of the first year as four
# digits, a '/', then the last year as two digits
def to_s
- "#{first_day.year}/#{last_day.year.to_s[-2..-1]}"
+ "#{first_day.year}/#{last_day.year.to_s.from 2}"
end
# creates a new UkFinancialYear from a string in the form '2000/01'
# @param [String] s the two years of the financial year in the form
# of the first year as four digits, a '/', then the last year as
# two digits
# @raise [RuntimeError] if the string cannot be parsed to a financial year
# @return [UkFinancialYear] the financial year specified by the string
def UkFinancialYear.from_s s
+ check_format s
+ new(Date.new s.to(3).to_i, 4, 6)
+ end
+
+ def self.check_format s
if /^(?<year1>\d{4})\/(?<year2>\d{2})$/ =~ s
year1 = year1.to_i
year1_century = year1 / 100
year2_century = year1 % 100 == 99 ? year1_century + 1 : year1_century
year2 = year2_century * 100 + year2.to_i
- raise %{"#{year1}" and "#{year2}" are not consecutive years} unless year2 == year1 + 1
- return new(Date.new year1, 4, 6)
+ raise %{"#{year1}" and "#{year2}" are not consecutive years} unless year1 + 1 == year2
+ else
+ raise %{"#{s}" does not match FY string format}
end
-
- raise %{"#{s}" does not match FY string format}
end
# returns the date of the first day of the financial year
# @return [Date] the first day
def first_day
@@ -71,34 +75,34 @@
def previous
UkFinancialYear.new self.first_day.prev_year
end
def adjacent? other_financial_year
- self.first_day.next_year == other_financial_year.first_day or self.first_day.prev_year == other_financial_year.first_day
+ other_financial_year.last_day.tomorrow == self.first_day or other_financial_year.first_day.yesterday == self.last_day
end
# tells if the given date or financial year is before this one
# @param [Date] date_or_fy date or financial year to check
# @return [Boolean] to indicate if this financial year is before
def before? date_or_fy
- self.first_day.before?(date_to_compare date_or_fy)
+ self.first_day < date_to_compare(date_or_fy)
end
# tells if the given date or financial year is after this one
- # @param (see FixedOdds#before?)
+ # @param (see UkFinancialYear#before?)
# @return [Boolean] to indicate if this financial year is after
def after? date_or_fy
- self.first_day.after?(date_to_compare date_or_fy)
+ self.first_day > date_to_compare(date_or_fy)
end
# returns the period before this date in the financial year
# @param [Date] date a date in the financial year
# @return [Range<Date>] the period before this date in the the
# financial year
def period_before date
- raise "#{date} is before FY #{to_s}" if date < first_day
- raise "#{date} is after FY #{to_s}" if date > last_day
+ 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
# equality method
@@ -110,24 +114,16 @@
def <=> other
self.first_day <=> other.first_day
end
private
- def date_to_compare other
- if other.is_a?(Date) then other else other.first_day end
- end
def start_date date
- swap_date_that_year = Date.new date.year, 4, 6
- (date.after?(swap_date_that_year) or date == swap_date_that_year) ? swap_date_that_year : swap_date_that_year.prev_year
+ swap_date_that_year = date.change day: 6, month: 4
+ date < swap_date_that_year ? swap_date_that_year.prev_year : swap_date_that_year
end
-end
-class Date
- def before? other
- self < other
- end
+ def date_to_compare other
+ other.is_a?(Date) ? other : other.first_day
+ end
- def after? other
- self > other
- end
end
\ No newline at end of file