summarily 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/summarily.rb +1 -0
- data/lib/summarily/class_methods.rb +13 -0
- data/lib/summarily/merge_strategy.rb +4 -0
- data/lib/summarily/version.rb +1 -1
- data/spec/summarily/class_methods_spec.rb +26 -9
- data/spec/summarily/merge_strategy_spec.rb +8 -0
- data/spec/summarily_spec.rb +9 -0
- 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: 799c0a064560e4b22c186ddb62a97a29d054f661
|
4
|
+
data.tar.gz: c659c5fb88793aea9e7869d9b826d985215d18e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b32044a6d6648ab964acf96755aab649bee64fc30fa4c7ce630e87237d32403020e7cff7bf05cbdacae918683fd5c78e7db64da27063c56c5fbc82736f9cb00
|
7
|
+
data.tar.gz: eeeeb25242c09f946f6eaeb5300d0e9ad392aeae752e1d9752dd6e9aae4f62116115843873bc39631033a2c86639d2740ad15042299628548307d5a4a5334870
|
data/lib/summarily.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'summarily'
|
2
|
+
|
1
3
|
module Summarily
|
2
4
|
|
3
5
|
module ClassMethods
|
@@ -25,6 +27,17 @@ module Summarily
|
|
25
27
|
|
26
28
|
merged_instance
|
27
29
|
end
|
30
|
+
|
31
|
+
define_method :merge! do |other|
|
32
|
+
strategy.merge_operations.each do |attr, operation|
|
33
|
+
fail RuntimeError, 'Use of merge during merge!'if operation == Summarily::MERGE
|
34
|
+
new_value = operation.call(self.send(attr), other.send(attr))
|
35
|
+
self.send(:"#{attr}=", new_value)
|
36
|
+
end
|
37
|
+
self.num_samples += other.num_samples
|
38
|
+
|
39
|
+
self
|
40
|
+
end
|
28
41
|
end
|
29
42
|
end
|
30
43
|
|
data/lib/summarily/version.rb
CHANGED
@@ -21,19 +21,36 @@ describe Summarily::ClassMethods do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
describe '#merge' do
|
25
|
+
let(:a) { Summable.new(total: 1) }
|
26
|
+
let(:b) { Summable.new(total: 2) }
|
27
|
+
let(:merged) { a.merge(b) }
|
26
28
|
|
27
|
-
|
29
|
+
it 'defines a merge strategy for an object with attributes' do
|
30
|
+
expect(merged).to eq(Summable.new(total: 3))
|
31
|
+
end
|
28
32
|
|
29
|
-
|
30
|
-
|
33
|
+
it 'defines a num_samples for an object' do
|
34
|
+
expect(a.num_samples).to eq 1
|
35
|
+
expect(b.num_samples).to eq 1
|
36
|
+
expect(merged.num_samples).to eq 2
|
37
|
+
end
|
31
38
|
end
|
32
39
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
40
|
+
describe '#merge!' do
|
41
|
+
let(:c) { Summable.new(total: 1) }
|
42
|
+
let(:d) { Summable.new(total: 2) }
|
43
|
+
before { c.merge!(d) }
|
44
|
+
|
45
|
+
it 'performs a mutating merge to left on attribute' do
|
46
|
+
expect(c.total).to eq(3)
|
47
|
+
expect(d.total).to eq(2)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'defines a num_samples for an object' do
|
51
|
+
expect(c.num_samples).to eq 2
|
52
|
+
expect(d.num_samples).to eq 1
|
53
|
+
end
|
37
54
|
end
|
38
55
|
end
|
39
56
|
|
@@ -35,6 +35,14 @@ describe Summarily::MergeStrategy do
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
describe '#merge!(attr)' do
|
39
|
+
before { subject.merge!(attr) }
|
40
|
+
|
41
|
+
it "sets the attribute's merge! operation" do
|
42
|
+
expect(subject.merge_operations[attr]).to eq(Summarily::MERGE_LEFT)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
38
46
|
describe '#union(attr)' do
|
39
47
|
before { subject.union(attr) }
|
40
48
|
|
data/spec/summarily_spec.rb
CHANGED
@@ -29,6 +29,15 @@ describe Summarily do
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
describe 'MERGE_LEFT' do
|
33
|
+
it 'mutates the left side argument in place' do
|
34
|
+
a = { foo: 3 }
|
35
|
+
b = { foo: 4 }
|
36
|
+
expect(Summarily::MERGE_LEFT.call(a, b)).to eq(b)
|
37
|
+
expect(a).to eq(b)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
32
41
|
describe 'UNION' do
|
33
42
|
it 'returns the union of two arrays' do
|
34
43
|
expect(Summarily::UNION.call([1,2],[2,3])).to match_array([1,2,3])
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: summarily
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Gillooly
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|