match_reduce 1.0.0.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +8 -0
- data/.gitignore +6 -0
- data/.rubocop.yml +23 -0
- data/.ruby-version +1 -0
- data/.travis.yml +25 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -0
- data/Guardfile +22 -0
- data/LICENSE +7 -0
- data/README.md +247 -0
- data/Rakefile +17 -0
- data/bin/console +18 -0
- data/lib/match_reduce/aggregate.rb +61 -0
- data/lib/match_reduce/any.rb +30 -0
- data/lib/match_reduce/index.rb +53 -0
- data/lib/match_reduce/processor/result.rb +37 -0
- data/lib/match_reduce/processor/result_builder.rb +55 -0
- data/lib/match_reduce/processor/results_builder.rb +40 -0
- data/lib/match_reduce/processor.rb +78 -0
- data/lib/match_reduce/version.rb +12 -0
- data/lib/match_reduce.rb +35 -0
- data/match_reduce.gemspec +35 -0
- data/spec/fixtures/snapshots/processor/abstract.yaml +68 -0
- data/spec/fixtures/snapshots/processor/teams_and_players.yaml +173 -0
- data/spec/match_reduce/aggregate_spec.rb +32 -0
- data/spec/match_reduce/any_spec.rb +32 -0
- data/spec/match_reduce/index_spec.rb +93 -0
- data/spec/match_reduce/processor/result_spec.rb +48 -0
- data/spec/match_reduce/processor_spec.rb +62 -0
- data/spec/match_reduce_spec.rb +39 -0
- data/spec/spec_helper.rb +52 -0
- metadata +232 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1c3c23c5467e8d1b83480b9440ace8237b05e588a5ea8e0b34212c5130ec2e29
|
4
|
+
data.tar.gz: f7a93d0e34f59b842d1a52e9990f6be7d9901e8a22b55c7af024c52475229c8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a0bb6158c5e8a48f279eaaa1c8c8fcaa36c6b48f5b7232a64f6ecba2949e46ea53fc6194c6095ae6fc6c39b8e45fc44b7689de1ca00e1589dbc88a580c0a330f
|
7
|
+
data.tar.gz: dfe1f78b5036761f777f978d0ee2075a06364b356722f14ca74c8a03b873cc2df227aa286cf69258146e2a0688ba6c875a84ed5b64b30286c06694300f287613
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Metrics/LineLength:
|
2
|
+
Max: 100
|
3
|
+
|
4
|
+
Metrics/BlockLength:
|
5
|
+
ExcludedMethods:
|
6
|
+
- let
|
7
|
+
- it
|
8
|
+
- describe
|
9
|
+
- context
|
10
|
+
- specify
|
11
|
+
- define
|
12
|
+
|
13
|
+
Metrics/MethodLength:
|
14
|
+
Max: 25
|
15
|
+
|
16
|
+
AllCops:
|
17
|
+
TargetRubyVersion: 2.3
|
18
|
+
|
19
|
+
Metrics/AbcSize:
|
20
|
+
Max: 16
|
21
|
+
|
22
|
+
Metrics/ClassLength:
|
23
|
+
Max: 125
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
env:
|
2
|
+
global:
|
3
|
+
- CC_TEST_REPORTER_ID=3a2a656a2bfbb6accb5058cabf72d576b506eb2211f8dcc555a79cfcab27563b
|
4
|
+
language: ruby
|
5
|
+
rvm:
|
6
|
+
# Build on the latest stable of all supported Rubies (https://www.ruby-lang.org/en/downloads/):
|
7
|
+
- 2.3.8
|
8
|
+
- 2.4.6
|
9
|
+
- 2.5.5
|
10
|
+
- 2.6.3
|
11
|
+
cache: bundler
|
12
|
+
before_script:
|
13
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
14
|
+
- chmod +x ./cc-test-reporter
|
15
|
+
- ./cc-test-reporter before-build
|
16
|
+
script:
|
17
|
+
- bundle exec rubocop
|
18
|
+
- bundle exec rspec spec --format documentation
|
19
|
+
after_script:
|
20
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
21
|
+
addons:
|
22
|
+
# https://docs.travis-ci.com/user/uploading-artifacts/
|
23
|
+
artifacts:
|
24
|
+
paths:
|
25
|
+
- Gemfile.lock
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at oss@bluemarblepayroll.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
guard :rspec, cmd: 'DISABLE_SIMPLECOV=true bundle exec rspec --format=documentation' do
|
4
|
+
require 'guard/rspec/dsl'
|
5
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
6
|
+
|
7
|
+
# RSpec files
|
8
|
+
rspec = dsl.rspec
|
9
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
11
|
+
watch(rspec.spec_files)
|
12
|
+
|
13
|
+
watch(%r{^spec/fixtures/snapshots(.*)(\.yaml)$}) do |m|
|
14
|
+
spec_file = m[1].split('/')[0...-1].join('/')
|
15
|
+
|
16
|
+
File.join('spec', 'match_reduce', "#{spec_file}_spec.rb")
|
17
|
+
end
|
18
|
+
|
19
|
+
# Ruby files
|
20
|
+
ruby = dsl.ruby
|
21
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
22
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2019 Blue Marble Payroll, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
# MatchReduce
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/match_reduce.svg)](https://badge.fury.io/rb/match_reduce) [![Build Status](https://travis-ci.org/bluemarblepayroll/match_reduce.svg?branch=master)](https://travis-ci.org/bluemarblepayroll/match_reduce) [![Maintainability](https://api.codeclimate.com/v1/badges/a441f1405041cd7a0807/maintainability)](https://codeclimate.com/github/bluemarblepayroll/match_reduce/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/a441f1405041cd7a0807/test_coverage)](https://codeclimate.com/github/bluemarblepayroll/match_reduce/test_coverage) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
4
|
+
|
5
|
+
MatchReduce is a high-speed, in-memory data aggregation and reduction algorithm. The lifecycle is:
|
6
|
+
|
7
|
+
1. define aggregates
|
8
|
+
2. pump records into algorithm
|
9
|
+
3. grab results
|
10
|
+
|
11
|
+
The dataset will only be processed once no matter how many aggregates you define. An aggregate is expressed as:
|
12
|
+
|
13
|
+
* patterns: an array of hashes, which are used to pattern match records
|
14
|
+
* reducer: a function, which is used when a record matches
|
15
|
+
* group_keys: key or keys to uniquely semantically identify records, so records do not get added multiple times. This is extremely beneficial for denormalized datasets where data may exist at row and/or column-level.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
To install through Rubygems:
|
20
|
+
|
21
|
+
````bash
|
22
|
+
gem install install match_reduce
|
23
|
+
````
|
24
|
+
|
25
|
+
You can also add this to your Gemfile:
|
26
|
+
|
27
|
+
````bash
|
28
|
+
bundle add match_reduce
|
29
|
+
````
|
30
|
+
|
31
|
+
## Examples
|
32
|
+
|
33
|
+
### Getting Started
|
34
|
+
|
35
|
+
A very basic example of calling this library would be:
|
36
|
+
|
37
|
+
````ruby
|
38
|
+
require 'match_reduce'
|
39
|
+
|
40
|
+
aggregates = [
|
41
|
+
{
|
42
|
+
name: :total_game_points
|
43
|
+
reducer: ->(memo, record, resolver) { memo.to_i + resolver.get(record, :game_points).to_i },
|
44
|
+
group_keys: :game
|
45
|
+
}
|
46
|
+
]
|
47
|
+
|
48
|
+
records = [
|
49
|
+
{ game: 1, game_points: 199, team: 'Bulls', team_points: 100 },
|
50
|
+
{ game: 1, game_points: 199, team: 'Celtics', team_points: 99 },
|
51
|
+
{ game: 2, game_points: 240, team: 'Rockets', team_points: 130 },
|
52
|
+
{ game: 2, game_points: 240, team: 'Bulls', team_points: 110 }
|
53
|
+
]
|
54
|
+
|
55
|
+
results = MatchReduce.process(aggregates, records)
|
56
|
+
````
|
57
|
+
|
58
|
+
`results` would be equal to:
|
59
|
+
|
60
|
+
````ruby
|
61
|
+
[
|
62
|
+
MatchReduce::Processor::Result.new(
|
63
|
+
name: :total_game_points,
|
64
|
+
records: [
|
65
|
+
{ game: 1, game_points: 199, team: 'Bulls', team_points: 100 },
|
66
|
+
{ game: 2, game_points: 240, team: 'Rockets', team_points: 130 }
|
67
|
+
],
|
68
|
+
value: 439
|
69
|
+
)
|
70
|
+
]
|
71
|
+
````
|
72
|
+
|
73
|
+
Notes:
|
74
|
+
|
75
|
+
* Not specifying patterns means: "match on everything"
|
76
|
+
* group_keys will limit the records matched on, per aggregator, to the first record only. This is why only the first and third records matched.
|
77
|
+
* keys are type-indifferent and will extracted using (the Objectable library)[https://github.com/bluemarblepayroll/objectable]. This means you can leverage dot-notation, non-hash record types, and indifference. You can also customize the resolver used and pass it as a third argument to MatchReduce#process(aggregates, records, resolver).
|
78
|
+
* Names of aggregates are type-sensitive, so: `:total_game_points` and `'total_game_points'` are two different aggregates and will produce two different results.
|
79
|
+
|
80
|
+
### Adding Patterns
|
81
|
+
|
82
|
+
Let's say we want to discretely know how many points the Bulls, Celtics, and Rockets have scored, we could do this:
|
83
|
+
|
84
|
+
````ruby
|
85
|
+
require 'match_reduce'
|
86
|
+
|
87
|
+
aggregates = [
|
88
|
+
{
|
89
|
+
name: :bulls_points
|
90
|
+
patterns: { team: 'Bulls' },
|
91
|
+
reducer: ->(memo, record, resolver) { memo.to_i + resolver.get(record, :team_points).to_i },
|
92
|
+
group_keys: :game
|
93
|
+
},
|
94
|
+
{
|
95
|
+
name: :celtics_points
|
96
|
+
patterns: { team: 'Celtics' },
|
97
|
+
reducer: ->(memo, record, resolver) { memo.to_i + resolver.get(record, :team_points).to_i },
|
98
|
+
group_keys: :game
|
99
|
+
},
|
100
|
+
{
|
101
|
+
name: :rockets_points
|
102
|
+
patterns: { team: 'Rockets' },
|
103
|
+
reducer: ->(memo, record, resolver) { memo.to_i + resolver.get(record, :team_points).to_i },
|
104
|
+
group_keys: :game
|
105
|
+
}
|
106
|
+
]
|
107
|
+
|
108
|
+
records = [
|
109
|
+
{ game: 1, game_points: 199, team: 'Bulls', team_points: 100 },
|
110
|
+
{ game: 1, game_points: 199, team: 'Celtics', team_points: 99 },
|
111
|
+
{ game: 2, game_points: 240, team: 'Rockets', team_points: 130 },
|
112
|
+
{ game: 2, game_points: 240, team: 'Bulls', team_points: 110 }
|
113
|
+
]
|
114
|
+
|
115
|
+
results = MatchReduce.process(aggregates, records)
|
116
|
+
````
|
117
|
+
|
118
|
+
`results` would now be equal to:
|
119
|
+
|
120
|
+
````ruby
|
121
|
+
[
|
122
|
+
MatchReduce::Processor::Result.new(
|
123
|
+
name: :bulls_points,
|
124
|
+
records: [
|
125
|
+
{ game: 1, game_points: 199, team: 'Bulls', team_points: 100 },
|
126
|
+
{ game: 2, game_points: 240, team: 'Bulls', team_points: 110 }
|
127
|
+
],
|
128
|
+
value: 210
|
129
|
+
),
|
130
|
+
MatchReduce::Processor::Result.new(
|
131
|
+
name: :celtics_points,
|
132
|
+
records: [
|
133
|
+
{ game: 1, game_points: 199, team: 'Celtics', team_points: 99 },
|
134
|
+
],
|
135
|
+
value: 99
|
136
|
+
),
|
137
|
+
MatchReduce::Processor::Result.new(
|
138
|
+
name: :rockets_points,
|
139
|
+
records: [
|
140
|
+
{ game: 2, game_points: 240, team: 'Rockets', team_points: 130 },
|
141
|
+
],
|
142
|
+
value: 130
|
143
|
+
),
|
144
|
+
]
|
145
|
+
````
|
146
|
+
|
147
|
+
We could also choose to aggregate multiple teams together by providing multiple patterns:
|
148
|
+
|
149
|
+
````ruby
|
150
|
+
require 'match_reduce'
|
151
|
+
|
152
|
+
aggregates = [
|
153
|
+
{
|
154
|
+
name: :bulls_and_celtics_points
|
155
|
+
patterns: [
|
156
|
+
{ team: 'Bulls' },
|
157
|
+
{ team: 'Celtics' }
|
158
|
+
],
|
159
|
+
reducer: ->(memo, record, resolver) { memo.to_i + resolver.get(record, :team_points).to_i },
|
160
|
+
group_keys: :game
|
161
|
+
},
|
162
|
+
]
|
163
|
+
|
164
|
+
records = [
|
165
|
+
{ game: 1, game_points: 199, team: 'Bulls', team_points: 100 },
|
166
|
+
{ game: 1, game_points: 199, team: 'Celtics', team_points: 99 },
|
167
|
+
{ game: 2, game_points: 240, team: 'Rockets', team_points: 130 },
|
168
|
+
{ game: 2, game_points: 240, team: 'Bulls', team_points: 110 }
|
169
|
+
]
|
170
|
+
|
171
|
+
results = MatchReduce.process(aggregates, records)
|
172
|
+
````
|
173
|
+
|
174
|
+
`results` would now be equal to:
|
175
|
+
|
176
|
+
````ruby
|
177
|
+
[
|
178
|
+
MatchReduce::Processor::Result.new(
|
179
|
+
name: :bulls_points,
|
180
|
+
records: [
|
181
|
+
{ game: 1, game_points: 199, team: 'Bulls', team_points: 100 },
|
182
|
+
{ game: 1, game_points: 199, team: 'Celtics', team_points: 99 },
|
183
|
+
{ game: 2, game_points: 240, team: 'Bulls', team_points: 110 }
|
184
|
+
],
|
185
|
+
value: 309
|
186
|
+
)
|
187
|
+
]
|
188
|
+
````
|
189
|
+
|
190
|
+
## Contributing
|
191
|
+
|
192
|
+
### Development Environment Configuration
|
193
|
+
|
194
|
+
Basic steps to take to get this repository compiling:
|
195
|
+
|
196
|
+
1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check match_reduce.gemspec for versions supported)
|
197
|
+
2. Install bundler (gem install bundler)
|
198
|
+
3. Clone the repository (git clone git@github.com:bluemarblepayroll/match_reduce.git)
|
199
|
+
4. Navigate to the root folder (cd match_reduce)
|
200
|
+
5. Install dependencies (bundle)
|
201
|
+
|
202
|
+
### Running Tests
|
203
|
+
|
204
|
+
To execute the test suite and code-coverage tool, run:
|
205
|
+
|
206
|
+
````bash
|
207
|
+
bundle exec rspec spec --format documentation
|
208
|
+
````
|
209
|
+
|
210
|
+
Alternatively, you can have Guard watch for changes:
|
211
|
+
|
212
|
+
````bash
|
213
|
+
bundle exec guard
|
214
|
+
````
|
215
|
+
|
216
|
+
Also, do not forget to run Rubocop:
|
217
|
+
|
218
|
+
````bash
|
219
|
+
bundle exec rubocop
|
220
|
+
````
|
221
|
+
|
222
|
+
or run all three in one command:
|
223
|
+
|
224
|
+
````bash
|
225
|
+
bundle exec rake
|
226
|
+
````
|
227
|
+
|
228
|
+
### Publishing
|
229
|
+
|
230
|
+
Note: ensure you have proper authorization before trying to publish new versions.
|
231
|
+
|
232
|
+
After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
|
233
|
+
|
234
|
+
1. Merge Pull Request into master
|
235
|
+
2. Update `lib/match_reduce/version.rb` using [semantic versioning](https://semver.org/)
|
236
|
+
3. Install dependencies: `bundle`
|
237
|
+
4. Update `CHANGELOG.md` with release notes
|
238
|
+
5. Commit & push master to remote and ensure CI builds master successfully
|
239
|
+
6. Run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
240
|
+
|
241
|
+
## Code of Conduct
|
242
|
+
|
243
|
+
Everyone interacting in this codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bluemarblepayroll/match_reduce/blob/master/CODE_OF_CONDUCT.md).
|
244
|
+
|
245
|
+
## License
|
246
|
+
|
247
|
+
This project is MIT Licensed.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'bundler/gem_tasks'
|
11
|
+
require 'rspec/core/rake_task'
|
12
|
+
require 'rubocop/rake_task'
|
13
|
+
|
14
|
+
RSpec::Core::RakeTask.new(:spec)
|
15
|
+
RuboCop::RakeTask.new
|
16
|
+
|
17
|
+
task default: %i[rubocop spec]
|
data/bin/console
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# Copyright (c) 2018-present, Blue Marble Payroll, LLC
|
6
|
+
#
|
7
|
+
# This source code is licensed under the MIT license found in the
|
8
|
+
# LICENSE file in the root directory of this source tree.
|
9
|
+
#
|
10
|
+
|
11
|
+
require 'bundler/setup'
|
12
|
+
require 'match_reduce'
|
13
|
+
|
14
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
15
|
+
# with your gem easier. You can also use a different console, if you like.
|
16
|
+
|
17
|
+
require 'pry'
|
18
|
+
Pry.start
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
module MatchReduce
|
11
|
+
# An aggregate is a group of patterns with a reducer that you wish to report on.
|
12
|
+
class Aggregate
|
13
|
+
acts_as_hashable
|
14
|
+
|
15
|
+
attr_reader :group_keys,
|
16
|
+
:name,
|
17
|
+
:patterns,
|
18
|
+
:reducer
|
19
|
+
|
20
|
+
def initialize(name:, group_keys: [], patterns: [], reducer: nil)
|
21
|
+
raise ArgumentError, 'name is required' if name.to_s.empty?
|
22
|
+
|
23
|
+
@name = name
|
24
|
+
@group_keys = Array(group_keys)
|
25
|
+
@patterns = stringed_keys(not_empty(array(patterns)))
|
26
|
+
@reducer = reducer
|
27
|
+
|
28
|
+
freeze
|
29
|
+
end
|
30
|
+
|
31
|
+
def keys
|
32
|
+
patterns.flat_map(&:keys)
|
33
|
+
end
|
34
|
+
|
35
|
+
def reduce(memo, record, resolver)
|
36
|
+
reducer ? reducer.call(memo, record, resolver) : memo
|
37
|
+
end
|
38
|
+
|
39
|
+
def grouped?
|
40
|
+
!group_keys.empty?
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
attr_reader :resolver
|
46
|
+
|
47
|
+
def stringed_keys(hashes)
|
48
|
+
hashes.map do |hash|
|
49
|
+
hash.map { |k, v| [k.to_s, v] }.to_h
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def array(val)
|
54
|
+
val.is_a?(Hash) ? [val] : Array(val)
|
55
|
+
end
|
56
|
+
|
57
|
+
def not_empty(val)
|
58
|
+
val.empty? ? [{}] : val
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
module MatchReduce
|
11
|
+
# This class represents a wildcard. Direct use of this class should be avoided, instead,
|
12
|
+
# use the MatchReduce top-level-declared helper ANY constant.
|
13
|
+
class Any
|
14
|
+
SPECIAL_VALUE = [name, :any].freeze
|
15
|
+
|
16
|
+
private_constant :SPECIAL_VALUE
|
17
|
+
|
18
|
+
# Just be something totally unique. Matching values cannot actually be an array, therefore
|
19
|
+
# there should never be a chance of a collision if the hash of this object is based on
|
20
|
+
# an array structure.
|
21
|
+
def hash
|
22
|
+
SPECIAL_VALUE.hash
|
23
|
+
end
|
24
|
+
|
25
|
+
def ==(other)
|
26
|
+
other.instance_of?(self.class) && hash == other.hash
|
27
|
+
end
|
28
|
+
alias eql? ==
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
module MatchReduce
|
11
|
+
# The Index holds all the aggregates, the reverse lookup data structure, and the ability
|
12
|
+
# to retrieve aggregates based on a pattern
|
13
|
+
class Index
|
14
|
+
extend Forwardable
|
15
|
+
|
16
|
+
attr_reader :aggregates,
|
17
|
+
:any_value,
|
18
|
+
:lookup
|
19
|
+
|
20
|
+
def_delegators :record, :keys
|
21
|
+
|
22
|
+
def initialize(aggregates = [], any_value = ANY)
|
23
|
+
@any_value = any_value
|
24
|
+
@aggregates = Aggregate.array(aggregates).uniq(&:name)
|
25
|
+
@lookup = {}
|
26
|
+
|
27
|
+
all_keys = @aggregates.flat_map(&:keys)
|
28
|
+
@record = HashMath::Record.new(all_keys, any_value)
|
29
|
+
|
30
|
+
@aggregates.map do |aggregate|
|
31
|
+
aggregate.patterns.each do |pattern|
|
32
|
+
normalized_pattern = record.make!(pattern)
|
33
|
+
|
34
|
+
get(normalized_pattern) << aggregate
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
freeze
|
39
|
+
end
|
40
|
+
|
41
|
+
def find(pattern)
|
42
|
+
lookup.fetch(pattern, [])
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
attr_reader :record
|
48
|
+
|
49
|
+
def get(normalized_pattern)
|
50
|
+
lookup[normalized_pattern] ||= Set.new
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
|
5
|
+
#
|
6
|
+
# This source code is licensed under the MIT license found in the
|
7
|
+
# LICENSE file in the root directory of this source tree.
|
8
|
+
#
|
9
|
+
|
10
|
+
module MatchReduce
|
11
|
+
class Processor
|
12
|
+
# This is the main resulting value object returned, one per aggregate.
|
13
|
+
class Result
|
14
|
+
attr_reader :name, :records, :value
|
15
|
+
|
16
|
+
def initialize(name, records, value)
|
17
|
+
@name = name
|
18
|
+
@records = records
|
19
|
+
@value = value
|
20
|
+
|
21
|
+
freeze
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash
|
25
|
+
[name, records, value].hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def ==(other)
|
29
|
+
other.instance_of?(self.class) &&
|
30
|
+
name == other.name &&
|
31
|
+
records == other.records &&
|
32
|
+
value == other.value
|
33
|
+
end
|
34
|
+
alias eql? ==
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|