lib/uk_financial_year.rb in fyuk-0.4.0 vs lib/uk_financial_year.rb in fyuk-0.5.0
- old
+ new
@@ -17,11 +17,11 @@
# 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
- "#{self.first_day.year}/#{self.last_day.year.to_s[-2..-1]}"
+ "#{self.first_day.year}/#{self.last_day.year.to_s.last(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
@@ -71,24 +71,58 @@
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 || self.first_day.prev_year == other_financial_year.first_day
+ self.first_day.next_year == other_financial_year.first_day or self.first_day.prev_year == other_financial_year.first_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)
+ end
+
+ # tells if the given date or financial year is after this one
+ # @param (see FixedOdds#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)
+ end
+
# equality method
- def ==(other)
+ def == other
self.first_day == other.first_day
end
# lesser financial years are those which occur earliest
- def <=>(other)
+ 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 >= swap_date_that_year ? swap_date_that_year : swap_date_that_year.prev_year
+ (date.after?(swap_date_that_year) or date == swap_date_that_year) ? swap_date_that_year : swap_date_that_year.prev_year
end
+end
+
+class String
+ def last n
+ self[-n, n]
+ end
+end
+
+class Date
+ def before? other
+ self < other
+ end
+
+ def after? other
+ self > other
+ end
end
\ No newline at end of file