rails_matching 0.1.0
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 +42 -0
- data/.rspec_status +13 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +110 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rails_matching.rb +146 -0
- data/lib/rails_matching/version.rb +3 -0
- data/rails_matching.gemspec +42 -0
- metadata +173 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 87d34a29fd588bbda82df552ef180b195be4b496
|
4
|
+
data.tar.gz: 7f8cf4f20ca66436959fb8607c922b33d31f4c72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf721089b75d4f953971458a09afc7f48e1857fb90281529f46712276459e622147b0b7f7fd139bd495541a259050e84922705d97b8b65b4fa75486b2063aee5
|
7
|
+
data.tar.gz: 8b0400376d108e1e77b61dd7667c96d9dbff8fdd8d79e609b35f53d752b437e679fa2e75b54695fd44b06ca5bf32c5cf128c7b086841df60a2cd218e977bc408
|
data/.gitignore
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
*.rbc
|
2
|
+
capybara-*.html
|
3
|
+
.rspec
|
4
|
+
/log
|
5
|
+
/tmp
|
6
|
+
/db/*.sqlite3
|
7
|
+
/db/*.sqlite3-journal
|
8
|
+
/public/system
|
9
|
+
/coverage/
|
10
|
+
/spec/tmp
|
11
|
+
**.orig
|
12
|
+
rerun.txt
|
13
|
+
pickle-email-*.html
|
14
|
+
|
15
|
+
# TODO Comment out these rules if you are OK with secrets being uploaded to the repo
|
16
|
+
config/initializers/secret_token.rb
|
17
|
+
config/secrets.yml
|
18
|
+
|
19
|
+
# dotenv
|
20
|
+
# TODO Comment out this rule if environment variables can be committed
|
21
|
+
.env
|
22
|
+
|
23
|
+
## Environment normalization:
|
24
|
+
/.bundle
|
25
|
+
/vendor/bundle
|
26
|
+
|
27
|
+
# these should all be checked in to normalize the environment:
|
28
|
+
# Gemfile.lock, .ruby-version, .ruby-gemset
|
29
|
+
|
30
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
31
|
+
.rvmrc
|
32
|
+
|
33
|
+
# if using bower-rails ignore default bower_components path bower.json files
|
34
|
+
/vendor/assets/bower_components
|
35
|
+
*.bowerrc
|
36
|
+
bower.json
|
37
|
+
|
38
|
+
# Ignore pow environment settings
|
39
|
+
.powenv
|
40
|
+
|
41
|
+
# Ignore Byebug command history file.
|
42
|
+
.byebug_history
|
data/.rspec_status
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
example_id | status | run_time |
|
2
|
+
----------------------------------- | ------ | --------------- |
|
3
|
+
./spec/rails_matching_spec.rb[1:2] | passed | 0.00064 seconds |
|
4
|
+
./spec/rails_matching_spec.rb[1:3] | passed | 0.55094 seconds |
|
5
|
+
./spec/rails_matching_spec.rb[1:4] | passed | 0.00997 seconds |
|
6
|
+
./spec/rails_matching_spec.rb[1:5] | passed | 0.00816 seconds |
|
7
|
+
./spec/rails_matching_spec.rb[1:7] | passed | 0.00141 seconds |
|
8
|
+
./spec/rails_matching_spec.rb[1:8] | passed | 0.00015 seconds |
|
9
|
+
./spec/rails_matching_spec.rb[1:9] | passed | 0.00009 seconds |
|
10
|
+
./spec/rails_matching_spec.rb[1:10] | passed | 0.03074 seconds |
|
11
|
+
./spec/rails_matching_spec.rb[1:12] | passed | 0.00305 seconds |
|
12
|
+
./spec/rails_matching_spec.rb[1:13] | passed | 0.00266 seconds |
|
13
|
+
./spec/rails_matching_spec.rb[1:14] | passed | 0.0029 seconds |
|
data/.travis.yml
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 jonas@matilhaestud.io. 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/Gemfile.lock
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rails_matching (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionmailer (4.0.13)
|
10
|
+
actionpack (= 4.0.13)
|
11
|
+
mail (~> 2.5, >= 2.5.4)
|
12
|
+
actionpack (4.0.13)
|
13
|
+
activesupport (= 4.0.13)
|
14
|
+
builder (~> 3.1.0)
|
15
|
+
erubis (~> 2.7.0)
|
16
|
+
rack (~> 1.5.2)
|
17
|
+
rack-test (~> 0.6.2)
|
18
|
+
activemodel (4.0.13)
|
19
|
+
activesupport (= 4.0.13)
|
20
|
+
builder (~> 3.1.0)
|
21
|
+
activerecord (4.0.13)
|
22
|
+
activemodel (= 4.0.13)
|
23
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
24
|
+
activesupport (= 4.0.13)
|
25
|
+
arel (~> 4.0.0)
|
26
|
+
activerecord-deprecated_finders (1.0.4)
|
27
|
+
activesupport (4.0.13)
|
28
|
+
i18n (~> 0.6, >= 0.6.9)
|
29
|
+
minitest (~> 4.2)
|
30
|
+
multi_json (~> 1.3)
|
31
|
+
thread_safe (~> 0.1)
|
32
|
+
tzinfo (~> 0.3.37)
|
33
|
+
arel (4.0.2)
|
34
|
+
builder (3.1.4)
|
35
|
+
concurrent-ruby (1.0.5)
|
36
|
+
diff-lcs (1.3)
|
37
|
+
erubis (2.7.0)
|
38
|
+
factory_girl (3.2.0)
|
39
|
+
activesupport (>= 3.0.0)
|
40
|
+
factory_girl_rails (3.2.0)
|
41
|
+
factory_girl (~> 3.2.0)
|
42
|
+
railties (>= 3.0.0)
|
43
|
+
faker (1.7.3)
|
44
|
+
i18n (~> 0.5)
|
45
|
+
i18n (0.8.1)
|
46
|
+
mail (2.6.5)
|
47
|
+
mime-types (>= 1.16, < 4)
|
48
|
+
mime-types (3.1)
|
49
|
+
mime-types-data (~> 3.2015)
|
50
|
+
mime-types-data (3.2016.0521)
|
51
|
+
minitest (4.7.5)
|
52
|
+
multi_json (1.12.1)
|
53
|
+
rack (1.5.5)
|
54
|
+
rack-test (0.6.3)
|
55
|
+
rack (>= 1.0)
|
56
|
+
rails (4.0.13)
|
57
|
+
actionmailer (= 4.0.13)
|
58
|
+
actionpack (= 4.0.13)
|
59
|
+
activerecord (= 4.0.13)
|
60
|
+
activesupport (= 4.0.13)
|
61
|
+
bundler (>= 1.3.0, < 2.0)
|
62
|
+
railties (= 4.0.13)
|
63
|
+
sprockets-rails (~> 2.0)
|
64
|
+
railties (4.0.13)
|
65
|
+
actionpack (= 4.0.13)
|
66
|
+
activesupport (= 4.0.13)
|
67
|
+
rake (>= 0.8.7)
|
68
|
+
thor (>= 0.18.1, < 2.0)
|
69
|
+
rake (10.4.2)
|
70
|
+
rspec (3.5.0)
|
71
|
+
rspec-core (~> 3.5.0)
|
72
|
+
rspec-expectations (~> 3.5.0)
|
73
|
+
rspec-mocks (~> 3.5.0)
|
74
|
+
rspec-core (3.5.4)
|
75
|
+
rspec-support (~> 3.5.0)
|
76
|
+
rspec-expectations (3.5.0)
|
77
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
78
|
+
rspec-support (~> 3.5.0)
|
79
|
+
rspec-mocks (3.5.0)
|
80
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
81
|
+
rspec-support (~> 3.5.0)
|
82
|
+
rspec-support (3.5.0)
|
83
|
+
sprockets (3.7.1)
|
84
|
+
concurrent-ruby (~> 1.0)
|
85
|
+
rack (> 1, < 3)
|
86
|
+
sprockets-rails (2.3.3)
|
87
|
+
actionpack (>= 3.0)
|
88
|
+
activesupport (>= 3.0)
|
89
|
+
sprockets (>= 2.8, < 4.0)
|
90
|
+
sqlite3 (1.3.13)
|
91
|
+
thor (0.19.4)
|
92
|
+
thread_safe (0.3.6)
|
93
|
+
tzinfo (0.3.53)
|
94
|
+
|
95
|
+
PLATFORMS
|
96
|
+
ruby
|
97
|
+
|
98
|
+
DEPENDENCIES
|
99
|
+
activerecord (~> 4.0.0)
|
100
|
+
bundler (~> 1.14)
|
101
|
+
factory_girl_rails
|
102
|
+
faker
|
103
|
+
rails
|
104
|
+
rails_matching!
|
105
|
+
rake (~> 10.0)
|
106
|
+
rspec (~> 3.0)
|
107
|
+
sqlite3
|
108
|
+
|
109
|
+
BUNDLED WITH
|
110
|
+
1.14.6
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jonas Scherer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jonas Scherer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# RailsMatchingGem
|
2
|
+
|
3
|
+
Use this gem to match attributes between your object instances.
|
4
|
+
It basically returns the percentage of attributes matched between two instances.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rails_matching'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
## Usage Examples:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# Matches all instances from a model with same model
|
23
|
+
# Returns object id, object id that id matched against, and percentage of match
|
24
|
+
RailsMatching.against_itself(User.all)
|
25
|
+
# [ [1, 2, 33.0 ]
|
26
|
+
# [1, 3, 0.0 ]
|
27
|
+
# [1, 4, 88.0 ] ]
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# Matches one instance from a model with all instances from same model
|
32
|
+
# Returns object id, object id that id matched against, and percentage of match
|
33
|
+
User.create(name: "Test", age: 22, job_title: "writer")
|
34
|
+
RailsMatching.instance_against_all(instance, User.all)
|
35
|
+
# You can also exclude attributes from match
|
36
|
+
RailsMatching.instance_against_all(instance, User.all, exclude_attrs: ["name"])
|
37
|
+
```
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
42
|
+
|
43
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then 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).
|
44
|
+
|
45
|
+
gem build rails_matching.gemspec
|
46
|
+
gem install rails_matching-0.1.0.gem
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jonaslsl/rails_matching. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
51
|
+
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
56
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rails_matching"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
require "rails_matching/version"
|
2
|
+
|
3
|
+
module RailsMatching
|
4
|
+
#
|
5
|
+
# Sets common parameters between diferent methods of matchs
|
6
|
+
# By default excludes common table fields as id, created at, updated at
|
7
|
+
# Sets id of an object as default response
|
8
|
+
# You can set map one param to another as:
|
9
|
+
# [["model1_attr1", "model2_attr1"], ["model1_attr2", "model2_attr4"]]
|
10
|
+
# [["band", "favorite_band"], ["age", "years"]]
|
11
|
+
# This is gonna be nice with one model has different attrinbute name from another model
|
12
|
+
#
|
13
|
+
def self.parametrize(key = "id", exclude_attrs = [], map_params = [])
|
14
|
+
exclude_attrs += %w[ id created_at updated_at ] if exclude_attrs.count == 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.validates_model(model)
|
18
|
+
raise "Error, first param must be an object" if !model.first.is_a? Object
|
19
|
+
raise "Error, first params must have at least one attribute" if model.first.attributes.count <= 0
|
20
|
+
raise "Error, first param must have and id" if model.first.id.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Matchs one model instance against all model instances
|
25
|
+
# by default it excludes common table fields
|
26
|
+
# as id, created at, updated at
|
27
|
+
# runs one model against another twice
|
28
|
+
#
|
29
|
+
def self.against_itself(model, key = "id", exclude_attrs = [])
|
30
|
+
|
31
|
+
validates_model(model)
|
32
|
+
|
33
|
+
exclude_attrs += %w[ id created_at updated_at ]
|
34
|
+
model_attrs = model.new.attributes.keys - exclude_attrs
|
35
|
+
|
36
|
+
# For each instance of a model
|
37
|
+
# Gets its atributes
|
38
|
+
# See if it matches with the same attribute of others instances
|
39
|
+
|
40
|
+
# Returns model key and it's percentage of match against another model like
|
41
|
+
# [ "a1", "a2", 99.9 ]
|
42
|
+
# [ a1, a3, 99.9 ]
|
43
|
+
# [ a1, a4, 45.9 ]
|
44
|
+
# [ a3, a1, 99.9 ]
|
45
|
+
|
46
|
+
result = model.all.map{
|
47
|
+
|a|
|
48
|
+
mathed = model.all.map{
|
49
|
+
|b|
|
50
|
+
#do not run againts same instance
|
51
|
+
if a.id != b.id then
|
52
|
+
matched_attrs = model_attrs.map{
|
53
|
+
|k|
|
54
|
+
a[k] == b[k] ? true : false
|
55
|
+
}
|
56
|
+
count = matched_attrs.count{ |e| e == true }
|
57
|
+
percentage = ( count * 100 ) / model_attrs.count
|
58
|
+
[ a[key], b[key], percentage ]
|
59
|
+
else
|
60
|
+
end
|
61
|
+
}
|
62
|
+
# return without nil values
|
63
|
+
mathed.compact
|
64
|
+
}
|
65
|
+
|
66
|
+
result
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Matchs one model against itself
|
71
|
+
# by default it excludes common table fields
|
72
|
+
# as id, created at, updated at
|
73
|
+
# does not run one model against another twice
|
74
|
+
#
|
75
|
+
# def against_itself_compact(model, key = "id", exclude_attrs = [])
|
76
|
+
# end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Matchs one model against another model
|
80
|
+
# by default it excludes common table fields
|
81
|
+
# as id, created at, updated at
|
82
|
+
# does not run one model against another twice
|
83
|
+
# required matched attributes must return true do be in the response
|
84
|
+
# def model_against_model(model1, model2, required_match_fields = [], key = "id", exclude_attrs = [])
|
85
|
+
# end
|
86
|
+
|
87
|
+
#
|
88
|
+
# Matchs one instance against all instances from a model
|
89
|
+
# by default it excludes common table fields
|
90
|
+
# as id, created at, updated at
|
91
|
+
def self.instance_against_all(instance, model, required_match_fields = [], key = "id", exclude_attrs = [])
|
92
|
+
|
93
|
+
validates_model(model)
|
94
|
+
|
95
|
+
exclude_attrs += %w[ id created_at updated_at ]
|
96
|
+
model_attrs = model.new.attributes.keys - exclude_attrs
|
97
|
+
|
98
|
+
# For each instance of a model
|
99
|
+
# Gets its atributes
|
100
|
+
# See if it matches with the same attribute instance
|
101
|
+
|
102
|
+
# Returns instances keys and it's percentage of match
|
103
|
+
# [ a1, a3, 99.9 ]
|
104
|
+
# [ a1, a4, 45.9 ]
|
105
|
+
# [ a3, a1, 99.9 ]
|
106
|
+
|
107
|
+
result = model.all.map{
|
108
|
+
|a|
|
109
|
+
#do not run againts same instance
|
110
|
+
if a.id != instance.id then
|
111
|
+
matched_attrs = model_attrs.map{
|
112
|
+
|attribute|
|
113
|
+
a[attribute] == instance[attribute] ? true : false
|
114
|
+
}
|
115
|
+
count = matched_attrs.count{ |a| a == true }
|
116
|
+
percentage = ( count * 100 ) / model_attrs.count
|
117
|
+
[ instance[key], a[key], percentage ]
|
118
|
+
else
|
119
|
+
end
|
120
|
+
}
|
121
|
+
|
122
|
+
result.compact
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# Matchs one model against another model
|
128
|
+
# by default it excludes common table fields
|
129
|
+
# as id, created at, updated at
|
130
|
+
# does not run one model against another twice
|
131
|
+
# required matched attributes must return true do be in the response
|
132
|
+
#
|
133
|
+
# def instance_against_instance(instance1, instance2, required_match_fields = [], key = "id", exclude_attrs = [])
|
134
|
+
# end
|
135
|
+
|
136
|
+
#
|
137
|
+
# Matchs one model against another model
|
138
|
+
# by default it excludes common table fields
|
139
|
+
# as id, created at, updated at
|
140
|
+
# does not run one model against another twice
|
141
|
+
# required matched attributes must return true do be in the response
|
142
|
+
#
|
143
|
+
# def instance_against_instance_with_relations(instance1, instance2, relations = [], required_match_fields = [], key = "id", exclude_attrs = [])
|
144
|
+
# end
|
145
|
+
|
146
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_matching/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_matching"
|
8
|
+
spec.version = RailsMatching::VERSION
|
9
|
+
spec.authors = ["Jonas Scherer"]
|
10
|
+
spec.email = ["jonas@matilhaestud.io"]
|
11
|
+
|
12
|
+
spec.summary = "Matches atributes from one instance with another one"
|
13
|
+
spec.description = "Use this gem to match attributes between your object instances.
|
14
|
+
It basically returns the percentage of attributes matched between two instances."
|
15
|
+
spec.homepage = "https://github.com/jonaslsl/rails_matching"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org/"
|
22
|
+
else
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
"public gem pushes."
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
spec.add_development_dependency "activerecord", "~> 4.0.0"
|
38
|
+
spec.add_development_dependency "sqlite3"
|
39
|
+
spec.add_development_dependency "factory_girl_rails"
|
40
|
+
spec.add_development_dependency "rails"
|
41
|
+
spec.add_development_dependency "faker"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_matching
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonas Scherer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-22 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
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: factory_girl_rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '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'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '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'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: faker
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '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'
|
125
|
+
description: "Use this gem to match attributes between your object instances. \nIt
|
126
|
+
basically returns the percentage of attributes matched between two instances."
|
127
|
+
email:
|
128
|
+
- jonas@matilhaestud.io
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec_status"
|
135
|
+
- ".travis.yml"
|
136
|
+
- CODE_OF_CONDUCT.md
|
137
|
+
- Gemfile
|
138
|
+
- Gemfile.lock
|
139
|
+
- LICENSE
|
140
|
+
- LICENSE.txt
|
141
|
+
- README.md
|
142
|
+
- Rakefile
|
143
|
+
- bin/console
|
144
|
+
- bin/setup
|
145
|
+
- lib/rails_matching.rb
|
146
|
+
- lib/rails_matching/version.rb
|
147
|
+
- rails_matching.gemspec
|
148
|
+
homepage: https://github.com/jonaslsl/rails_matching
|
149
|
+
licenses:
|
150
|
+
- MIT
|
151
|
+
metadata:
|
152
|
+
allowed_push_host: https://rubygems.org/
|
153
|
+
post_install_message:
|
154
|
+
rdoc_options: []
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
requirements: []
|
168
|
+
rubyforge_project:
|
169
|
+
rubygems_version: 2.5.2
|
170
|
+
signing_key:
|
171
|
+
specification_version: 4
|
172
|
+
summary: Matches atributes from one instance with another one
|
173
|
+
test_files: []
|