fresh_objects 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 +25 -0
- data/.ruby-version +1 -0
- data/.travis.yml +25 -0
- data/CHANGELOG.md +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -0
- data/Guardfile +16 -0
- data/LICENSE +7 -0
- data/README.md +149 -0
- data/Rakefile +17 -0
- data/bin/console +18 -0
- data/fresh_objects.gemspec +35 -0
- data/lib/fresh_objects.rb +23 -0
- data/lib/fresh_objects/filter.rb +70 -0
- data/lib/fresh_objects/lookup.rb +73 -0
- data/lib/fresh_objects/version.rb +12 -0
- data/spec/fresh_objects/filter_spec.rb +123 -0
- data/spec/fresh_objects/lookup_spec.rb +73 -0
- data/spec/fresh_objects_spec.rb +29 -0
- data/spec/spec_helper.rb +22 -0
- metadata +186 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ebc619340a5b428eecb98a339179d2e34fded10b7f4aa504e4b18e657e329d6d
|
4
|
+
data.tar.gz: 2f9cccab381e4c60efe6131a7e3830f54658e547e4e004bea68c1d735c53c155
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 066aec06f31fd8d86bd3721c286c31b42a6c320066d2009a43021f96a3bfd6063208ee8539b6cfbca6005e4efe3ccf5d2862b36381bbb6ed1139c0189b6ec3dc
|
7
|
+
data.tar.gz: 3a72e3f846a61683a8d5a946ba7d1cb7349237985a2fe24634d289e650427e404adfd0b3ae83d1cdc46e95b28d4c7162a59e8b5fcaf81becc2e37382db9a702e
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Metrics/LineLength:
|
2
|
+
Max: 100
|
3
|
+
Exclude:
|
4
|
+
- fresh_objects.gemspec
|
5
|
+
|
6
|
+
Metrics/BlockLength:
|
7
|
+
ExcludedMethods:
|
8
|
+
- let
|
9
|
+
- it
|
10
|
+
- describe
|
11
|
+
- context
|
12
|
+
- specify
|
13
|
+
- define
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Max: 25
|
17
|
+
|
18
|
+
AllCops:
|
19
|
+
TargetRubyVersion: 2.3
|
20
|
+
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Max: 16
|
23
|
+
|
24
|
+
Metrics/ClassLength:
|
25
|
+
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=b569b140239d7341120e5d507f418e417bc9f4e78ba008f46eb8ee3015ab45ec
|
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,16 @@
|
|
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
|
+
# Ruby files
|
14
|
+
ruby = dsl.ruby
|
15
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
16
|
+
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,149 @@
|
|
1
|
+
# Fresh Objects
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/fresh_objects.svg)](https://badge.fury.io/rb/fresh_objects) [![Build Status](https://travis-ci.org/bluemarblepayroll/fresh_objects.svg?branch=master)](https://travis-ci.org/bluemarblepayroll/fresh_objects) [![Maintainability](https://api.codeclimate.com/v1/badges/157958522c93760a396c/maintainability)](https://codeclimate.com/github/bluemarblepayroll/fresh_objects/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/157958522c93760a396c/test_coverage)](https://codeclimate.com/github/bluemarblepayroll/fresh_objects/test_coverage) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
4
|
+
|
5
|
+
This library was built to support incoming data streams where you may or may not get the data in order. Since the data coming in is guaranteed to be complete "per object", you only need to keep the latest copy of it and discard stale versions. You can use this library to pass in all this data (in or out of order) and it will ensure only the freshest data is kept (stale copies are discarded.)
|
6
|
+
|
7
|
+
Note: persistence is outside of the scope of this library. You can, however, use this library to access the "object id => timestamp" hash structure that is used for filtering and persist it. Then, you can use it to re-hydrate a filter and pick up where you left off.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
To install through Rubygems:
|
12
|
+
|
13
|
+
````bash
|
14
|
+
gem install install fresh_objects
|
15
|
+
````
|
16
|
+
|
17
|
+
You can also add this to your Gemfile:
|
18
|
+
|
19
|
+
````bash
|
20
|
+
bundle add fresh_objects
|
21
|
+
````
|
22
|
+
|
23
|
+
## Examples
|
24
|
+
|
25
|
+
Say we have the following dataset:
|
26
|
+
|
27
|
+
````ruby
|
28
|
+
objects = [
|
29
|
+
{
|
30
|
+
id: 1,
|
31
|
+
name: 'Batman'
|
32
|
+
timestamp: '2019-01-02 12:00:00 UTC'
|
33
|
+
},
|
34
|
+
{
|
35
|
+
id: 1,
|
36
|
+
name: 'Superman'
|
37
|
+
timestamp: '2019-01-02 12:00:01 UTC'
|
38
|
+
},
|
39
|
+
{
|
40
|
+
id: 1,
|
41
|
+
name: 'Ironman'
|
42
|
+
timestamp: '2019-01-02 11:59:59 UTC'
|
43
|
+
}
|
44
|
+
]
|
45
|
+
````
|
46
|
+
|
47
|
+
You could pass these objects into this library in order to resolve that the object you really want is the "Superman" object:
|
48
|
+
|
49
|
+
````ruby
|
50
|
+
filter = FreshObjects.filter(timestamp_key: :timestamp).add_each(objects)
|
51
|
+
|
52
|
+
filtered_objects = filter.objects
|
53
|
+
````
|
54
|
+
|
55
|
+
Now, filtered_objects should equal:
|
56
|
+
|
57
|
+
````ruby
|
58
|
+
[
|
59
|
+
{
|
60
|
+
id: 1,
|
61
|
+
name: 'Superman'
|
62
|
+
timestamp: '2019-01-02 12:00:01 UTC'
|
63
|
+
}
|
64
|
+
]
|
65
|
+
````
|
66
|
+
|
67
|
+
Notes:
|
68
|
+
|
69
|
+
* You can change the object ID key by passing in the `id_key` into `FreshObjects#filter`.
|
70
|
+
* You can change the timestamp key by passing in the `timestamp_key` into `FreshObjects#filter`.
|
71
|
+
* You can change the resolver by passing in a custom `resolver` into `FreshObjects#filter`. Technically, a resolver can be anything with a #get ethod based on the [Objectable resolver#get method](https://github.com/bluemarblepayroll/objectable/blob/master/lib/objectable/resolver.rb).
|
72
|
+
|
73
|
+
### Saving a Filter
|
74
|
+
|
75
|
+
Ultimately what backs a filter is a hash with strings as keys and Time objects as values. This can be accessed for persistence:
|
76
|
+
|
77
|
+
````ruby
|
78
|
+
lookup = filter.timestamps_by_id
|
79
|
+
````
|
80
|
+
|
81
|
+
Then, you can re-constitute a filter from this:
|
82
|
+
|
83
|
+
````ruby
|
84
|
+
filter = FreshObjects.filter(lookup: timestamps_by_id)
|
85
|
+
````
|
86
|
+
|
87
|
+
Notes:
|
88
|
+
|
89
|
+
* The lookup is just the timestamp cache, it does not contain the other options such as other Filter#new options.
|
90
|
+
* The next time you use a re-constituted filter, objects will be empty. That means the `#objects` method is not cumulative across uses (per object instance.) Remember, the goal of this library is the core filtering algorithm, not persistence.
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
### Development Environment Configuration
|
95
|
+
|
96
|
+
Basic steps to take to get this repository compiling:
|
97
|
+
|
98
|
+
1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check fresh_objects.gemspec for versions supported)
|
99
|
+
2. Install bundler (gem install bundler)
|
100
|
+
3. Clone the repository (git clone git@github.com:bluemarblepayroll/fresh_objects.git)
|
101
|
+
4. Navigate to the root folder (cd fresh_objects)
|
102
|
+
5. Install dependencies (bundle)
|
103
|
+
|
104
|
+
### Running Tests
|
105
|
+
|
106
|
+
To execute the test suite and code-coverage tool, run:
|
107
|
+
|
108
|
+
````bash
|
109
|
+
bundle exec rspec spec --format documentation
|
110
|
+
````
|
111
|
+
|
112
|
+
Alternatively, you can have Guard watch for changes:
|
113
|
+
|
114
|
+
````bash
|
115
|
+
bundle exec guard
|
116
|
+
````
|
117
|
+
|
118
|
+
Also, do not forget to run Rubocop:
|
119
|
+
|
120
|
+
````bash
|
121
|
+
bundle exec rubocop
|
122
|
+
````
|
123
|
+
|
124
|
+
or run all three in one command:
|
125
|
+
|
126
|
+
````bash
|
127
|
+
bundle exec rake
|
128
|
+
````
|
129
|
+
|
130
|
+
### Publishing
|
131
|
+
|
132
|
+
Note: ensure you have proper authorization before trying to publish new versions.
|
133
|
+
|
134
|
+
After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
|
135
|
+
|
136
|
+
1. Merge Pull Request into master
|
137
|
+
2. Update `lib/fresh_objects/version.rb` using [semantic versioning](https://semver.org/)
|
138
|
+
3. Install dependencies: `bundle`
|
139
|
+
4. Update `CHANGELOG.md` with release notes
|
140
|
+
5. Commit & push master to remote and ensure CI builds master successfully
|
141
|
+
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).
|
142
|
+
|
143
|
+
## Code of Conduct
|
144
|
+
|
145
|
+
Everyone interacting in this codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bluemarblepayroll/fresh_objects/blob/master/CODE_OF_CONDUCT.md).
|
146
|
+
|
147
|
+
## License
|
148
|
+
|
149
|
+
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 'fresh_objects'
|
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,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './lib/fresh_objects/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'fresh_objects'
|
7
|
+
s.version = FreshObjects::VERSION
|
8
|
+
s.summary = 'Filtering algorithm that keeps track of object timestamps and only keeps the freshest version of each object.'
|
9
|
+
|
10
|
+
s.description = <<-DESCRIPTION
|
11
|
+
This library provides a simple algorithm for object filtering based on an object's timestamp.
|
12
|
+
A master list of object ID's and timestamps are managed by this library and can be persisted
|
13
|
+
(outside of this library) for picking up where you last left off.
|
14
|
+
DESCRIPTION
|
15
|
+
|
16
|
+
s.authors = ['Matthew Ruggio']
|
17
|
+
s.email = ['mruggio@bluemarblepayroll.com']
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
21
|
+
s.homepage = 'https://github.com/bluemarblepayroll/fresh_objects'
|
22
|
+
s.license = 'MIT'
|
23
|
+
|
24
|
+
s.required_ruby_version = '>= 2.3.8'
|
25
|
+
|
26
|
+
s.add_dependency('objectable', '~>1')
|
27
|
+
|
28
|
+
s.add_development_dependency('guard-rspec', '~>4.7')
|
29
|
+
s.add_development_dependency('pry', '~>0')
|
30
|
+
s.add_development_dependency('rake', '~> 12')
|
31
|
+
s.add_development_dependency('rspec')
|
32
|
+
s.add_development_dependency('rubocop', '~>0.74.0')
|
33
|
+
s.add_development_dependency('simplecov', '~>0.17.0')
|
34
|
+
s.add_development_dependency('simplecov-console', '~>0.5.0')
|
35
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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 'objectable'
|
11
|
+
require 'time'
|
12
|
+
|
13
|
+
require_relative 'fresh_objects/filter'
|
14
|
+
|
15
|
+
# Top-level namespace
|
16
|
+
module FreshObjects
|
17
|
+
class << self
|
18
|
+
# Syntactic sugary proxy for easy building of Filter instances.
|
19
|
+
def filter(*args)
|
20
|
+
Filter.new(*args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,70 @@
|
|
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_relative 'lookup'
|
11
|
+
|
12
|
+
module FreshObjects
|
13
|
+
# This class can do a "row/timestamp-based semantic merge". In other words:
|
14
|
+
# you can use this class to dump any number of arrays of objects into and it will sift
|
15
|
+
# through them and only keep the latest, non-stale copies of the objects.
|
16
|
+
class Filter
|
17
|
+
extend Forwardable
|
18
|
+
include Enumerable
|
19
|
+
|
20
|
+
attr_reader :id_key,
|
21
|
+
:lookup,
|
22
|
+
:resolver,
|
23
|
+
:timestamp_key
|
24
|
+
|
25
|
+
def_delegators :lookup, :timestamps_by_id
|
26
|
+
|
27
|
+
def_delegators :objects, :each
|
28
|
+
|
29
|
+
def initialize(lookup: {}, id_key: :id, timestamp_key: nil, resolver: Objectable.resolver)
|
30
|
+
@lookup = Lookup.make(lookup)
|
31
|
+
@id_key = id_key
|
32
|
+
@timestamp_key = timestamp_key
|
33
|
+
@resolver = resolver
|
34
|
+
@objects_by_id = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
def add_each(objects, default_timestamp: Time.now.utc)
|
38
|
+
tap { objects.each { |o| add(o, default_timestamp: default_timestamp) } }
|
39
|
+
end
|
40
|
+
|
41
|
+
def add(object, default_timestamp: Time.now.utc)
|
42
|
+
id = resolver.get(object, id_key).to_s
|
43
|
+
timestamp = resolve_timestamp(object, default_timestamp)
|
44
|
+
|
45
|
+
objects_by_id[id] = object if lookup.fresh_set?(id, timestamp)
|
46
|
+
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
attr_reader :objects_by_id
|
53
|
+
|
54
|
+
def objects
|
55
|
+
objects_by_id.values
|
56
|
+
end
|
57
|
+
|
58
|
+
def resolve_timestamp(object, default_timestamp)
|
59
|
+
# If we have a timestamp key then lets try and get it from the record.
|
60
|
+
# If we don't then we can defer to the default.
|
61
|
+
retrieved = timestamp_key ? resolver.get(object, timestamp_key) : default_timestamp
|
62
|
+
|
63
|
+
# One last check, just in case the record returned back something "null-like" like a blank
|
64
|
+
# string, lets treat that as nil.
|
65
|
+
return default_timestamp if retrieved.to_s.empty?
|
66
|
+
|
67
|
+
retrieved
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,73 @@
|
|
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 FreshObjects
|
11
|
+
# A data structure which holds a timestamp per ID. It can then be used as a performant lookup
|
12
|
+
# to see if an incoming timestamp is stale or new. It is essentially backed by a hash
|
13
|
+
# where the key is a string and the value is a Time object.
|
14
|
+
# You can also pass in a hash into the constructor for configuration ease.
|
15
|
+
class Lookup
|
16
|
+
class << self
|
17
|
+
def make(timestamps_by_id = {})
|
18
|
+
if timestamps_by_id.is_a?(self)
|
19
|
+
timestamps_by_id
|
20
|
+
else
|
21
|
+
new(timestamps_by_id)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :timestamps_by_id
|
27
|
+
|
28
|
+
def initialize(timestamps_by_id = {})
|
29
|
+
@timestamps_by_id = timestamps_by_id.map do |id, timestamp|
|
30
|
+
[id.to_s, parse_time(timestamp)]
|
31
|
+
end.to_h
|
32
|
+
|
33
|
+
freeze
|
34
|
+
end
|
35
|
+
|
36
|
+
def fresh_set?(id, timestamp)
|
37
|
+
fresh?(id, timestamp).tap do |fresh|
|
38
|
+
set(id, timestamp) if fresh
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def fresh?(id, timestamp)
|
43
|
+
!stale?(id, timestamp)
|
44
|
+
end
|
45
|
+
|
46
|
+
def stale?(id, timestamp)
|
47
|
+
id = id.to_s
|
48
|
+
current = get(id)
|
49
|
+
timestamp = parse_time(timestamp)
|
50
|
+
|
51
|
+
current && timestamp <= current
|
52
|
+
end
|
53
|
+
|
54
|
+
def get(id)
|
55
|
+
timestamps_by_id[id.to_s]
|
56
|
+
end
|
57
|
+
|
58
|
+
def set(id, timestamp)
|
59
|
+
tap { timestamps_by_id[id.to_s] = parse_time(timestamp) }
|
60
|
+
end
|
61
|
+
|
62
|
+
def ==(other)
|
63
|
+
other.is_a?(self.class) && timestamps_by_id == other.timestamps_by_id
|
64
|
+
end
|
65
|
+
alias eql? ==
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def parse_time(time)
|
70
|
+
time.is_a?(Time) ? time : Time.parse(time.to_s).utc
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,12 @@
|
|
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 FreshObjects
|
11
|
+
VERSION = '1.0.0-alpha'
|
12
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2018-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 'spec_helper'
|
11
|
+
|
12
|
+
RSpec.describe FreshObjects::Filter do
|
13
|
+
let(:lookup) do
|
14
|
+
{
|
15
|
+
'rizzo': '1950-01-03 12:00:00 UTC'
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
subject { described_class.new(lookup: lookup, timestamp_key: :timestamp) }
|
20
|
+
|
21
|
+
specify '#timestamps_by_id returns the underlying lookup hash' do
|
22
|
+
expected = {
|
23
|
+
'rizzo' => Time.parse('1950-01-03 12:00:00 UTC').utc
|
24
|
+
}
|
25
|
+
|
26
|
+
expect(subject.timestamps_by_id).to eq(expected)
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'using default_timestamp' do
|
30
|
+
it 'skips existing objects before timestamp' do
|
31
|
+
object = {
|
32
|
+
id: :rizzo,
|
33
|
+
first: :frank
|
34
|
+
}
|
35
|
+
|
36
|
+
subject.add(object, default_timestamp: '1950-01-03 11:59:59 UTC')
|
37
|
+
|
38
|
+
expect(subject.to_a).to eq([])
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'adds existing objects after timestamp' do
|
42
|
+
object = {
|
43
|
+
id: :rizzo,
|
44
|
+
first: :frank
|
45
|
+
}
|
46
|
+
|
47
|
+
subject.add(object, default_timestamp: '1950-01-03 12:00:01 UTC')
|
48
|
+
|
49
|
+
expect(subject.to_a).to eq([object])
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'adds new objects' do
|
53
|
+
object = {
|
54
|
+
id: :bozo,
|
55
|
+
first: 'the clown'
|
56
|
+
}
|
57
|
+
|
58
|
+
subject.add(object, default_timestamp: '1950-01-03 12:00:01 UTC')
|
59
|
+
|
60
|
+
expect(subject.to_a).to eq([object])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'using object timestamp' do
|
65
|
+
it 'skips existing objects before timestamp' do
|
66
|
+
object = {
|
67
|
+
id: :rizzo,
|
68
|
+
first: :frank,
|
69
|
+
timestamp: '1950-01-03 11:59:59 UTC'
|
70
|
+
}
|
71
|
+
|
72
|
+
subject.add(object)
|
73
|
+
|
74
|
+
expect(subject.to_a).to eq([])
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'adds existing objects after timestamp' do
|
78
|
+
object = {
|
79
|
+
id: :rizzo,
|
80
|
+
first: :frank,
|
81
|
+
timestamp: '1950-01-03 12:00:01 UTC'
|
82
|
+
}
|
83
|
+
|
84
|
+
subject.add(object)
|
85
|
+
|
86
|
+
expect(subject.to_a).to eq([object])
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'overriding objects' do
|
91
|
+
let(:object1) do
|
92
|
+
{
|
93
|
+
id: :rizzo,
|
94
|
+
first: :frank1,
|
95
|
+
timestamp: '1950-01-03 12:00:01 UTC'
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
let(:object2) do
|
100
|
+
{
|
101
|
+
id: :rizzo,
|
102
|
+
first: :frank2,
|
103
|
+
timestamp: '1950-01-03 12:00:02 UTC'
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'in chonological add order' do
|
108
|
+
before(:each) { subject.add_each([object1, object2]) }
|
109
|
+
|
110
|
+
it 'keeps latest object' do
|
111
|
+
expect(subject.to_a).to eq([object2])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'in chonological add order' do
|
116
|
+
before(:each) { subject.add_each([object2, object1]) }
|
117
|
+
|
118
|
+
it 'keeps latest object' do
|
119
|
+
expect(subject.to_a).to eq([object2])
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2018-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 'spec_helper'
|
11
|
+
|
12
|
+
RSpec.describe FreshObjects::Lookup do
|
13
|
+
let(:rizzo_stamp) { '1950-01-03 12:00:00 UTC' }
|
14
|
+
|
15
|
+
let(:later_stamp) { '1950-01-03 12:00:01 UTC' }
|
16
|
+
|
17
|
+
let(:timestamps_by_id) do
|
18
|
+
{
|
19
|
+
rizzo: rizzo_stamp
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
subject { described_class.new(timestamps_by_id) }
|
24
|
+
|
25
|
+
describe 'class methods' do
|
26
|
+
describe '#make' do
|
27
|
+
it 'returns argument if argument is a Lookup instance' do
|
28
|
+
subject2 = described_class.make(subject)
|
29
|
+
|
30
|
+
# #be for object equality
|
31
|
+
expect(subject2).to be(subject)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'delegates to initializer if argument is not a Lookup instance' do
|
35
|
+
expect(described_class).to receive(:new).with(timestamps_by_id)
|
36
|
+
|
37
|
+
described_class.make(timestamps_by_id)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# This implicitly will test #fresh?, #stale?, #get, and #set.
|
43
|
+
describe '#fresh_set?' do
|
44
|
+
it 'is true for ids not present' do
|
45
|
+
expect(subject.fresh_set?(:marsha, '2000-04-05 02:03:04 UTC')).to be true
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'returns is false for ids already present that have equal timestamps' do
|
49
|
+
expect(subject.fresh_set?(:rizzo, rizzo_stamp)).to be false
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns false for ids already present that have earlier timestamps' do
|
53
|
+
expect(subject.fresh_set?(:rizzo, '1950-01-03 11:59:59 UTC')).to be false
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns true and updates timestamp for ids already present that have later timestamps' do
|
57
|
+
expect(subject.fresh_set?(:rizzo, later_stamp)).to be true
|
58
|
+
expect(subject.timestamps_by_id['rizzo']).to eq(Time.parse(later_stamp).utc)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'equality' do
|
63
|
+
let(:subject2) { described_class.new(timestamps_by_id) }
|
64
|
+
|
65
|
+
specify '#eql? compares class type and underlying hash' do
|
66
|
+
expect(subject).to eql(subject2)
|
67
|
+
end
|
68
|
+
|
69
|
+
specify '#== compares class type and underlying hash' do
|
70
|
+
expect(subject).to eq(subject2)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2018-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 'spec_helper'
|
11
|
+
|
12
|
+
RSpec.describe FreshObjects do
|
13
|
+
describe 'class methods' do
|
14
|
+
let(:arguments) do
|
15
|
+
{
|
16
|
+
lookup: { a: :b },
|
17
|
+
id_key: :id_key,
|
18
|
+
timestamp_key: :timestamp_key,
|
19
|
+
resolver: Objectable.resolver
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
specify '#filter delegates to Filter#new' do
|
24
|
+
expect(described_class::Filter).to receive(:new).with(arguments)
|
25
|
+
|
26
|
+
described_class.filter(arguments)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Copyright (c) 2018-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 'pry'
|
11
|
+
|
12
|
+
unless ENV['DISABLE_SIMPLECOV'] == 'true'
|
13
|
+
require 'simplecov'
|
14
|
+
require 'simplecov-console'
|
15
|
+
|
16
|
+
SimpleCov.formatter = SimpleCov::Formatter::Console
|
17
|
+
SimpleCov.start do
|
18
|
+
add_filter %r{\A/spec/}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
require './lib/fresh_objects'
|
metadata
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fresh_objects
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.pre.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Ruggio
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: objectable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
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: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.74.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.74.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.17.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.17.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov-console
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.5.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.5.0
|
125
|
+
description: |2
|
126
|
+
This library provides a simple algorithm for object filtering based on an object's timestamp.
|
127
|
+
A master list of object ID's and timestamps are managed by this library and can be persisted
|
128
|
+
(outside of this library) for picking up where you last left off.
|
129
|
+
email:
|
130
|
+
- mruggio@bluemarblepayroll.com
|
131
|
+
executables:
|
132
|
+
- console
|
133
|
+
extensions: []
|
134
|
+
extra_rdoc_files: []
|
135
|
+
files:
|
136
|
+
- ".editorconfig"
|
137
|
+
- ".gitignore"
|
138
|
+
- ".rubocop.yml"
|
139
|
+
- ".ruby-version"
|
140
|
+
- ".travis.yml"
|
141
|
+
- CHANGELOG.md
|
142
|
+
- CODE_OF_CONDUCT.md
|
143
|
+
- Gemfile
|
144
|
+
- Guardfile
|
145
|
+
- LICENSE
|
146
|
+
- README.md
|
147
|
+
- Rakefile
|
148
|
+
- bin/console
|
149
|
+
- fresh_objects.gemspec
|
150
|
+
- lib/fresh_objects.rb
|
151
|
+
- lib/fresh_objects/filter.rb
|
152
|
+
- lib/fresh_objects/lookup.rb
|
153
|
+
- lib/fresh_objects/version.rb
|
154
|
+
- spec/fresh_objects/filter_spec.rb
|
155
|
+
- spec/fresh_objects/lookup_spec.rb
|
156
|
+
- spec/fresh_objects_spec.rb
|
157
|
+
- spec/spec_helper.rb
|
158
|
+
homepage: https://github.com/bluemarblepayroll/fresh_objects
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata: {}
|
162
|
+
post_install_message:
|
163
|
+
rdoc_options: []
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: 2.3.8
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">"
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: 1.3.1
|
176
|
+
requirements: []
|
177
|
+
rubygems_version: 3.0.3
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: Filtering algorithm that keeps track of object timestamps and only keeps
|
181
|
+
the freshest version of each object.
|
182
|
+
test_files:
|
183
|
+
- spec/fresh_objects/filter_spec.rb
|
184
|
+
- spec/fresh_objects/lookup_spec.rb
|
185
|
+
- spec/fresh_objects_spec.rb
|
186
|
+
- spec/spec_helper.rb
|