active_registry 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +79 -0
- data/.rspec +3 -0
- data/.travis.yml +20 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +18 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/active_registry.gemspec +34 -0
- data/bin/console +12 -0
- data/bin/setup +8 -0
- data/lib/active_registry.rb +141 -0
- data/spec/active_registry/active_registry_spec.rb +180 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/support/custom_matchers.rb +18 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a45fdd8c57209c6e4124d4bf76bdcf77b23078e7ff55cce31ab73de3d25f1764
|
4
|
+
data.tar.gz: cb740a2184216be2aff84af2ba3c9bdbdf6a7f9d7963c33d65b41b2012036c77
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 67731f1dada2e8da7809d8faa285f5591f5a6ee5ca07cf99df2a912e676c2fede230a61bdebc6b02bb80f3fca88dbb3c21a5833f82dea25fabbc2b908f4d7f71
|
7
|
+
data.tar.gz: 4e8f72faeb357e6f0a5bbd9fb44f59be9e0ba637b9b74fe92844f8a666b4d1ab9bce0d46f0771842f2dae0be2ca6645c077f9e4222872377cfa666971cebc67c
|
data/.gitignore
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore_global
|
6
|
+
|
7
|
+
# Database config and secrets
|
8
|
+
/config/database.yml
|
9
|
+
/config/secrets.yml
|
10
|
+
|
11
|
+
# Ignore bundler config
|
12
|
+
/.bundle
|
13
|
+
|
14
|
+
# Ignore client credentials
|
15
|
+
/config/client_api_credentials.yml
|
16
|
+
|
17
|
+
# Ignore the default SQLite database.
|
18
|
+
/db/*.sqlite3
|
19
|
+
|
20
|
+
# Ignore all logfiles and tempfiles.
|
21
|
+
/log/*.log
|
22
|
+
/tmp
|
23
|
+
/db/structure.sql
|
24
|
+
/doc/app/*
|
25
|
+
/vendor/cldr/*
|
26
|
+
/public/uploads/*
|
27
|
+
/public/photo/*
|
28
|
+
/public/test/*
|
29
|
+
/test/assets/*
|
30
|
+
/spec/assets/*
|
31
|
+
/public/assets/**
|
32
|
+
.powenv
|
33
|
+
.rvmrc
|
34
|
+
.env
|
35
|
+
.ruby-version
|
36
|
+
.rspec_status
|
37
|
+
|
38
|
+
# Compiled source #
|
39
|
+
###################
|
40
|
+
/pkg/
|
41
|
+
*.com
|
42
|
+
*.class
|
43
|
+
*.dll
|
44
|
+
*.exe
|
45
|
+
*.o
|
46
|
+
*.so
|
47
|
+
|
48
|
+
# Packages #
|
49
|
+
############
|
50
|
+
# it's better to unpack these files and commit the raw source
|
51
|
+
# git has its own built in compression methods
|
52
|
+
*.7z
|
53
|
+
*.dmg
|
54
|
+
*.gz
|
55
|
+
*.iso
|
56
|
+
*.jar
|
57
|
+
*.rar
|
58
|
+
*.tar
|
59
|
+
*.zip
|
60
|
+
|
61
|
+
# Logs and databases #
|
62
|
+
######################
|
63
|
+
*.log
|
64
|
+
*.sql
|
65
|
+
*.sqlite
|
66
|
+
|
67
|
+
# OS generated files #
|
68
|
+
######################
|
69
|
+
.DS_Store
|
70
|
+
.DS_Store?
|
71
|
+
._*
|
72
|
+
.Spotlight-V100
|
73
|
+
.Trashes
|
74
|
+
Icon?
|
75
|
+
ehthumbs.db
|
76
|
+
Thumbs.db
|
77
|
+
/Gemfile.lock
|
78
|
+
/coverage
|
79
|
+
/.editorconfig
|
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
language: ruby
|
2
|
+
sudo: false
|
3
|
+
|
4
|
+
cache: bundler
|
5
|
+
|
6
|
+
before_script:
|
7
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
8
|
+
- chmod +x ./cc-test-reporter
|
9
|
+
- ./cc-test-reporter before-build
|
10
|
+
|
11
|
+
script:
|
12
|
+
- bundle exec rspec
|
13
|
+
|
14
|
+
after_script:
|
15
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
16
|
+
|
17
|
+
rvm:
|
18
|
+
- 2.5
|
19
|
+
- 2.4
|
20
|
+
- 2.3
|
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 dale@getnotion.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
@@ -0,0 +1,18 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
|
7
|
+
# Generates coverage stats of specs
|
8
|
+
gem 'simplecov'
|
9
|
+
|
10
|
+
# Publishes coverage to codeclimate
|
11
|
+
gem 'codeclimate-test-reporter'
|
12
|
+
|
13
|
+
# Gives CircleCI more perspective on our tests
|
14
|
+
gem 'rspec_junit_formatter'
|
15
|
+
|
16
|
+
gem 'rspec'
|
17
|
+
|
18
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Dale Stevens
|
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,58 @@
|
|
1
|
+
[![Version ](https://img.shields.io/gem/v/active_registry.svg?maxAge=2592000)](https://rubygems.org/gems/active_registry)
|
2
|
+
[![Build Status ](https://travis-ci.org/TwilightCoders/active_registry.svg)](https://travis-ci.org/TwilightCoders/active_registry)
|
3
|
+
[![Code Climate ](https://api.codeclimate.com/v1/badges/a18ae809af878357acfa/maintainability)](https://codeclimate.com/github/TwilightCoders/active_registry/maintainability)
|
4
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/a18ae809af878357acfa/test_coverage)](https://codeclimate.com/github/TwilightCoders/active_registry/test_coverage)
|
5
|
+
[![Dependencies ](https://gemnasium.com/badges/github.com/TwilightCoders/active_registry.svg)](https://gemnasium.com/github.com/TwilightCoders/active_registry)
|
6
|
+
|
7
|
+
# ActiveRegistry
|
8
|
+
|
9
|
+
Provides a data structure for collecting homogeneous objects and indexing them for quick lookup.
|
10
|
+
|
11
|
+
## Requirements
|
12
|
+
Ruby 2.3+
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'active_registry'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install active_registry
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Person = Struct.new(:id, :name, :email)
|
34
|
+
|
35
|
+
registry = ActiveRegistry.new([
|
36
|
+
Person.new(1, 'Dale', 'dale@twilightcoders.net'),
|
37
|
+
Person.new(2, 'Dale', 'dale@chillywinds.com')
|
38
|
+
])
|
39
|
+
|
40
|
+
registry.index(:name)
|
41
|
+
|
42
|
+
d = registry[:name, 'Dale'].first
|
43
|
+
d.name = "Bob"
|
44
|
+
|
45
|
+
registry.find(:name, 'Bob')
|
46
|
+
```
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bundle` to install dependencies. Then, run `bundle exec rspec` to run the tests.
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/TwilightCoders/active_registry. 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.
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "active_registry"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "active_registry"
|
7
|
+
spec.version = ActiveRegistry::VERSION
|
8
|
+
spec.authors = ["Dale Stevens"]
|
9
|
+
spec.email = ["dale@twilightcoders.net"]
|
10
|
+
|
11
|
+
spec.summary = %q{Data Structure with multiple}
|
12
|
+
# spec.description = %q{}
|
13
|
+
spec.homepage = "https://github.com/TwilightCoders/active_registry."
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
if spec.respond_to?(:metadata)
|
17
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
18
|
+
else
|
19
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.files = `git ls-files -z`.split("\x0")
|
23
|
+
spec.bindir = 'bin'
|
24
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
25
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
26
|
+
spec.require_paths = ['lib', 'spec']
|
27
|
+
|
28
|
+
spec.required_ruby_version = '>= 2.2'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'pry-byebug', '~> 3'
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.0'
|
32
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
33
|
+
spec.add_development_dependency 'rspec', "~> 3.0"
|
34
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_registry"
|
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
|
+
|
data/bin/setup
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
class ActiveRegistry < Set
|
2
|
+
class MoreThanOneRecordFound < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
VERSION = "0.1.0"
|
6
|
+
|
7
|
+
DEFAULT_INDEX = :object_id
|
8
|
+
|
9
|
+
def initialize(*args, indexes: [])
|
10
|
+
@indexed = {}
|
11
|
+
super(*args)
|
12
|
+
reindex!(indexes)
|
13
|
+
end
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
to_a.inspect
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_h
|
20
|
+
@indexed
|
21
|
+
end
|
22
|
+
|
23
|
+
def indexes
|
24
|
+
@indexed.keys - [:object_id]
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(item)
|
28
|
+
@indexed.each do |idx, store|
|
29
|
+
ignore_setter(item, idx) if include?(item)
|
30
|
+
begin
|
31
|
+
idx_value = item.send(idx)
|
32
|
+
(store[idx_value] ||= Set.new).delete(item)
|
33
|
+
store.delete(idx_value) if store[idx_value].empty?
|
34
|
+
rescue NoMethodError => e
|
35
|
+
raise "#{item.name} cannot be added because indexable attribute (#{idx}) is missing."
|
36
|
+
end
|
37
|
+
end
|
38
|
+
super(item)
|
39
|
+
end
|
40
|
+
|
41
|
+
def add(item)
|
42
|
+
@indexed.each do |idx, store|
|
43
|
+
watch_setter(item, idx) unless include?(item)
|
44
|
+
begin
|
45
|
+
idx_value = item.send(idx)
|
46
|
+
(store[idx_value] ||= Set.new) << (item)
|
47
|
+
rescue NoMethodError => e
|
48
|
+
raise "#{item.name} cannot be added because indexable attribute (#{idx}) is missing."
|
49
|
+
end
|
50
|
+
end
|
51
|
+
super(item)
|
52
|
+
end
|
53
|
+
alias << add
|
54
|
+
|
55
|
+
def find!(search_criteria)
|
56
|
+
_find(search_criteria) { raise MoreThanOneRecordFound, "There were more than 1 records found" }
|
57
|
+
end
|
58
|
+
|
59
|
+
def find(search_criteria)
|
60
|
+
_find(search_criteria) { warn "There were more than 1 records found" }
|
61
|
+
end
|
62
|
+
|
63
|
+
def where(search_criteria)
|
64
|
+
sets = search_criteria.inject([]) do |sets, (idx, value)|
|
65
|
+
raise "No '#{idx}' index! Add it with '.index(:#{idx})'" unless @indexed.include?(idx)
|
66
|
+
sets << (@indexed.dig(idx, value) || Set.new)
|
67
|
+
end
|
68
|
+
|
69
|
+
subset_records = sets.reduce(sets.first, &:&)
|
70
|
+
subset_registry = ActiveRegistry.new(subset_records, indexes: indexes)
|
71
|
+
subset_registry
|
72
|
+
end
|
73
|
+
|
74
|
+
def index(*indexes)
|
75
|
+
indexes.each do |idx|
|
76
|
+
warn "Index #{idx} already exists!" and next if @indexed.key?(idx)
|
77
|
+
each { |item| watch_setter(item, idx) }
|
78
|
+
indexed_records = group_by { |a| a.send(idx) }
|
79
|
+
indexed_sets = Hash[indexed_records.keys.zip(indexed_records.values.map { |e| Set.new(e) })]
|
80
|
+
@indexed[idx] = indexed_sets
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def reindex!(indexes = [])
|
85
|
+
@indexed = {}
|
86
|
+
index(*([DEFAULT_INDEX] | indexes))
|
87
|
+
end
|
88
|
+
|
89
|
+
protected
|
90
|
+
|
91
|
+
def reindex(idx, item, old_value, new_value)
|
92
|
+
if (new_value != old_value)
|
93
|
+
@indexed[idx][old_value].delete item
|
94
|
+
(@indexed[idx][new_value] ||= Set.new).add item
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def _find(search_criteria)
|
101
|
+
results = where(search_criteria)
|
102
|
+
yield if block_given? && results.count > 1
|
103
|
+
results.first
|
104
|
+
end
|
105
|
+
|
106
|
+
def watch_setter(item, idx)
|
107
|
+
return if item.frozen?
|
108
|
+
__registry__ = self
|
109
|
+
item.public_methods.select { |m| m.match(/^#{idx}=$/) }.each do |original_method|
|
110
|
+
watched_method = "__watched_#{original_method}".to_sym
|
111
|
+
renamed_method = "__unwatched_#{original_method}".to_sym
|
112
|
+
next if item.methods.include?(watched_method)
|
113
|
+
|
114
|
+
item.singleton_class.class_eval do
|
115
|
+
define_method(watched_method) do |*args|
|
116
|
+
old_value = item.send(idx)
|
117
|
+
send(renamed_method, *args).tap do |new_value|
|
118
|
+
__registry__.send(:reindex, idx, item, old_value, new_value)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
alias_method renamed_method, original_method
|
122
|
+
alias_method original_method, watched_method
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def ignore_setter(item, idx)
|
128
|
+
return if item.frozen?
|
129
|
+
item.public_methods.select { |m| m.match(/^#{idx}=$/) }.each do |original_method|
|
130
|
+
watched_method = "__watched_#{original_method}".to_sym
|
131
|
+
renamed_method = "__unwatched_#{original_method}".to_sym
|
132
|
+
next unless item.methods.include?(watched_method)
|
133
|
+
item.singleton_class.class_eval do
|
134
|
+
alias_method original_method, renamed_method
|
135
|
+
remove_method(watched_method)
|
136
|
+
remove_method(renamed_method)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe ActiveRegistry do
|
4
|
+
Person = Struct.new(:id, :name, :email)
|
5
|
+
let(:u1) { Person.new(1, 'Dale', 'dale@twilightcoders.net') }
|
6
|
+
let(:u2) { Person.new(2, 'Dale', 'dale@billyjoel.com') }
|
7
|
+
let(:u3) { Person.new(3, 'Foo', 'foobar@twilightcoders.net') }
|
8
|
+
|
9
|
+
context 'Helper Methods' do
|
10
|
+
let!(:r1) { ActiveRegistry.new([ u1, u2 ]) }
|
11
|
+
|
12
|
+
it 'should not raise error' do
|
13
|
+
expect{r1.to_h}.to_not raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should not raise error' do
|
17
|
+
expect{r1.inspect}.to_not raise_error
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'Adding' do
|
22
|
+
let!(:r1) { ActiveRegistry.new([ u1, u2 ]) }
|
23
|
+
|
24
|
+
it 'should add the correct item' do
|
25
|
+
r1 << u3
|
26
|
+
|
27
|
+
expect(r1).to contain_mappings(
|
28
|
+
object_id: {
|
29
|
+
u1.object_id => [u1],
|
30
|
+
u2.object_id => [u2],
|
31
|
+
u3.object_id => [u3]
|
32
|
+
}
|
33
|
+
)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'Deleting' do
|
38
|
+
let!(:r1) { ActiveRegistry.new([ u1, u2 ]) }
|
39
|
+
|
40
|
+
before(:each) do
|
41
|
+
r1 << u3
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should remove the correct item' do
|
45
|
+
r1.delete(u2)
|
46
|
+
|
47
|
+
expect(r1).to contain_mappings(
|
48
|
+
object_id: {
|
49
|
+
u1.object_id => [u1],
|
50
|
+
u3.object_id => [u3]
|
51
|
+
}
|
52
|
+
)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "Access" do
|
57
|
+
let!(:registry) { ActiveRegistry.new([u1, u2, u3]) }
|
58
|
+
|
59
|
+
before(:each) do
|
60
|
+
registry.index(:name, :email)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should return a registry' do
|
64
|
+
subregistry = registry.where(name: 'Dale')
|
65
|
+
expect(subregistry).to be_a_kind_of(ActiveRegistry)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should be able to access with the subregistry' do
|
69
|
+
subregistry = registry.where(name: 'Dale')
|
70
|
+
item = subregistry.where(email: 'dale@billyjoel.com')
|
71
|
+
expect(item.first).to eq(u2)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should be able to access with the subregistry' do
|
75
|
+
subregistry = registry.where(name: 'Dale', email: 'dale@billyjoel.com')
|
76
|
+
expect(subregistry.first).to eq(u2)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should be able to access with the subregistry' do
|
80
|
+
subregistry = registry.where(name: 'Dale', email: 'snail@billyjoel.com')
|
81
|
+
expect(subregistry.count).to eq(0)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should find the element' do
|
85
|
+
item = registry.find(name: 'Dale', email: 'dale@billyjoel.com')
|
86
|
+
expect(item).to eq(u2)
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should find! the element' do
|
90
|
+
item = registry.find!(name: 'Dale', email: 'dale@billyjoel.com')
|
91
|
+
expect(item).to eq(u2)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should raise a MoreThanOneRecordFound exception' do
|
95
|
+
expect{registry.find!(name: 'Dale')}.to raise_error(ActiveRegistry::MoreThanOneRecordFound)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context "Indexing" do
|
100
|
+
let!(:registry) { ActiveRegistry.new([ u1, u2 ]) }
|
101
|
+
|
102
|
+
before(:each) do
|
103
|
+
registry.index(:name)
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'indexes' do
|
107
|
+
expect(registry).to contain_mappings(
|
108
|
+
object_id: {
|
109
|
+
u1.object_id => [u1],
|
110
|
+
u2.object_id => [u2]
|
111
|
+
},
|
112
|
+
name: {
|
113
|
+
"Dale" => [u1, u2]
|
114
|
+
}
|
115
|
+
)
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'reindexes' do
|
119
|
+
d = registry.where(name: 'Dale').first
|
120
|
+
d.name = "Bob"
|
121
|
+
|
122
|
+
expect(registry.where(name: 'Bob').first).to eq(d)
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'Error conditions' do
|
126
|
+
it 'should raise exception when adding' do
|
127
|
+
expect{registry.add("billyjoel")}.to raise_error(StandardError)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should raise exception when deleting' do
|
131
|
+
expect{registry.delete("billyjoel")}.to raise_error(StandardError)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'watches' do
|
137
|
+
Animal = Struct.new(:id, :name)
|
138
|
+
let!(:a1) { Animal.new(1, 'Boris') }
|
139
|
+
let!(:a1_original_methods) { a1.methods }
|
140
|
+
let!(:a1_original_method_count) { a1.methods.count }
|
141
|
+
let!(:registry) { ActiveRegistry.new([ a1 ]) }
|
142
|
+
|
143
|
+
before(:each) do
|
144
|
+
registry.index(:name)
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'should add only two methods' do
|
148
|
+
d = registry.where(name: 'Boris').first
|
149
|
+
expect(d.methods.count).to eq(a1_original_method_count + 2)
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should include expected methods' do
|
153
|
+
d = registry.where(name: 'Boris').first
|
154
|
+
expect(d.methods).to include(:__watched_name=, :__unwatched_name=)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'unwatches' do
|
159
|
+
let!(:registry) { ActiveRegistry.new([ u1, u2 ]) }
|
160
|
+
|
161
|
+
before(:each) do
|
162
|
+
registry.index(:name)
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should not include expected methods' do
|
166
|
+
d = registry.where(name: 'Dale').first
|
167
|
+
registry.delete(d)
|
168
|
+
|
169
|
+
expect(d.methods).to_not include(:__watched_name=, :__unwatched_name=)
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'should set the name after removing an element from the registry' do
|
173
|
+
d = registry.where(name: 'Dale').first
|
174
|
+
registry.delete(d)
|
175
|
+
|
176
|
+
d.name="Bob"
|
177
|
+
expect(d.name).to eq("Bob")
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
RSpec::Matchers.define :contain_mappings do |expected_mappings|
|
2
|
+
|
3
|
+
match do |registry|
|
4
|
+
expected_mappings.each do |index_name, expected_mapping|
|
5
|
+
expected_mapping.each do |index, expected|
|
6
|
+
actual = registry.where(index_name => index).to_a
|
7
|
+
|
8
|
+
actual.sort!{ |a, b| a.object_id <=> b.object_id }
|
9
|
+
expected.sort!{ |a, b| a.object_id <=> b.object_id }
|
10
|
+
|
11
|
+
return false unless actual == expected
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_registry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dale Stevens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry-byebug
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '12.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- dale@twilightcoders.net
|
72
|
+
executables:
|
73
|
+
- console
|
74
|
+
- setup
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".travis.yml"
|
81
|
+
- CODE_OF_CONDUCT.md
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- active_registry.gemspec
|
87
|
+
- bin/console
|
88
|
+
- bin/setup
|
89
|
+
- lib/active_registry.rb
|
90
|
+
- spec/active_registry/active_registry_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
- spec/support/custom_matchers.rb
|
93
|
+
homepage: https://github.com/TwilightCoders/active_registry.
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
allowed_push_host: https://rubygems.org
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
- spec
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '2.2'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.7.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Data Structure with multiple
|
119
|
+
test_files:
|
120
|
+
- spec/active_registry/active_registry_spec.rb
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
- spec/support/custom_matchers.rb
|