registry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +0 -0
  3. data/LICENSE +21 -0
  4. data/README.md +58 -0
  5. data/lib/registry.rb +141 -0
  6. metadata +105 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 85568ec14b863810f3a55309ba34bb90e5f8db07dbf47992f960dd2f6afc54af
4
+ data.tar.gz: 6a119eff0fc0f2d0bec47045c28aeeeb0f4035e288c0fdc787874b40300b58fc
5
+ SHA512:
6
+ metadata.gz: 2b8454fe659ee8636ff65a343ec13fd82eab74d68c6e2ce4811536b1397df6897edb307acfe8287f560f212f87cc826b5929d36a88babdec214b6687b12580fd
7
+ data.tar.gz: 286388bd07a40b74e67371dd422aed24730b888e0fff56120b55c47e908fc35df2d6dcf4d3053b762cd684104f9e24a5da6e6ff64eb78fbe4654a1d3e389fb9d
File without changes
data/LICENSE 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.
@@ -0,0 +1,58 @@
1
+ [![Version ](https://img.shields.io/gem/v/registry.svg)](https://rubygems.org/gems/registry)
2
+ [![Build Status ](https://travis-ci.org/TwilightCoders/registry.svg)](https://travis-ci.org/TwilightCoders/registry)
3
+ [![Code Climate ](https://api.codeclimate.com/v1/badges/a18ae809af878357acfa/maintainability)](https://codeclimate.com/github/TwilightCoders/registry/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/a18ae809af878357acfa/test_coverage)](https://codeclimate.com/github/TwilightCoders/registry/test_coverage)
5
+ [![Dependencies ](https://gemnasium.com/badges/github.com/TwilightCoders/registry.svg)](https://gemnasium.com/github.com/TwilightCoders/registry)
6
+
7
+ # Registry
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 'registry'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install registry
29
+
30
+ ## Usage
31
+
32
+ ```ruby
33
+ Person = Struct.new(:id, :name, :email)
34
+
35
+ registry = Registry.new([
36
+ Person.new(1, 'Dale', 'jason@twilightcoders.net'),
37
+ Person.new(2, 'Dale', 'dale@twilightcoders.net')
38
+ ])
39
+
40
+ registry.index(:name)
41
+
42
+ d = registry[:name, 'Dale'].first
43
+ d.name = "Jason"
44
+
45
+ registry.find(:name, 'Jason')
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/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).
@@ -0,0 +1,141 @@
1
+ class Registry < Set
2
+
3
+ MoreThanOneRecordFound = Class.new(StandardError)
4
+
5
+ VERSION = "0.0.1"
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 = Registry.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
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: registry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dale Stevens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-29 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
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CHANGELOG.md
77
+ - LICENSE
78
+ - README.md
79
+ - lib/registry.rb
80
+ homepage: https://github.com/TwilightCoders/registry.
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ - spec
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '2.3'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Data structure for quick item lookup via indexes.
105
+ test_files: []