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.2.1'
- @@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.
11
12
13
14
|
# File 'lib/fiscaly.rb', line 11
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
16
17
18
19
20
21
22
|
# File 'lib/fiscaly.rb', line 16
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.
115
116
117
|
# File 'lib/fiscaly.rb', line 115
def options
@options
end
|
Instance Attribute Details
#date ⇒ Object
Returns the value of attribute date.
8
9
10
|
# File 'lib/fiscaly.rb', line 8
def date
@date
end
|
#options ⇒ Object
Returns the value of attribute options.
9
10
11
|
# File 'lib/fiscaly.rb', line 9
def options
@options
end
|
Class Method Details
.date(date, options = {}) ⇒ Object
127
128
129
|
# File 'lib/fiscaly.rb', line 127
def date(date, options = {})
self.new(date, options)
end
|
.fdate(fdate, options = {}) ⇒ Object
139
140
141
|
# File 'lib/fiscaly.rb', line 139
def fdate(fdate, options = {})
fymd(fdate.year, fdate.month, fdate.day, options)
end
|
.forward_fyear=(val) ⇒ Object
173
174
175
|
# File 'lib/fiscaly.rb', line 173
def forward_fyear=(val)
@@options[:forward_fyear] = val
end
|
.fparse(fstr, options = {}) ⇒ Object
143
144
145
146
|
# File 'lib/fiscaly.rb', line 143
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
148
149
150
|
# File 'lib/fiscaly.rb', line 148
def fymd(fyear, month = 1, day = 1, options = {})
self.new(normalize(fyear, month, day, options), options)
end
|
.global_options ⇒ Object
161
162
163
|
# File 'lib/fiscaly.rb', line 161
def global_options
@@options.merge((Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || {})
end
|
.parse(str, options = {}) ⇒ Object
131
132
133
|
# File 'lib/fiscaly.rb', line 131
def parse(str, options = {})
date(Date.parse(str), options)
end
|
.start_day=(val) ⇒ Object
169
170
171
|
# File 'lib/fiscaly.rb', line 169
def start_day=(val)
@@options[:start_day] = val
end
|
.start_month=(val) ⇒ Object
165
166
167
|
# File 'lib/fiscaly.rb', line 165
def start_month=(val)
@@options[:start_month] = val
end
|
.today(options = {}) ⇒ Object
123
124
125
|
# File 'lib/fiscaly.rb', line 123
def today(options = {})
date(Date.today, options)
end
|
.with(options = {}) ⇒ Object
152
153
154
155
156
157
158
159
|
# File 'lib/fiscaly.rb', line 152
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
135
136
137
|
# File 'lib/fiscaly.rb', line 135
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
60
61
62
63
64
65
66
67
68
|
# File 'lib/fiscaly.rb', line 60
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
96
97
98
|
# File 'lib/fiscaly.rb', line 96
def beginning_of_fmonth
Date.new(year, month, start_day)
end
|
#beginning_of_fquarter(index = nil) ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/fiscaly.rb', line 78
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
47
48
49
50
|
# File 'lib/fiscaly.rb', line 47
def beginning_of_fyear
year = forward_fyear? ? fyear - 1 : fyear
Date.new(year, start_month, start_day)
end
|
#end_of_fhalf(index = nil) ⇒ Object
70
71
72
|
# File 'lib/fiscaly.rb', line 70
def end_of_fhalf(index = nil)
beginning_of_fhalf(index) + 6.months - 1.days
end
|
#end_of_fmonth ⇒ Object
100
101
102
|
# File 'lib/fiscaly.rb', line 100
def end_of_fmonth
beginning_of_fmonth + 1.month - 1.days
end
|
#end_of_fquarter(index = nil) ⇒ Object
88
89
90
|
# File 'lib/fiscaly.rb', line 88
def end_of_fquarter(index = nil)
beginning_of_fquarter(index) + 3.months - 1.days
end
|
#end_of_fyear ⇒ Object
52
53
54
|
# File 'lib/fiscaly.rb', line 52
def end_of_fyear
beginning_of_fyear + 12.months - 1.days
end
|
#fdate ⇒ Object
43
44
45
|
# File 'lib/fiscaly.rb', line 43
def fdate
Date.new(fyear, @date.month, @date.day);
end
|
#forward_fyear? ⇒ Boolean
32
33
34
|
# File 'lib/fiscaly.rb', line 32
def forward_fyear?
@options[:forward_fyear]
end
|
#fyear ⇒ Object
36
37
38
39
40
41
|
# File 'lib/fiscaly.rb', line 36
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
74
75
76
|
# File 'lib/fiscaly.rb', line 74
def range_of_fhalf(index = nil)
beginning_of_fhalf(index)..end_of_fhalf(index)
end
|
#range_of_fmonth ⇒ Object
104
105
106
|
# File 'lib/fiscaly.rb', line 104
def range_of_fmonth
beginning_of_fmonth..end_of_fmonth
end
|
#range_of_fquarter(index = nil) ⇒ Object
92
93
94
|
# File 'lib/fiscaly.rb', line 92
def range_of_fquarter(index = nil)
beginning_of_fquarter(index)..end_of_fquarter(index)
end
|
#range_of_fyear ⇒ Object
56
57
58
|
# File 'lib/fiscaly.rb', line 56
def range_of_fyear
beginning_of_fyear..end_of_fyear
end
|
#range_of_month ⇒ Object
108
109
110
|
# File 'lib/fiscaly.rb', line 108
def range_of_month
@date.beginning_of_month..@date.end_of_month
end
|
#start_day ⇒ Object
28
29
30
|
# File 'lib/fiscaly.rb', line 28
def start_day
@options[:start_day]
end
|
#start_month ⇒ Object
24
25
26
|
# File 'lib/fiscaly.rb', line 24
def start_month
@options[:start_month]
end
|