summarily 0.0.1
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 +7 -0
- data/.gitignore +23 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/lib/summarily/class_methods.rb +23 -0
- data/lib/summarily/merge_strategy.rb +41 -0
- data/lib/summarily/version.rb +3 -0
- data/lib/summarily.rb +13 -0
- data/spec/summarily/class_methods_spec.rb +32 -0
- data/spec/summarily/merge_strategy_spec.rb +61 -0
- data/spec/summarily_spec.rb +38 -0
- data/summarily.gemspec +24 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37e801b452a2d2952786393339d8cfc35fe68f5e
|
4
|
+
data.tar.gz: 2ad0f1e848c57396d4b64ff8c11f35088096bb7a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85a61641ab26a7c6d8a0aa6e0d8374fafd74478fa192882ce15d20dbe3c85c318414348faf5d104c5481042f68101c5bdc81108d5a4581fcf4172064da8fa845
|
7
|
+
data.tar.gz: dba53b9c9dc9e7d85e6fb0769a15bf5c7839d987749699610eb00661e83a9b62c51690ffaabf82e71e7fc650dbebc6a1a02b27e754e35e6ea2a734d94b802af3
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.*.swp
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Summarily
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'summarily'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install summarily
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/summarily/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Summarily
|
2
|
+
|
3
|
+
module ClassMethods
|
4
|
+
def summarily
|
5
|
+
strategy = MergeStrategy.new
|
6
|
+
yield strategy
|
7
|
+
|
8
|
+
define_method :merge do |other|
|
9
|
+
merged = {}
|
10
|
+
|
11
|
+
strategy.merge_operations.each do |attr, operation|
|
12
|
+
merged[attr] = operation.call(
|
13
|
+
self.send(attr),
|
14
|
+
other.send(attr)
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
self.class.new(merged)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Summarily
|
2
|
+
|
3
|
+
class MergeStrategy
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@merge_operations = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :merge_operations
|
10
|
+
|
11
|
+
def sum(attr)
|
12
|
+
@merge_operations[attr] = Summarily::SUM
|
13
|
+
end
|
14
|
+
|
15
|
+
def min(attr)
|
16
|
+
@merge_operations[attr] = Summarily::MIN
|
17
|
+
end
|
18
|
+
|
19
|
+
def max(attr)
|
20
|
+
@merge_operations[attr] = Summarily::MAX
|
21
|
+
end
|
22
|
+
|
23
|
+
def merge(attr)
|
24
|
+
@merge_operations[attr] = Summarily::MERGE
|
25
|
+
end
|
26
|
+
|
27
|
+
def union(attr)
|
28
|
+
@merge_operations[attr] = Summarily::UNION
|
29
|
+
end
|
30
|
+
|
31
|
+
def custom(attr, &block)
|
32
|
+
@merge_operations[attr] = block
|
33
|
+
end
|
34
|
+
|
35
|
+
# TODO:
|
36
|
+
# * min_by(attr, &block)
|
37
|
+
# * max_by(attr, &block)
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/lib/summarily.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'summarily/version'
|
2
|
+
require 'summarily/class_methods'
|
3
|
+
require 'summarily/merge_strategy'
|
4
|
+
|
5
|
+
module Summarily
|
6
|
+
|
7
|
+
SUM = Proc.new{|a, b| a + b}
|
8
|
+
MIN = Proc.new{|a, b| a < b ? a : b}
|
9
|
+
MAX = Proc.new{|a, b| a > b ? a : b}
|
10
|
+
MERGE = Proc.new{|a, b| a.merge(b)}
|
11
|
+
UNION = Proc.new{|a, b| (a + b).uniq}
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'summarily/class_methods'
|
2
|
+
require 'virtus'
|
3
|
+
|
4
|
+
describe Summarily::ClassMethods do
|
5
|
+
|
6
|
+
class Summable
|
7
|
+
include Virtus.value_object
|
8
|
+
extend Summarily::ClassMethods
|
9
|
+
|
10
|
+
values do
|
11
|
+
attribute :total, Integer
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
subject { Summable.new }
|
16
|
+
|
17
|
+
describe '#summarily' do
|
18
|
+
before do
|
19
|
+
Summable.summarily do |s|
|
20
|
+
s.sum :total
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:a) { Summable.new(total: 1) }
|
25
|
+
let(:b) { Summable.new(total: 2) }
|
26
|
+
|
27
|
+
it 'defines a merge strategy for an object with attributes' do
|
28
|
+
expect(a.merge(b)).to eq(Summable.new(total: 3))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'summarily/merge_strategy'
|
2
|
+
|
3
|
+
describe Summarily::MergeStrategy do
|
4
|
+
let(:attr) { :foo }
|
5
|
+
|
6
|
+
describe '#sum(attr)' do
|
7
|
+
before { subject.sum(attr) }
|
8
|
+
|
9
|
+
it "sets the attribute's merge operation" do
|
10
|
+
expect(subject.merge_operations[attr]).to eq(Summarily::SUM)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#min(attr)' do
|
15
|
+
before { subject.min(attr) }
|
16
|
+
|
17
|
+
it "sets the attribute's merge operation" do
|
18
|
+
expect(subject.merge_operations[attr]).to eq(Summarily::MIN)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#max(attr)' do
|
23
|
+
before { subject.max(attr) }
|
24
|
+
|
25
|
+
it "sets the attribute's merge operation" do
|
26
|
+
expect(subject.merge_operations[attr]).to eq(Summarily::MAX)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#merge(attr)' do
|
31
|
+
before { subject.merge(attr) }
|
32
|
+
|
33
|
+
it "sets the attribute's merge operation" do
|
34
|
+
expect(subject.merge_operations[attr]).to eq(Summarily::MERGE)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#union(attr)' do
|
39
|
+
before { subject.union(attr) }
|
40
|
+
|
41
|
+
it "sets the attribute's merge operation" do
|
42
|
+
expect(subject.merge_operations[attr]).to eq(Summarily::UNION)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#custom(attr, &block)' do
|
47
|
+
let(:my_proc) do
|
48
|
+
Proc.new do |a, b|
|
49
|
+
a * b
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
before { subject.custom(attr, &my_proc) }
|
54
|
+
|
55
|
+
it "sets the attribute's merge operation" do
|
56
|
+
expect(subject.merge_operations[attr]).to eq(my_proc)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#def custom(attr, &block)
|
61
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'summarily'
|
2
|
+
|
3
|
+
describe Summarily do
|
4
|
+
|
5
|
+
describe 'SUM' do
|
6
|
+
it 'adds two numbers' do
|
7
|
+
expect(Summarily::SUM.call(1,2)).to eq(3)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'MIN' do
|
12
|
+
it 'returns the lesser of two numbers' do
|
13
|
+
expect(Summarily::MIN.call(1,2)).to eq(1)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'MAX' do
|
18
|
+
it 'returns the greater of two numbers' do
|
19
|
+
expect(Summarily::MAX.call(1,2)).to eq(2)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'MERGE' do
|
24
|
+
it 'returns the merged result of two objects' do
|
25
|
+
expect(Summarily::MERGE.call({foo: 3}, {bar: 4})).to eq({
|
26
|
+
foo: 3,
|
27
|
+
bar: 4,
|
28
|
+
})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'UNION' do
|
33
|
+
it 'returns the union of two arrays' do
|
34
|
+
expect(Summarily::UNION.call([1,2],[2,3])).to match_array([1,2,3])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/summarily.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'summarily/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "summarily"
|
8
|
+
spec.version = Summarily::VERSION
|
9
|
+
spec.authors = ["Matt Gillooly"]
|
10
|
+
spec.email = ["matt@swipely.com"]
|
11
|
+
spec.summary = %q{DSL for merging stats objects}
|
12
|
+
spec.description = %q{Define merge strategies for combining sets of stats}
|
13
|
+
spec.homepage = "http://github.com/swipely/summarily"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
spec.add_development_dependency "virtus"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: summarily
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Gillooly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: virtus
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Define merge strategies for combining sets of stats
|
70
|
+
email:
|
71
|
+
- matt@swipely.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/summarily.rb
|
82
|
+
- lib/summarily/class_methods.rb
|
83
|
+
- lib/summarily/merge_strategy.rb
|
84
|
+
- lib/summarily/version.rb
|
85
|
+
- spec/summarily/class_methods_spec.rb
|
86
|
+
- spec/summarily/merge_strategy_spec.rb
|
87
|
+
- spec/summarily_spec.rb
|
88
|
+
- summarily.gemspec
|
89
|
+
homepage: http://github.com/swipely/summarily
|
90
|
+
licenses: []
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.0
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: DSL for merging stats objects
|
112
|
+
test_files:
|
113
|
+
- spec/summarily/class_methods_spec.rb
|
114
|
+
- spec/summarily/merge_strategy_spec.rb
|
115
|
+
- spec/summarily_spec.rb
|