forked from chochkov/activerecord-periodic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.rb
185 lines (159 loc) · 4.79 KB
/
span.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# This is a basic Span object that lets us do things like:
#
# Examples:
#
# Span['this year'].monthly do |month|
# # month is a Span object representing each month of the year
# # do something with it - like subperiodic reporting, time breakdowns, etc
# end
#
# Span['the last 3 months'].weekly do |week|
# Aggregation.new(week).save
# # or do something meaningful in your own context
# end
#
# This lacks some handy methods and offers place for extension in next versions.
# For exmaple, it'd be cool to have Span['last week'].week? or cweek delegation,
# etc.
#
module ActiveRecord::Periodic
class Span
include Comparable # by length
attr_accessor :beginning, :end, :group_by
def initialize(*args)
args = args.flatten
@beginning, @end =
if args.size == 2 && args.all? { |a| a.kind_of?(Time) }
[ args.first, args.last ]
elsif (text = args.first).kind_of?(String)
group_by = text.split(/\sgroup\s/i)
@group_by = group_by[1]
Parser[group_by.first].parse
end
end
class << self
alias :[] :new
end
def grouping?
!! group_by
end
# if the span is finite or not
def finite?
! infinite?
end
# if the span is inifinte or not
def infinite?
@beginning == MINUS_INFINITY || @end == INFINITY
end
# if self and other have intersections in time
def overlap?(other)
! disjoint(other)
end
# if self and other have no intersections in time
def disjoint?(other)
@end < other.beginning || other.end < @beginning
end
# is a Time object inside the span
def inside?(time)
@beginning <= time && time <= @end
end
# is a Time object outside of the span
def outside?(time)
! inside?(time)
end
# compare by length of the time span
def <=>(other)
length <=> other.length
end
# compare by @beginning and @end of this and other (stricter than == method)
def ===(other)
to_a.map(&:to_i) == other.to_a.map(&:to_i)
end
def to_a
[ @beginning, @end ]
end
def to_s
"from #{@beginning} until #{@end}"
end
def to_i
(@end - @beginning).floor + 1
end
alias :length :to_i
# Define utility methods for breaking down time spans into smaller intervals
#
# Examples:
#
# Span['last week'].days
# # an array of Span objects each representing a day from that week.
#
# Span['last month'].weekly do |week|
# # do something with that week object
# end
# # This will yield all weeks that started or ended in 'this month'
#
# Span['last month'].weekly(true) do |week|
# # ...
# end
# # This will yield the complete weeks within that period only.
# # See `breakdown_by` for examples.
#
[ :hour, :day, :week, :month, :year ].each do |aggregate|
define_method "#{aggregate}s" do |strict = false|
breakdown_by(aggregate, strict)
end
define_method "#{aggregate}ly" do |strict = false, &block|
per(aggregate, strict, &block)
end unless aggregate.eql?(:day)
define_method :daily do |strict = false, &block|
per(:day, strict, &block)
end
end
# aggegate is :hour, :day, :week, :month, :year
#
# Examples:
#
# Span['last week'].per(:day) # the same as Span['last week'].daily
#
def per(aggregate, strict = false)
group = breakdown_by(aggregate, strict)
if block_given?
group.each { |member| yield member }.size
else
group
end
end
private
# This takes an `aggregate` in :hour, :day, :week, :month, :year and an
# `strict` in true | false, default false.
#
# Returns an array of Span objects or an empty Array.
#
# Examples:
#
# Span['this month'].breakdown_by(:day)
# # an array with all days this month
#
# Span['this month'].breakdown_by(:week, true)
# # return only weeks that started and ended during 'this month'
#
def breakdown_by(aggregate, strict = false)
start = @beginning.send("beginning_of_#{aggregate}")
finish = @end.send("end_of_#{aggregate}")
if outside?(start) && strict
start = @beginning.in(1.send(aggregate)).send("beginning_of_#{aggregate}")
end
if outside?(finish) && strict
finish = @end.ago(1.send(aggregate)).send("end_of_#{aggregate}")
end
result = []
this_beginning = start
this_end = start.send("end_of_#{aggregate}")
while this_end <= finish do
result.push(Span[this_beginning, this_end])
this_beginning = this_beginning.in(1.send(aggregate))
this_end = this_beginning.send("end_of_#{aggregate}")
end
result
end
end
end