Skip to content

Commit 7faee00

Browse files
authored
Merge pull request #4 from richmolj/master
Accomodate auto-parsing stats
2 parents d22054a + d349b9b commit 7faee00

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

lib/jsonapi_compliable/stats/payload.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def generate
1212
@directive.each_pair do |name, calculation|
1313
stats[name] = {}
1414

15-
each_calculation(name, calculation) do |calc, function|
15+
each_calculation(name, parse_calculation(calculation)) do |calc, function|
1616
stats[name][calc] = function.call(@scope, name)
1717
end
1818
end
@@ -21,14 +21,20 @@ def generate
2121

2222
private
2323

24-
def each_calculation(name, calculation_string)
25-
calculations = calculation_string.split(',').map(&:to_sym)
26-
24+
def each_calculation(name, calculations)
2725
calculations.each do |calc|
2826
function = @dsl.stat(name, calc)
2927
yield calc, function
3028
end
3129
end
30+
31+
def parse_calculation(calculation)
32+
if calculation.is_a?(String)
33+
calculation.split(',').map(&:to_sym)
34+
else
35+
calculation.map(&:to_sym)
36+
end
37+
end
3238
end
3339
end
3440
end

lib/jsonapi_compliable/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JsonapiCompliable
2-
VERSION = "0.3.9"
2+
VERSION = "0.3.10"
33
end

spec/stats/payload_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ def stub_stat(attr, calc, result)
3131
attr2: { maximum: 3 }
3232
})
3333
end
34+
35+
# We may be auto-parsing comma-delimited values
36+
context 'when the calculation string is already parsed' do
37+
let(:params) { { stats: { attr1: ['count', 'average'] } } }
38+
39+
it 'generates correct payload' do
40+
expect(subject).to eq(attr1: { count: 2, average: 1 })
41+
end
42+
end
3443
end
3544

3645
describe '.new' do
@@ -47,8 +56,7 @@ def stub_stat(attr, calc, result)
4756

4857
it 'uses that scope' do
4958
instance = described_class.new(controller, 'a')
50-
expect(instance.instance_variable_get(:@scope))
51-
.to eq('a')
59+
expect(instance.instance_variable_get(:@scope)).to eq('a')
5260
end
5361
end
5462
end

0 commit comments

Comments
 (0)