ruby_reportable 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/ruby_reportable/report.rb +53 -24
- data/lib/ruby_reportable/version.rb +1 -1
- data/spec/report_spec.rb +56 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 105e1e74042eaae1eaa26a163b12e78fd79212fe
|
4
|
+
data.tar.gz: 3f617be2415d29d5d1df070a87acf616faa06548
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99b6e66e7c4b6bb142c24abc7218d28b7668c8f927d0a03d736f57abe6d0dc9867eff501a45c9fa66d2111c6ce90c2d4f4ca0e35750601bf2683cf53254611e6
|
7
|
+
data.tar.gz: 371b88318c0cd2d1d2ba3922ef2beb03178a6a0a6684bf262ed9cc8e95dc0cdc5e5a06dc193ff443bda10767d79ad80d62e49a26039714d80117078dc52a53ce
|
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ require 'benchmark'
|
|
2
2
|
|
3
3
|
module RubyReportable
|
4
4
|
module Report
|
5
|
-
attr_accessor :data_source, :filters
|
5
|
+
attr_accessor :data_source, :filters, :records_returned
|
6
6
|
|
7
7
|
def clear
|
8
8
|
@outputs = []
|
@@ -12,6 +12,7 @@ module RubyReportable
|
|
12
12
|
@category = 'Reports'
|
13
13
|
@meta = {}
|
14
14
|
@benchmarks = {}
|
15
|
+
@records_returned = 0
|
15
16
|
end
|
16
17
|
|
17
18
|
# :sandbox, :filters, :finalize, :output, :group, :sort
|
@@ -137,12 +138,12 @@ module RubyReportable
|
|
137
138
|
# build sandbox for building outputs
|
138
139
|
sandbox = RubyReportable::Sandbox.new(:meta => @meta, @data_source[:as] => nil)
|
139
140
|
|
140
|
-
source_data.inject(
|
141
|
+
source_data.inject([]) do |rows, element|
|
141
142
|
# fill sandbox with data element
|
142
143
|
sandbox[@data_source[:as]] = element
|
143
144
|
|
144
145
|
# grab outputs
|
145
|
-
rows
|
146
|
+
rows << @outputs.inject({}) do |row, output|
|
146
147
|
row[output.name] = sandbox.instance_eval(&output.logic)
|
147
148
|
row
|
148
149
|
end
|
@@ -151,27 +152,51 @@ module RubyReportable
|
|
151
152
|
end
|
152
153
|
end
|
153
154
|
|
154
|
-
def
|
155
|
-
|
156
|
-
data[:results].inject({}) do |hash, element|
|
157
|
-
key = element[group]
|
158
|
-
hash[key] ||= []
|
159
|
-
hash[key] << element
|
160
|
-
hash
|
161
|
-
end
|
162
|
-
else
|
155
|
+
def _sort(sort, data, options = {})
|
156
|
+
if sort.to_s.empty?
|
163
157
|
data
|
158
|
+
else
|
159
|
+
data.sort_by {|element| element[sort]}
|
164
160
|
end
|
165
161
|
end
|
166
162
|
|
167
|
-
def
|
168
|
-
|
169
|
-
|
170
|
-
|
163
|
+
def _group(group, data, options = {})
|
164
|
+
# Run through elements in data which are ordered
|
165
|
+
# from the previous call to #_sort via #run
|
166
|
+
#
|
167
|
+
# Since the elements are already sorted, as we pop
|
168
|
+
# them into their grouping they will remain sorted as
|
169
|
+
# intended
|
170
|
+
#
|
171
|
+
if group.to_s.empty?
|
172
|
+
{:results => data}
|
173
|
+
else
|
174
|
+
group = [group] unless group.is_a?(Array)
|
175
|
+
group.map!(&:to_s)
|
176
|
+
|
177
|
+
# the last group critieria should contain an array
|
178
|
+
# so lets pop it off for special use
|
179
|
+
last_group = group.pop
|
180
|
+
|
181
|
+
data.inject({}) do |hash, element|
|
182
|
+
# grab a local reference to the hash
|
183
|
+
ref = hash
|
184
|
+
|
185
|
+
# run through initial groupings to grab local ref
|
186
|
+
# and default them to {}
|
187
|
+
group.map do |grouping|
|
188
|
+
key = element[grouping]
|
189
|
+
ref[key] ||= {}
|
190
|
+
ref = ref[key]
|
191
|
+
end
|
192
|
+
|
193
|
+
# handle our last grouping
|
194
|
+
key = element[last_group]
|
195
|
+
ref[key] ||= []
|
196
|
+
ref[key] << element
|
197
|
+
|
171
198
|
hash
|
172
199
|
end
|
173
|
-
else
|
174
|
-
data
|
175
200
|
end
|
176
201
|
end
|
177
202
|
|
@@ -198,14 +223,18 @@ module RubyReportable
|
|
198
223
|
_output(source_data, options)
|
199
224
|
end
|
200
225
|
|
201
|
-
#
|
202
|
-
|
203
|
-
|
226
|
+
# now that we have all of our data go ahead and cache the size
|
227
|
+
records_returned = data.size
|
228
|
+
|
229
|
+
# sort the data first cause that makes sense you know
|
230
|
+
sorted = benchmark(:sort) do
|
231
|
+
_sort(options[:sort], data, options)
|
204
232
|
end
|
205
233
|
|
206
|
-
#
|
207
|
-
|
208
|
-
|
234
|
+
# transform into {group => {group => [outputs => values]}}
|
235
|
+
# level of grouping depends on how many groups are passed in
|
236
|
+
benchmark(:group) do
|
237
|
+
_group(options[:group], sorted, options)
|
209
238
|
end
|
210
239
|
end # end def run
|
211
240
|
|
data/spec/report_spec.rb
CHANGED
@@ -135,6 +135,61 @@ describe RubyReportable::Report do
|
|
135
135
|
|
136
136
|
@report._group('letter', @report._output(source_data)).should == {"m" => [{'name' => :method, 'letter' => 'm'}]}
|
137
137
|
end
|
138
|
+
|
139
|
+
it "should handle multiple groupings" do
|
140
|
+
|
141
|
+
raw = []
|
142
|
+
raw[0] = ['test1', 29201, 'road']
|
143
|
+
raw[1] = ['test2', 29201, 'road']
|
144
|
+
raw[2] = ['test3', 29201, 'drive']
|
145
|
+
raw[3] = ['test4', 29204, 'road']
|
146
|
+
|
147
|
+
output = []
|
148
|
+
[0, 1, 2, 3].map do |index|
|
149
|
+
output[index] = {
|
150
|
+
'name' => raw[index][0],
|
151
|
+
'zip' => raw[index][1],
|
152
|
+
'path' => raw[index][2]
|
153
|
+
}
|
154
|
+
end
|
155
|
+
|
156
|
+
result = {
|
157
|
+
29201 => {
|
158
|
+
'road' => [output[0], output[1]],
|
159
|
+
'drive' => [output[2]]
|
160
|
+
},
|
161
|
+
29204 => {
|
162
|
+
'road' => [output[3]]
|
163
|
+
}
|
164
|
+
}
|
165
|
+
|
166
|
+
@report.source do
|
167
|
+
logic do
|
168
|
+
[
|
169
|
+
raw[0],
|
170
|
+
raw[1],
|
171
|
+
raw[2],
|
172
|
+
raw[3]
|
173
|
+
]
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
@report.output('name') do
|
178
|
+
element[0]
|
179
|
+
end
|
180
|
+
|
181
|
+
@report.output('zip') do
|
182
|
+
element[1]
|
183
|
+
end
|
184
|
+
|
185
|
+
@report.output('path') do
|
186
|
+
element[2]
|
187
|
+
end
|
188
|
+
|
189
|
+
source_data = @report._data(@report._source).source
|
190
|
+
|
191
|
+
@report._group(['zip', 'path'], @report._output(source_data)).should == result
|
192
|
+
end
|
138
193
|
end
|
139
194
|
|
140
195
|
context "#_sort" do
|
@@ -152,7 +207,7 @@ describe RubyReportable::Report do
|
|
152
207
|
source_data = @report._data(@report._source).source
|
153
208
|
final = @report._sort('name', @report._output(source_data))
|
154
209
|
|
155
|
-
final.should ==
|
210
|
+
final.should == Object.methods.sort.map {|method| {'name' => method}}
|
156
211
|
end
|
157
212
|
end
|
158
213
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_reportable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John 'asceth' Long
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|