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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0aa188dcb5de88cfce998b016a61c2b52953d77
4
- data.tar.gz: d58a97a134d2303e745c24912b4438ddf47b36f5
3
+ metadata.gz: 799c0a064560e4b22c186ddb62a97a29d054f661
4
+ data.tar.gz: c659c5fb88793aea9e7869d9b826d985215d18e5
5
5
  SHA512:
6
- metadata.gz: cd0e85fb3afcccf53d84b17f3fea8bca066878d3ab45cdc37a818b602cabc6d4f7fb8fab6d5d670be6ecf0d77ee12249fa690390b0a097f36d6e38d3921fdaa7
7
- data.tar.gz: 81ed0f9718c1e8cce5adb99880e6838a5e2abb7464449d1686624201955d61d65479dd375bd2dfff8529d9365212690c0f172faa11021dfb643c00aad3414662
6
+ metadata.gz: 3b32044a6d6648ab964acf96755aab649bee64fc30fa4c7ce630e87237d32403020e7cff7bf05cbdacae918683fd5c78e7db64da27063c56c5fbc82736f9cb00
7
+ data.tar.gz: eeeeb25242c09f946f6eaeb5300d0e9ad392aeae752e1d9752dd6e9aae4f62116115843873bc39631033a2c86639d2740ad15042299628548307d5a4a5334870
data/lib/summarily.rb CHANGED
@@ -8,6 +8,7 @@ module Summarily
8
8
  MIN = Proc.new{|a, b| a < b ? a : b}
9
9
  MAX = Proc.new{|a, b| a > b ? a : b}
10
10
  MERGE = Proc.new{|a, b| a.merge(b)}
11
+ MERGE_LEFT = Proc.new{|a, b| a.merge!(b)}
11
12
  UNION = Proc.new{|a, b| (a + b).uniq}
12
13
 
13
14
  end
@@ -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
 
@@ -24,6 +24,10 @@ module Summarily
24
24
  @merge_operations[attr] = Summarily::MERGE
25
25
  end
26
26
 
27
+ def merge!(attr)
28
+ @merge_operations[attr] = Summarily::MERGE_LEFT
29
+ end
30
+
27
31
  def union(attr)
28
32
  @merge_operations[attr] = Summarily::UNION
29
33
  end
@@ -1,3 +1,3 @@
1
1
  module Summarily
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -21,19 +21,36 @@ describe Summarily::ClassMethods do
21
21
  end
22
22
  end
23
23
 
24
- let(:a) { Summable.new(total: 1) }
25
- let(:b) { Summable.new(total: 2) }
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
- let(:merged) { a.merge(b) }
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
- it 'defines a merge strategy for an object with attributes' do
30
- expect(merged).to eq(Summable.new(total: 3))
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
- 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
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
 
@@ -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.2
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-03 00:00:00.000000000 Z
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler