Class: Fiscaly

Inherits:
Object
  • Object
show all
Defined in:
lib/fiscaly.rb,
lib/fiscaly/version.rb,
lib/fiscaly/extension.rb

Defined Under Namespace

Modules: Extension

Constant Summary collapse

KEY =
:fiscaly_options
VERSION =
'1.1.0'
@@options =
{
  start_month: 4,
  forward_fyear: false
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date, options = {}) ⇒ Fiscaly

Returns a new instance of Fiscaly.



17
18
19
20
# File 'lib/fiscaly.rb', line 17

def initialize(date, options = {})
  @options = self.class.global_options.merge(options)
  @date = date
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/fiscaly.rb', line 22

def method_missing(name, *args)
  if @date.respond_to?(name)
    @date.send(name, *args)
  else
    super
  end
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



14
15
16
# File 'lib/fiscaly.rb', line 14

def date
  @date
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/fiscaly.rb', line 15

def options
  @options
end

Class Method Details

.date(date, options = {}) ⇒ Object



109
110
111
# File 'lib/fiscaly.rb', line 109

def date(date, options = {})
  self.new(date, options)
end

.fdate(fdate, options = {}) ⇒ Object



121
122
123
# File 'lib/fiscaly.rb', line 121

def fdate(fdate, options = {})
  fymd(fdate.year, fdate.month, fdate.day, options)
end

.forward_fyear=(val) ⇒ Object



151
152
153
# File 'lib/fiscaly.rb', line 151

def forward_fyear=(val)
  @@options[:forward_fyear] = val
end

.fparse(fstr, options = {}) ⇒ Object



125
126
127
128
# File 'lib/fiscaly.rb', line 125

def fparse(fstr, options = {})
  parsed = Date._parse(fstr)
  fymd(parsed[:year], parsed[:mon], parsed[:mday], options)
end

.fymd(fyear, month = 1, day = 1, options = {}) ⇒ Object



130
131
132
# File 'lib/fiscaly.rb', line 130

def fymd(fyear, month = 1, day = 1, options = {})
  self.new(normalize(fyear, month, day, options), options)
end

.global_optionsObject



143
144
145
# File 'lib/fiscaly.rb', line 143

def global_options
  @@options.merge((Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || {})
end

.parse(str, options = {}) ⇒ Object



113
114
115
# File 'lib/fiscaly.rb', line 113

def parse(str, options = {})
  date(Date.parse(str), options)
end

.start_month=(val) ⇒ Object



147
148
149
# File 'lib/fiscaly.rb', line 147

def start_month=(val)
  @@options[:start_month] = val
end

.today(options = {}) ⇒ Object



105
106
107
# File 'lib/fiscaly.rb', line 105

def today(options = {})
  date(Date.today, options)
end

.with(options = {}) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/fiscaly.rb', line 134

def with(options = {})
  Thread.current[KEY] ||= []
  Thread.current[KEY].push(options)
  yield
ensure
  Thread.current[KEY].pop
  Thread.current[KEY] = nil if Thread.current[KEY].empty?
end

.ymd(year, month = 1, day = 1, options = {}) ⇒ Object



117
118
119
# File 'lib/fiscaly.rb', line 117

def ymd(year, month = 1, day = 1, options = {})
  date(Date.new(year, month, day), options)
end

Instance Method Details

#beginning_of_fhalf(index = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/fiscaly.rb', line 62

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
  end
end

#beginning_of_fquarter(index = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/fiscaly.rb', line 80

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
  end
end

#beginning_of_fyearObject



49
50
51
52
# File 'lib/fiscaly.rb', line 49

def beginning_of_fyear
  year = forward_fyear? ? fyear - 1 : fyear
  Date.new(year, start_month)
end

#end_of_fhalf(index = nil) ⇒ Object



72
73
74
# File 'lib/fiscaly.rb', line 72

def end_of_fhalf(index = nil)
  (beginning_of_fhalf(index) + 5.months).end_of_month
end

#end_of_fquarter(index = nil) ⇒ Object



90
91
92
# File 'lib/fiscaly.rb', line 90

def end_of_fquarter(index = nil)
  (beginning_of_fquarter(index) + 2.months).end_of_month
end

#end_of_fyearObject



54
55
56
# File 'lib/fiscaly.rb', line 54

def end_of_fyear
  (beginning_of_fyear + 11.months).end_of_month
end

#fdateObject



45
46
47
# File 'lib/fiscaly.rb', line 45

def fdate
  Date.new(fyear, @date.month, @date.day);
end

#forward_fyear?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/fiscaly.rb', line 34

def forward_fyear?
  @options[:forward_fyear]
end

#fyearObject



38
39
40
41
42
43
# File 'lib/fiscaly.rb', line 38

def fyear
  fy = @date.year
  fy -= 1 if @date.month < start_month
  fy += 1 if forward_fyear?
  fy
end

#range_of_fhalf(index = nil) ⇒ Object



76
77
78
# File 'lib/fiscaly.rb', line 76

def range_of_fhalf(index = nil)
  beginning_of_fhalf(index)..end_of_fhalf(index)
end

#range_of_fquarter(index = nil) ⇒ Object



94
95
96
# File 'lib/fiscaly.rb', line 94

def range_of_fquarter(index = nil)
  beginning_of_fquarter(index)..end_of_fquarter(index)
end

#range_of_fyearObject



58
59
60
# File 'lib/fiscaly.rb', line 58

def range_of_fyear
  beginning_of_fyear..end_of_fyear
end

#range_of_monthObject



98
99
100
# File 'lib/fiscaly.rb', line 98

def range_of_month
  @date.beginning_of_month..@date.end_of_month
end

#start_monthObject



30
31
32
# File 'lib/fiscaly.rb', line 30

def start_month
  @options[:start_month]
end