Class: Fiscal::FiscalPeriod
- Inherits:
-
Object
- Object
- Fiscal::FiscalPeriod
- Includes:
- FiscalConfig
- Defined in:
- lib/fiscal/period.rb
Defined Under Namespace
Classes: FiscalError
Instance Method Summary collapse
- #end ⇒ Object
-
#initialize(options = {}) ⇒ FiscalPeriod
constructor
A new instance of FiscalPeriod.
- #next ⇒ Object
- #number ⇒ Object
- #prev ⇒ Object
- #start ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
- #validate ⇒ Object
Methods included from FiscalConfig
Constructor Details
#initialize(options = {}) ⇒ FiscalPeriod
Returns a new instance of FiscalPeriod.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fiscal/period.rb', line 8 def initialize( = {}) @type = ([:type]) @date = ([:date] || Date.today).to_date @country = ([:country] || :nil).to_sym if [:index] # user input @index = [:index].to_i elsif @type == :year # only 1 year in a fiscal year @index = 1 else # if the user does not enter, compute the index @index = number end validate end |
Instance Method Details
#end ⇒ Object
47 48 49 50 |
# File 'lib/fiscal/period.rb', line 47 def end # find start date for next year, and minus one start_date(next: true) - 1 end |
#next ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fiscal/period.rb', line 66 def next if @index == (12 / months_in(@type)) date = @date.change(year: @date.year + 1) index = 1 else date = @date index = @index + 1 end self.class.new(date: date, country: @country, type: @type, index: index) end |
#number ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fiscal/period.rb', line 52 def number if @type == :year # if year, return the year number self.end.year elsif @index # if user input index, return it and save some computation @index else # find the number of intervals from start of the year start = start_date(type: :year) ((months_between(start, @date).to_f) / months_in(@type)).ceil end end |
#prev ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fiscal/period.rb', line 77 def prev if @index == 1 date = @date.change(year: @date.year - 1) index = (12 / months_in(@type)) else date = @date index = @index - 1 end self.class.new(date: date, country: @country, type: @type, index: index) end |
#start ⇒ Object
42 43 44 45 |
# File 'lib/fiscal/period.rb', line 42 def start # start date start_date end |
#to_i ⇒ Object
88 89 90 |
# File 'lib/fiscal/period.rb', line 88 def to_i number end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/fiscal/period.rb', line 92 def to_s number.to_s end |
#type ⇒ Object
37 38 39 40 |
# File 'lib/fiscal/period.rb', line 37 def type # return type, in case user decides to pass around the return object @type end |
#validate ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/fiscal/period.rb', line 28 def validate # date validation is handled by active support # country raise(FiscalError, "`#{@country}` is not a recognized country") unless self.config()[@country] # index valid_indexes = (1..(12 / months_in(type))) raise(FiscalError, "`#{@index}` is not a valid index for `#{@type}`") unless valid_indexes.include?(@index) end |