Class: Fiscal::FiscalPeriod

Inherits:
Object
  • Object
show all
Includes:
FiscalConfig
Defined in:
lib/fiscal/period.rb

Defined Under Namespace

Classes: FiscalError

Instance Method Summary collapse

Methods included from FiscalConfig

#config

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(options = {})

  @type    = (options[:type])
  @date    = (options[:date] || Date.today).to_date
  @country = (options[:country] || :nil).to_sym

  if options[:index]
    # user input
    @index = options[: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

#endObject



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

#nextObject



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

#numberObject



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

#prevObject



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

#startObject



42
43
44
45
# File 'lib/fiscal/period.rb', line 42

def start
  # start date
  start_date
end

#to_iObject



88
89
90
# File 'lib/fiscal/period.rb', line 88

def to_i
  number
end

#to_sObject



92
93
94
# File 'lib/fiscal/period.rb', line 92

def to_s
  number.to_s
end

#typeObject



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

#validateObject

Raises:



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