lib/fiscaly.rb in fiscaly-1.0.0 vs lib/fiscaly.rb in fiscaly-1.1.0

- old
+ new

@@ -1,20 +1,23 @@ require 'active_support/core_ext/integer' require 'active_support/core_ext/time' require 'fiscaly/version' class Fiscaly - KEY = :fiscaly_start_month + KEY = :fiscaly_options - cattr_accessor :start_month - @@start_month = 4 + cattr_accessor :options + @@options = { + start_month: 4, + forward_fyear: false + } attr_reader :date - attr_reader :start_month + attr_reader :options - def initialize(date, start_month: nil) - @start_month = start_month || self.class.global_start_month + def initialize(date, options = {}) + @options = self.class.global_options.merge(options) @date = date end def method_missing(name, *args) if @date.respond_to?(name) @@ -22,24 +25,32 @@ else super end end + def start_month + @options[:start_month] + end + + def forward_fyear? + @options[:forward_fyear] + end + def fyear - if @date.month >= @start_month - @date.year - else - @date.year - 1 - end + fy = @date.year + fy -= 1 if @date.month < start_month + fy += 1 if forward_fyear? + fy end def fdate Date.new(fyear, @date.month, @date.day); end def beginning_of_fyear - Date.new(fyear, @start_month) + year = forward_fyear? ? fyear - 1 : fyear + Date.new(year, start_month) end def end_of_fyear (beginning_of_fyear + 11.months).end_of_month end @@ -51,11 +62,11 @@ def beginning_of_fhalf(index = nil) if index beginning_of_fyear + (index * 6).months else date = beginning_of_fyear - date += 6.months until @date <= date + date += 6.months until @date < date date -= 6.months end end def end_of_fhalf(index = nil) @@ -69,11 +80,11 @@ def beginning_of_fquarter(index = nil) if index beginning_of_fyear + (index * 3).months else date = beginning_of_fyear - date += 3.months until @date <= date + date += 3.months until @date < date date -= 3.months end end def end_of_fquarter(index = nil) @@ -105,42 +116,50 @@ def ymd(year, month = 1, day = 1, options = {}) date(Date.new(year, month, day), options) end - def fdate(date, options = {}) - self.new(normalize(date, options[:start_month]), options) + def fdate(fdate, options = {}) + fymd(fdate.year, fdate.month, fdate.day, options) end - def fparse(str, options = {}) - fdate(Date.parse(str), options) + def fparse(fstr, options = {}) + parsed = Date._parse(fstr) + fymd(parsed[:year], parsed[:mon], parsed[:mday], options) end def fymd(fyear, month = 1, day = 1, options = {}) - fdate(Date.new(fyear, month, day), options) + self.new(normalize(fyear, month, day, options), options) end - def with_start_month(start_month) + def with(options = {}) Thread.current[KEY] ||= [] - Thread.current[KEY].push(start_month) + Thread.current[KEY].push(options) yield ensure Thread.current[KEY].pop Thread.current[KEY] = nil if Thread.current[KEY].empty? end - def global_start_month - (Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || self.start_month + def global_options + @@options.merge((Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || {}) end + def start_month=(val) + @@options[:start_month] = val + end + + def forward_fyear=(val) + @@options[:forward_fyear] = val + end + private - def normalize(date, start_month) - start_month ||= global_start_month - if date.month < start_month - Date.new(date.year + 1, date.month, date.day) - else - date - end + def normalize(fyear, month, day, options = {}) + options = global_options.merge(options) + year = fyear + year += 1 if month < options[:start_month] + year -= 1 if options[:forward_fyear] + Date.new(year, month, day) end end end