Class: Fiscaly
- Inherits:
-
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.3.0'
- @@options =
{
start_month: 4,
start_day: 1,
forward_fyear: false
}
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.date(date, options = {}) ⇒ Object
-
.fdate(fdate, options = {}) ⇒ Object
-
.forward_fyear=(val) ⇒ Object
-
.fparse(fstr, options = {}) ⇒ Object
-
.fymd(fyear, month = 1, day = 1, options = {}) ⇒ Object
-
.global_options ⇒ Object
-
.parse(str, options = {}) ⇒ Object
-
.start_day=(val) ⇒ Object
-
.start_month=(val) ⇒ Object
-
.today(options = {}) ⇒ Object
-
.with(options = {}) ⇒ Object
-
.ymd(year, month = 1, day = 1, options = {}) ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(date, options = {}) ⇒ Fiscaly
Returns a new instance of Fiscaly.
13
14
15
16
|
# File 'lib/fiscaly.rb', line 13
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
18
19
20
21
22
23
24
|
# File 'lib/fiscaly.rb', line 18
def method_missing(name, *args)
if @date.respond_to?(name)
@date.send(name, *args)
else
super
end
end
|
Class Attribute Details
.options ⇒ Object
Returns the value of attribute options.
117
118
119
|
# File 'lib/fiscaly.rb', line 117
def options
@options
end
|
Instance Attribute Details
#date ⇒ Object
Returns the value of attribute date.
10
11
12
|
# File 'lib/fiscaly.rb', line 10
def date
@date
end
|
#options ⇒ Object
Returns the value of attribute options.
11
12
13
|
# File 'lib/fiscaly.rb', line 11
def options
@options
end
|
Class Method Details
.date(date, options = {}) ⇒ Object
129
130
131
|
# File 'lib/fiscaly.rb', line 129
def date(date, options = {})
self.new(date, options)
end
|
.fdate(fdate, options = {}) ⇒ Object
141
142
143
|
# File 'lib/fiscaly.rb', line 141
def fdate(fdate, options = {})
fymd(fdate.year, fdate.month, fdate.day, options)
end
|
.forward_fyear=(val) ⇒ Object
175
176
177
|
# File 'lib/fiscaly.rb', line 175
def forward_fyear=(val)
@@options[:forward_fyear] = val
end
|
.fparse(fstr, options = {}) ⇒ Object
145
146
147
148
|
# File 'lib/fiscaly.rb', line 145
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
150
151
152
|
# File 'lib/fiscaly.rb', line 150
def fymd(fyear, month = 1, day = 1, options = {})
self.new(normalize(fyear, month, day, options), options)
end
|
.global_options ⇒ Object
163
164
165
|
# File 'lib/fiscaly.rb', line 163
def global_options
@@options.merge((Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || {})
end
|
.parse(str, options = {}) ⇒ Object
133
134
135
|
# File 'lib/fiscaly.rb', line 133
def parse(str, options = {})
date(Date.parse(str), options)
end
|
.start_day=(val) ⇒ Object
171
172
173
|
# File 'lib/fiscaly.rb', line 171
def start_day=(val)
@@options[:start_day] = val
end
|
.start_month=(val) ⇒ Object
167
168
169
|
# File 'lib/fiscaly.rb', line 167
def start_month=(val)
@@options[:start_month] = val
end
|
.today(options = {}) ⇒ Object
125
126
127
|
# File 'lib/fiscaly.rb', line 125
def today(options = {})
date(Date.today, options)
end
|
.with(options = {}) ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/fiscaly.rb', line 154
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
137
138
139
|
# File 'lib/fiscaly.rb', line 137
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_fmonth ⇒ Object
98
99
100
|
# File 'lib/fiscaly.rb', line 98
def beginning_of_fmonth
Date.new(year, month, start_day)
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_fyear ⇒ Object
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, start_day)
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) + 6.months - 1.days
end
|
#end_of_fmonth ⇒ Object
102
103
104
|
# File 'lib/fiscaly.rb', line 102
def end_of_fmonth
beginning_of_fmonth + 1.month - 1.days
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) + 3.months - 1.days
end
|
#end_of_fyear ⇒ Object
54
55
56
|
# File 'lib/fiscaly.rb', line 54
def end_of_fyear
beginning_of_fyear + 12.months - 1.days
end
|
#fdate ⇒ Object
45
46
47
|
# File 'lib/fiscaly.rb', line 45
def fdate
Date.new(fyear, @date.month, @date.day);
end
|
#forward_fyear? ⇒ Boolean
34
35
36
|
# File 'lib/fiscaly.rb', line 34
def forward_fyear?
@options[:forward_fyear]
end
|
#fyear ⇒ Object
38
39
40
41
42
43
|
# File 'lib/fiscaly.rb', line 38
def fyear
fy = @date.year
fy -= 1 if @date.month < start_month || (@date.month == start_month && @date.day < start_day)
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_fmonth ⇒ Object
106
107
108
|
# File 'lib/fiscaly.rb', line 106
def range_of_fmonth
beginning_of_fmonth..end_of_fmonth
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_fyear ⇒ Object
58
59
60
|
# File 'lib/fiscaly.rb', line 58
def range_of_fyear
beginning_of_fyear..end_of_fyear
end
|
#range_of_month ⇒ Object
110
111
112
|
# File 'lib/fiscaly.rb', line 110
def range_of_month
@date.beginning_of_month..@date.end_of_month
end
|
#start_day ⇒ Object
30
31
32
|
# File 'lib/fiscaly.rb', line 30
def start_day
@options[:start_day]
end
|
#start_month ⇒ Object
26
27
28
|
# File 'lib/fiscaly.rb', line 26
def start_month
@options[:start_month]
end
|