registration_office 0.3.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9e9a99e58c2ff2cdeaf005e04ae3cfeece220470587888ef5d8854970c040d0b
4
+ data.tar.gz: 39a7396787573856edb05c2636e7ee7c4cafe2f59410ccb5a9f7b15c8cf3489d
5
+ SHA512:
6
+ metadata.gz: 2edbd3e31eb5436b8d43e5ef24f7d0582e25b9d1b59f98d9195b5427e9e4984f58ce152f652fa7949386bbd9cccc0ace2d9a5e6bcd13e9668711f1d77ee4656b
7
+ data.tar.gz: 3868114684008aa27fe13c68ba1fb41275651919f0b7218214e5098fd77fed30d8aa7807433bd7474185ad48aa5ca782f0377444ac4872f47ac460e2aa0514dd
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,41 @@
1
+ plugins:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.4
7
+ NewCops: enable
8
+
9
+ Layout/FirstArgumentIndentation:
10
+ EnforcedStyle: special_for_inner_method_call
11
+
12
+ Style/StringLiterals:
13
+ Enabled: true
14
+ EnforcedStyle: single_quotes
15
+
16
+ Style/StringLiteralsInInterpolation:
17
+ Enabled: true
18
+ EnforcedStyle: single_quotes
19
+
20
+ Style/SymbolArray:
21
+ Enabled: false
22
+
23
+ Layout/LineLength:
24
+ Max: 120
25
+
26
+ RSpec/NestedGroups:
27
+ AllowedGroups:
28
+ - describe
29
+
30
+ RSpec/DescribedClass:
31
+ Enabled: false
32
+
33
+ RSpec/ContextWording:
34
+ Prefixes:
35
+ - when
36
+ - with
37
+ - without
38
+ - that
39
+ - if
40
+ - unless
41
+ - for
data/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.3.0] - 2026-09-08
4
+
5
+ - Adds option for additional payload for each key:
6
+ ```ruby
7
+ register(:some_name, keys: [:key_one])
8
+ # is identical to
9
+ register(:some_name, keys: [key_one: []])
10
+
11
+ # The method `#use!` allows you to access the payload as well:
12
+ demand (:some_name).use!(:key_one)
13
+ ```
14
+
15
+ ## [0.2.1] - 2026-09-08
16
+
17
+ - Prevent overriding of already defined demands when `RegistrationOffice[:demand]` is included multiple times.
18
+ This happens automatically when defining both a demand and a registry within the same object.
19
+
20
+ ## [0.2.0] - 2026-09-07
21
+
22
+ - Uses multiple, named registers and demands instead of anonymous
23
+ - Enforces registering keys of same register all at once
24
+
25
+ ## [0.1.0] - 2026-09-04
26
+
27
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ek@aec3.de. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AEC3 Deutschland GmbH
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/README.md ADDED
@@ -0,0 +1,260 @@
1
+ # RegistrationOffice
2
+
3
+ The `registration_office` gem allows registering a collection of key-value pairs. The registration is per class (or
4
+ module), which is useful for registering possible failure codes that a service may return. See also the example in
5
+ section [*Example*](#example).
6
+
7
+ ## Installation
8
+
9
+ ### Using `bundler`
10
+
11
+ If your application uses a `Gemfile`, add it there:
12
+
13
+ ```ruby
14
+ # Gemfile
15
+ gem 'registration_office'
16
+ ```
17
+
18
+ *or* add the gem to the application's Gemfile by executing:
19
+
20
+ ```shell
21
+ $ bundle add registration_office
22
+ ```
23
+
24
+ ### Without `bundler`
25
+
26
+ If bundler is not being used to manage dependencies, install the gem by executing:
27
+
28
+ ```shell
29
+ $ gem install registration_office
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Define a Register
35
+
36
+ #### Keys only
37
+
38
+ Just add `include RegistrationOffice[:registration]` to any class or model
39
+ and start registering with `register(<name>, keys: [<keys>])`:
40
+
41
+ ```ruby
42
+
43
+ class MyService
44
+ include RegistrationOffice[:registration]
45
+
46
+ register(
47
+ :some_name,
48
+ keys: [
49
+ :some_failure,
50
+ :another_api_failure
51
+ ]
52
+ )
53
+ end
54
+ ```
55
+
56
+ While keys can be any Ruby object, it is recommended to use `Symbol`s for readability.
57
+
58
+ #### Keys with Payload
59
+
60
+ The gem's version `v0.3.0` adds optional payload for each key:
61
+
62
+ ```ruby
63
+ class MyService
64
+ include RegistrationOffice[:registration]
65
+
66
+ register(
67
+ :some_name,
68
+ keys: [
69
+ { some_failure: { message: 'Something went wrong' } },
70
+ :another_api_failure
71
+ ]
72
+ )
73
+ end
74
+
75
+ MyService.demand(:some_name).key!(:some_failure)
76
+ # => :some_failure
77
+
78
+ MyService.demand(:some_name).use!(:some_failure)
79
+ # => [:some_failure, { :message => "Something went wrong" }]
80
+
81
+ MyService.demand(:some_name).use!(:another_api_failure)
82
+ # => [:another_api_failure, []]
83
+ ```
84
+
85
+ ### Use a Register
86
+
87
+ The consuming class or module needs to include the `:demand` module and show their demand with `.add_demand`.
88
+
89
+ The register can then be queried by using `demand.key!(<key>)`:
90
+
91
+ ```ruby
92
+
93
+ class ServiceCaller
94
+ include RegistrationOffice[:demand]
95
+ add_demand(:some_name, MyService)
96
+
97
+ def call
98
+ demand(:some_name).key!(:some_failure)
99
+ end
100
+
101
+ def bad_call
102
+ demand(:some_name).key!(:unknown_key)
103
+ end
104
+
105
+ def unregistered_name
106
+ demand(:i_do_not_know_you)
107
+ end
108
+ end
109
+
110
+ ServiceCaller.call
111
+ # => :some_failure
112
+
113
+ ServiceCaller.bad_call
114
+ # => RegistrationOffice::Register::UnregisteredKeyError:
115
+ # 'Register `some_name` in `MyService`: key `unknown_key` is not registered'
116
+
117
+ ServiceCaller.unregistered_name
118
+ # => RegistrationOffice::Registers::UnregisteredRegisterNameError:
119
+ # A register with name `i_do_not_know_you` is not registered
120
+ ```
121
+
122
+ Note that you do not need to define a demand for the registering object itself.
123
+ As soon as you `register` a register, the corresponding `demand` methods (class/mdoule and instance method)
124
+ are automatically added.
125
+
126
+ ### Multiple Registers
127
+
128
+ It is possible to define more than one register.
129
+ You may have guessed it: this is what the first argument in `.register` is for.
130
+ It defines the name of the register and can be named as you want.
131
+ Defining another register with the same name within the same object raises
132
+ `RegistrationOffice::Registers::DuplicateRegisterNameError`.
133
+
134
+ **Note**
135
+
136
+ The registers are isolated within their registering object.
137
+ You can use the same name in different registering objects.
138
+ But be aware that this restricts you from using them in the same demanding objects:
139
+
140
+ ```ruby
141
+ class ServiceCaller
142
+ include RegistrationOffice[:demand]
143
+ add_demand(:some_name, MyService)
144
+ add_demand(:some_name, MyOtherService)
145
+ end
146
+ # => RegistrationOffice::Demand::DuplicateDemandNameError
147
+ # "A register with name `some_name` is already demanded"
148
+
149
+ ```
150
+
151
+ ## Example
152
+
153
+ Assume following `BicycleDealer` class:
154
+
155
+ ```ruby
156
+
157
+ class BicycleDealer
158
+ include RegistrationOffice[:registration]
159
+
160
+ register(
161
+ :failures,
162
+ keys: [
163
+ :invalid_bicycle_configuration,
164
+ :invalid_coupon_code,
165
+ :bicycle_not_in_stock,
166
+ :customer_not_solvent,
167
+ ]
168
+ )
169
+
170
+ def call(customers_order)
171
+ return demand(:failures).key!(:insult) if customers_order == :car
172
+
173
+ if customers_order == :bicycle_with_zero_wheels
174
+ return demand(:failures).key!(:invalid_bicycle_configuration)
175
+ end
176
+
177
+ Order.new.invoice(customers_order)
178
+ end
179
+ end
180
+ ```
181
+
182
+ And an `Order` class that wants to use the registered keys from `BicycleDealer`:
183
+
184
+ ```ruby
185
+
186
+ class Order
187
+ include RegistrationOffice[:demand]
188
+ add_demand(:failures, BicycleDealer)
189
+
190
+ def invoice(customers_order)
191
+ case customers_order
192
+ when :golden_bike
193
+ demand(:failures).key!(:customer_not_solvent)
194
+ when :cool_bike
195
+ demand(:failures).key!(:bicycle_not_in_stock)
196
+ else
197
+ 'thx for your order'
198
+ end
199
+ end
200
+ end
201
+ ```
202
+
203
+ Unregistered keys raise an error:
204
+
205
+ ```ruby
206
+ BicycleDealer.new.call(:car)
207
+ # raises UnregisteredKeyError:
208
+ # => RegistrationOffice::Register::UnregisteredKeyError:
209
+ # 'Register `failures` in `BicycleDealer`: key `insult` is not registered'
210
+
211
+ BicycleDealer.new.call(:golden_bike)
212
+ # => :customer_not_solvent
213
+ ```
214
+
215
+ Get all registered keys with `registry.keys`
216
+
217
+ ```ruby
218
+ BicycleDealer.registry.keys
219
+ # => [:invalid_bicycle_configuration, :invalid_coupon_code, ...]
220
+ ```
221
+
222
+ Which is the same as `demand.keys` within demanding objects
223
+
224
+ ```ruby
225
+ Order.demand.keys
226
+ # => [:invalid_bicycle_configuration, :invalid_coupon_code, ...]
227
+ ```
228
+
229
+ ## Roadmap
230
+
231
+ If this gem proves to be useful, following topics may be added:
232
+
233
+ - Meta:
234
+ - Publishing on https://gem.coop
235
+ - CI/CD via GitHub Actions (`rspec`, `rubocop`, publishing)
236
+ - Features: currently nothing more planned
237
+
238
+ ## Development
239
+
240
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
241
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
242
+
243
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
244
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
245
+ push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
246
+
247
+ ## Contributing
248
+
249
+ Bug reports and pull requests are welcome on GitHub at https://github.com/AEC3-Deutschland-GmbH/registration_office.
250
+ This project is intended to be a safe, welcoming space for collaboration. Contributors are expected to adhere to
251
+ the [code of conduct](CODE_OF_CONDUCT.md).
252
+
253
+ ## License
254
+
255
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
256
+
257
+ ## Code of Conduct
258
+
259
+ Everyone interacting in the RegistrationOffice project's codebases, issue trackers, chat rooms and mailing lists is
260
+ expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RegistrationOffice
4
+ # Defines the "receiving end" of a register, i.e. the demanding (consuming) object.
5
+ # This module is not intended to be included directly.
6
+ # Instead, the +.[]+ singleton method should be used,
7
+ # which returns a new module that can be included.
8
+ module Demand
9
+ class DuplicateDemandNameError < StandardError; end
10
+
11
+ class << self
12
+ # rubocop:disable-next Metrics/MethodLength
13
+ def included(base)
14
+ return if registry_demand_already_defined?(base)
15
+
16
+ mod = prepare_mod(Module.new)
17
+ base.module_eval do
18
+ const_set('RegistryDemand', mod)
19
+
20
+ define_singleton_method(:add_demand) do |register_name, register_object|
21
+ const_get('RegistryDemand').add_demand(
22
+ register_name,
23
+ register_object.registry(register_name)
24
+ )
25
+ end
26
+
27
+ define_singleton_method(:demand) { |register_name| const_get('RegistryDemand').demand(register_name) }
28
+ define_method(:demand) { |register_name| self.class.const_get('RegistryDemand').demand(register_name) }
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def registry_demand_already_defined?(base)
35
+ defined?(base::RegistryDemand)
36
+ end
37
+
38
+ # rubocop:disable-next Metrics/MethodLength
39
+ def prepare_mod(mod)
40
+ mod.module_eval do
41
+ class << self
42
+ def demands
43
+ @demands ||= Registers.new
44
+ end
45
+
46
+ def demand(register_name)
47
+ demands.fetch(register_name)
48
+ end
49
+
50
+ def add_demand(register_name, register)
51
+ demands.register_a_register(register_name, register)
52
+ rescue Registers::DuplicateRegisterNameError
53
+ raise(
54
+ DuplicateDemandNameError,
55
+ "A register with name `#{register_name}` is already demanded"
56
+ )
57
+ end
58
+ end
59
+ end
60
+
61
+ mod
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RegistrationOffice
4
+ # Provides a named key register.
5
+ class Register
6
+ class DuplicateKeyError < StandardError; end
7
+
8
+ class UnregisteredKeyError < StandardError; end
9
+
10
+ attr_reader :name
11
+
12
+ def initialize(registering_object)
13
+ @registering_object = registering_object
14
+ end
15
+
16
+ def register_keys(name, keys:)
17
+ @name = name
18
+ keys.each do |key|
19
+ key_as_hash, actual_key = key_to_hash(key)
20
+ raise DuplicateKeyError, "#{identifier}: key `#{actual_key}` already registered" if registry.key?(actual_key)
21
+
22
+ registry.merge!(key_as_hash)
23
+ end
24
+ end
25
+
26
+ def keys
27
+ registry.keys
28
+ end
29
+
30
+ def use!(key)
31
+ raise UnregisteredKeyError, "#{identifier}: key `#{key}` is not registered" unless key?(key)
32
+
33
+ [key, registry[key]]
34
+ end
35
+
36
+ def key!(key)
37
+ raise UnregisteredKeyError, "#{identifier}: key `#{key}` is not registered" unless key?(key)
38
+
39
+ key
40
+ end
41
+
42
+ def key?(key)
43
+ registry.key?(key)
44
+ end
45
+
46
+ def registry
47
+ @registry ||= {}
48
+ end
49
+
50
+ def all
51
+ registry.to_h
52
+ end
53
+
54
+ private
55
+
56
+ def identifier
57
+ "Register `#{name}` in `#{@registering_object}`"
58
+ end
59
+
60
+ def key_to_hash(key)
61
+ if key.is_a?(Hash)
62
+ [key, key.keys.first]
63
+ else
64
+ [{ key => [] }, key]
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RegistrationOffice
4
+ # Provides a register of registers
5
+ class Registers
6
+ class DuplicateRegisterNameError < StandardError; end
7
+
8
+ class UnregisteredRegisterNameError < StandardError; end
9
+
10
+ def register_a_register(register_name, register)
11
+ if register_registry.key?(register_name)
12
+ raise DuplicateRegisterNameError, "A register with name `#{register_name}` is already registered"
13
+ end
14
+
15
+ register_registry[register_name] = register
16
+ end
17
+
18
+ def fetch(register_name)
19
+ unless register_registry.key?(register_name)
20
+ raise UnregisteredRegisterNameError, "A register with name `#{register_name}` is not registered"
21
+ end
22
+
23
+ register_registry[register_name]
24
+ end
25
+
26
+ def all
27
+ register_registry
28
+ end
29
+
30
+ private
31
+
32
+ def register_registry
33
+ @register_registry ||= {}
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'register'
4
+ require_relative 'registers'
5
+
6
+ module RegistrationOffice
7
+ # Provides the methods +register+ and +registry+ when included.
8
+ module Registry
9
+ # rubocop:disable-next Metrics/AbcSize, Metrics/MethodLength, Metrics/BlockLength
10
+ def self.included(base)
11
+ # This is evaluated withing the including class/module
12
+ base.module_eval do
13
+ mod = const_set('RegistryStore', Module.new)
14
+
15
+ # This is evaluated withing the dynamically created, nested module "RegistryStore"
16
+ mod.module_eval do
17
+ define_singleton_method(:registering_object) { base }
18
+
19
+ class << self
20
+ def registers
21
+ @registers ||= Registers.new
22
+ end
23
+
24
+ def registry(name)
25
+ registers.fetch(name)
26
+ end
27
+
28
+ def all
29
+ registry.registry
30
+ end
31
+
32
+ def use!(key)
33
+ registry.use!(key)
34
+ end
35
+
36
+ def key!(key)
37
+ registry.key!(key)
38
+ end
39
+
40
+ def key?(key)
41
+ registry.key?(key)
42
+ end
43
+
44
+ private
45
+
46
+ def register(register_name, keys:)
47
+ register = Register.new(registering_object).tap { it.register_keys(register_name, keys:) }
48
+ registers.register_a_register(register_name, register)
49
+ registering_object.add_demand(register_name, registering_object)
50
+ end
51
+ end
52
+ end
53
+
54
+ # This is evaluated on "self" for the including class/module
55
+ class << self
56
+ def registry(name)
57
+ const_get('RegistryStore').registry(name)
58
+ end
59
+
60
+ private
61
+
62
+ def register(...)
63
+ const_get('RegistryStore').send(:register, ...)
64
+ end
65
+ end
66
+
67
+ # "Delegate" #registry to class
68
+ def registry(name)
69
+ self.class.registry(name)
70
+ end
71
+
72
+ include RegistrationOffice[:demand]
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RegistrationOffice
4
+ VERSION = '0.3.0'
5
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'registration_office/version'
4
+ require_relative 'registration_office/registry'
5
+ require_relative 'registration_office/demand'
6
+
7
+ # Assume following BicycleDealer class:
8
+ # class BicycleDealer
9
+ # include RegistrationOffice[:registration]
10
+ #
11
+ # register(:failures, keys: [
12
+ # :invalid_bicycle_configuration,
13
+ # :invalid_coupon_code,
14
+ # :bicycle_not_in_stock,
15
+ # :customer_not_solvent]
16
+ # )
17
+ #
18
+ # def call(customers_order)
19
+ # return demand(:failures).key!(:insult) if customers_order == :car
20
+ #
21
+ # if customers_order == :bicycle_with_zero_wheels
22
+ # return demand(:failures).key!(:invalid_bicycle_configuration)
23
+ # end
24
+ #
25
+ # Order.new.invoice(customers_order)
26
+ # end
27
+ # end
28
+ #
29
+ # And an Order class that wants to use the registered keys from BicycleDealer:
30
+ # class Order
31
+ # include RegistrationOffice[:demand]
32
+ # add_demand(:failures, BicycleDealer)
33
+ #
34
+ # def invoice(customers_order)
35
+ # case customers_order
36
+ # when :golden_bike
37
+ # demand(:failures).key!(:customer_not_solvent)
38
+ # when :cool_bike
39
+ # demand(:failures).key!(:bicycle_not_in_stock)
40
+ # else
41
+ # 'thx for your order'
42
+ # end
43
+ # end
44
+ # end
45
+ #
46
+ # Unregistered keys raise an error:
47
+ # BicycleDealer.new.call(:car)
48
+ # # raises UnregisteredKeyError:
49
+ # # => RegistrationOffice::Register::UnregisteredKeyError:
50
+ # # 'Register `failures` in `BicycleDealer`: key `insult` is not registered'
51
+ #
52
+ # BicycleDealer.new.call(:golden_bike)
53
+ # # => :customer_not_solvent
54
+ #
55
+ # Get all registered keys with +registry.keys+
56
+ # BicycleDealer.registry(:failures).keys
57
+ # # => [:invalid_bicycle_configuration, :invalid_coupon_code, ...]
58
+ # Which is the same as +demand.keys+ within demanding objects
59
+ # Order.demand(:failures).keys
60
+ # # => [:invalid_bicycle_configuration, :invalid_coupon_code, ...]
61
+ module RegistrationOffice
62
+ def self.[](module_name)
63
+ case module_name
64
+ when :registration
65
+ Registry
66
+ when :demand
67
+ Demand
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+ module RegistrationOffice
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: registration_office
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Kaulfuß
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: 'Allows named registers within modules and classes
13
+
14
+ '
15
+ email:
16
+ - ek@aec3.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - lib/registration_office.rb
29
+ - lib/registration_office/demand.rb
30
+ - lib/registration_office/register.rb
31
+ - lib/registration_office/registers.rb
32
+ - lib/registration_office/registry.rb
33
+ - lib/registration_office/version.rb
34
+ - sig/registration_office.rbs
35
+ homepage: https://github.com/araccaine/registration_office
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ rubygems_mfa_required: 'true'
40
+ homepage_uri: https://github.com/araccaine/registration_office
41
+ source_code_uri: https://github.com/araccaine/registration_office
42
+ changelog_uri: https://github.com/araccaine/registration_office
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '3.4'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.6.9
58
+ specification_version: 4
59
+ summary: Allows named registers within modules and classes
60
+ test_files: []