manufacturable 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +283 -0
- data/Rakefile +6 -0
- data/assets/factory.png +0 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/manufacturable.rb +25 -0
- data/lib/manufacturable/builder.rb +33 -0
- data/lib/manufacturable/config.rb +29 -0
- data/lib/manufacturable/factory.rb +15 -0
- data/lib/manufacturable/item.rb +18 -0
- data/lib/manufacturable/object_factory.rb +7 -0
- data/lib/manufacturable/railtie.rb +9 -0
- data/lib/manufacturable/registrar.rb +77 -0
- data/lib/manufacturable/version.rb +3 -0
- data/manufacturable.gemspec +34 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 554e6c657da02aee6b562be4f814b2b5c07b084ffb96a788f703a7ac8dcd882e
|
4
|
+
data.tar.gz: decfafcea45c7577d1188479cafbfb67dc79c12cf9c1b0e9bba80eeaa4f4ced3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5079fca78679c717c8596779ff6fe5eb3c53f174471aff0d0445c5ac7a2651a73d3ad185bb530bd3ad7dd74679fa86d6a2011aba9b463a079e326eb67185de85
|
7
|
+
data.tar.gz: 49fdc92529428e396de87edaebb9b1f9802cb7fc3f4b3d12212707c5c3663c1cbd1f17dc562c13d99cd992c4850382cc4f15698348c3ca11f0b5f7fefcf2cbba
|
data/.gitignore
ADDED
data/.rspec
ADDED
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 support@firsttry.software. 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 [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Alan Ridlehoover and Fito von Zastrow
|
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,283 @@
|
|
1
|
+
<img src="./assets/factory.png" alt="Factory">
|
2
|
+
|
3
|
+
# Manufacturable
|
4
|
+
|
5
|
+
Manufacturable is a factory that builds self-registering objects.
|
6
|
+
|
7
|
+
It leverages self-registration to move factory setup from case statements,
|
8
|
+
hashes, and configuration files to a simple DSL within the instantiable
|
9
|
+
classes themselves. Giving classes the responsibility of registering
|
10
|
+
themselves with the factory does two things. It allows the factory to be
|
11
|
+
[extended without modification][ocp]. And, it leaves the factory with only
|
12
|
+
[one responsibility][srp]: building objects.
|
13
|
+
|
14
|
+
## Motivation
|
15
|
+
|
16
|
+
We wrote Manufacturable so we wouldn't have to keep modifying our factory
|
17
|
+
code every time we needed to add functionanlity to our applications. For
|
18
|
+
example, consider this factory:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class AutomobileFactory
|
22
|
+
def self.build(type, *args)
|
23
|
+
case type
|
24
|
+
when :sedan
|
25
|
+
Sedan.new(*args)
|
26
|
+
when :coupe
|
27
|
+
Coupe.new(*args)
|
28
|
+
when :convertible
|
29
|
+
Convertible.new(*args)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
If you want to start building `Hatchback` objects, you'll need to modify the
|
36
|
+
factory. To solve this problem in Ruby, factories are often built using
|
37
|
+
metaprogramming, like this:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class AutomobileFactory
|
41
|
+
def self.build(type, *args)
|
42
|
+
Object.const_get(type.capitalize)&.new(*args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
But, this very simple factory relies on a convention: the type symbol must
|
48
|
+
match the name of the class. This means that classes with namespaces, or
|
49
|
+
symbols with underscores will not work. In other words, you could not use the
|
50
|
+
symbol `:four_door` to build a `Sedan` object.
|
51
|
+
|
52
|
+
Manufacturable solves these problems by allowing classes to register themselves
|
53
|
+
with the factory using a key of their choosing. This means you never have to
|
54
|
+
modify the factory code again.
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
### The Basics
|
59
|
+
|
60
|
+
A class may register itself with Manufacturable like this:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class Sedan
|
64
|
+
extend Manufacturable::Item
|
65
|
+
|
66
|
+
corresponds_to :four_door
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
Extending `Manufacturable::Item` adds the Manufacturable DSL to the class.
|
71
|
+
Calling `corresponds_to` with a key registers that class with the factory.
|
72
|
+
|
73
|
+
Once registed, a class may be instantiated like this:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
Manufacturable.build(Object, :four_door, *args)
|
77
|
+
```
|
78
|
+
|
79
|
+
Note the first parameter. This is the parent class of the registered class.
|
80
|
+
In this case, the parent class happens to be `Object`. So, Manufacturable
|
81
|
+
registered the `Sedan` class under the `Object` namespace to prevent key
|
82
|
+
collision. To instantiate the `Sedan`, we need to request the `:four_door`
|
83
|
+
key from the `Object` namespace.
|
84
|
+
|
85
|
+
For convenience, Manufacturable provides an `ObjectFactory` to build objects
|
86
|
+
that are stored in the `Object` namespace:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
Manufacturable::ObjectFactory.build(:four_door, *args)
|
90
|
+
```
|
91
|
+
|
92
|
+
In most cases, though, your class will actually inherit from a specific class
|
93
|
+
other than `Object`. For example, it is likely that the `Sedan` class would
|
94
|
+
inherit from an `Automobile` class. If that were the case, you would pass
|
95
|
+
`Automobile` as the first parameter to `Manufacturable.build`:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class Automobile
|
99
|
+
extend Manufacturable::Item
|
100
|
+
end
|
101
|
+
|
102
|
+
class Sedan < Automobile
|
103
|
+
corresponds_to :four_door
|
104
|
+
end
|
105
|
+
|
106
|
+
Manufacturable.build(Automobile, :four_door, *args)
|
107
|
+
```
|
108
|
+
|
109
|
+
That's all you need to know to begin using Manufacturable. But, it's not all
|
110
|
+
there is to know. Manunfacturable allows you to:
|
111
|
+
|
112
|
+
* [Configure your own factory classes](#using-factory-classes)
|
113
|
+
* [Define a default manufacturable item](#defining-a-default-manufacturable-item)
|
114
|
+
* [Register multiple classes under the same key within a namespace](#registering-multiple-classes-under-the-same-key-within-a-namespace)
|
115
|
+
* [Register a class to correspond with an entire namespace](#registering-a-class-to-correspond-with-an-entire-namespace)
|
116
|
+
|
117
|
+
### Using Factory Classes
|
118
|
+
|
119
|
+
Manufacturable also has a DSL for creating factories:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
class AutomobileFactory
|
123
|
+
extend Manufacturable::Factory
|
124
|
+
|
125
|
+
manufactures Automobile
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
Extending `Manufacturable::Factory` adds the DSL to the factory class.
|
130
|
+
Calling `manufactures` with a class designates it as the namespace for the
|
131
|
+
factory.
|
132
|
+
|
133
|
+
Once configured, you can use the `AutomobileFactory` to build objects from
|
134
|
+
classes in the `Automobile` namespace:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
AutomobileFactory.build(:four_door, *args)
|
138
|
+
```
|
139
|
+
|
140
|
+
### Defining a Default Manufacturable Item
|
141
|
+
|
142
|
+
What happens when Manufacturable is unable to find the key you're looking for?
|
143
|
+
That depends on what you tell Manufacturable. By default, it will return `nil`
|
144
|
+
when it does not find a class registered at a specific key. But, you can also
|
145
|
+
configure Manufacturable's response. This allows you to implement the [null
|
146
|
+
object pattern][nop].
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
class NullAutomobile < Automobile
|
150
|
+
default_manufacturable
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
Now, your calling code does not have to check for `nil` before calling a method
|
155
|
+
on the class:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
AutomobileFactory.build(:lemon, *args).drive
|
159
|
+
```
|
160
|
+
|
161
|
+
### Registering Multiple Classes Under the Same Key within a Namespace
|
162
|
+
|
163
|
+
Manufacturable allows you to register multiple classes under the same key:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
class StandardEngine < Component
|
167
|
+
corresponds_to :sedan
|
168
|
+
end
|
169
|
+
|
170
|
+
class AutomaticTransmission < Component
|
171
|
+
corresponds_to :sedan
|
172
|
+
end
|
173
|
+
|
174
|
+
class PowerfulEngine < Component
|
175
|
+
corresponds_to :coupe
|
176
|
+
end
|
177
|
+
|
178
|
+
class ManualTransmission < Component
|
179
|
+
corresponds_to :coupe
|
180
|
+
end
|
181
|
+
```
|
182
|
+
|
183
|
+
Then, when you request that key, you'll receive an array containing a new
|
184
|
+
instance of each class registered under that key.
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
ComponentFactory.build(:sedan, *args)
|
188
|
+
# => [#<StandardEngine:0x00007fad6c07e858>, #<AutomaticTransmission:0x00007fad6c07e808>]
|
189
|
+
|
190
|
+
ComponentFactory.build(:coupe, *args)
|
191
|
+
# => [#<PowerfulEngine:0x00007fad6c07e858>, #<ManualTransmission:0x00007fad6c07e808>]
|
192
|
+
```
|
193
|
+
|
194
|
+
### Registering a Class to Correspond with an Entire Namespace
|
195
|
+
|
196
|
+
Manufacturable will also let you register a class that corresponds with all
|
197
|
+
of the keys in a namespace:
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
class HeadLight < Component
|
201
|
+
corresponds_to_all
|
202
|
+
end
|
203
|
+
```
|
204
|
+
|
205
|
+
Now, the `ComponentFactory` will include `HeadLight` objects for both the
|
206
|
+
`:sedan` and `:coupe`.
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
ComponentFactory.build(:sedan, *args)
|
210
|
+
# => [
|
211
|
+
# #<StandardEngine:0x00007fad6c07e858>,
|
212
|
+
# #<AutomaticTransmission:0x00007fad6c07e808>,
|
213
|
+
# #<HeadLight:0x00007fad6c07e667>
|
214
|
+
# ]
|
215
|
+
|
216
|
+
ComponentFactory.build(:coupe, *args)
|
217
|
+
# => [
|
218
|
+
# #<PowerfulEngine:0x00007fad6c07e858>,
|
219
|
+
# #<ManualTransmission:0x00007fad6c07e808>,
|
220
|
+
# #<HeadLight:0x00007fad6c07e667>
|
221
|
+
# ]
|
222
|
+
```
|
223
|
+
|
224
|
+
## Installation
|
225
|
+
|
226
|
+
Add this line to your application's Gemfile:
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
gem 'manufacturable'
|
230
|
+
```
|
231
|
+
|
232
|
+
And then execute:
|
233
|
+
|
234
|
+
$ bundle install
|
235
|
+
|
236
|
+
Or install it yourself as:
|
237
|
+
|
238
|
+
$ gem install manufacturable
|
239
|
+
|
240
|
+
If you are using Manufacturable with Rails, you'll need an initializer to tell
|
241
|
+
manufacturable where the classes are so they can be autoloaded.
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
Manufacturable.config do |config|
|
245
|
+
config.paths << Rails.root.join('app', 'automobiles')
|
246
|
+
config.paths << Rails.root.join('app', 'components')
|
247
|
+
end
|
248
|
+
```
|
249
|
+
|
250
|
+
## Development
|
251
|
+
|
252
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then,
|
253
|
+
run `rake spec` to run the tests. You can also run `bin/console` for an
|
254
|
+
interactive prompt that will allow you to experiment. To install this gem
|
255
|
+
onto your local machine, run `bundle exec rake install`.
|
256
|
+
|
257
|
+
## Contributing
|
258
|
+
|
259
|
+
Bug reports and pull requests are welcome on [GitHub][git].
|
260
|
+
|
261
|
+
## License
|
262
|
+
|
263
|
+
The gem is available as open source under the terms of the [MIT License][mit].
|
264
|
+
|
265
|
+
## Code of Conduct
|
266
|
+
|
267
|
+
Everyone interacting in the Manufacturable project's codebases, issue trackers,
|
268
|
+
chat rooms and mailing lists is expected to follow the [code of conduct][cod].
|
269
|
+
|
270
|
+
## Acknowledgements
|
271
|
+
|
272
|
+
Manufacturable was inspired by work we did at Entelo on [Industrialist][ind].
|
273
|
+
We will be forever grateful to the people at Entelo for giving us the
|
274
|
+
opportunity to work on things we're still proud of today.
|
275
|
+
|
276
|
+
[srp]: https://en.wikipedia.org/wiki/Single-responsibility_principle
|
277
|
+
[ocp]: https://en.wikipedia.org/wiki/Open-closed_principle
|
278
|
+
[nop]: https://en.wikipedia.org/wiki/Null_object_pattern
|
279
|
+
[gem]: https://rubygems.org
|
280
|
+
[git]: https://github.com/first-try-software/manufacturable
|
281
|
+
[cod]: https://github.com/first-try-software/manufacturable/blob/master/CODE_OF_CONDUCT.md
|
282
|
+
[mit]: https://opensource.org/licenses/MIT
|
283
|
+
[ind]: https://github.com/entelo/industrialist
|
data/Rakefile
ADDED
data/assets/factory.png
ADDED
Binary file
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "manufacturable"
|
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,25 @@
|
|
1
|
+
require 'manufacturable/version'
|
2
|
+
require 'manufacturable/config'
|
3
|
+
require 'manufacturable/factory'
|
4
|
+
require 'manufacturable/item'
|
5
|
+
require 'manufacturable/object_factory'
|
6
|
+
require 'manufacturable/railtie' if defined?(Rails)
|
7
|
+
|
8
|
+
module Manufacturable
|
9
|
+
def self.build(*args)
|
10
|
+
Builder.build(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.registered_types
|
14
|
+
Registrar.registered_types
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.registered_keys(type)
|
18
|
+
Registrar.registered_keys(type)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.config
|
22
|
+
yield(Config)
|
23
|
+
Config.load_paths
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'manufacturable/registrar'
|
2
|
+
|
3
|
+
module Manufacturable
|
4
|
+
class Builder
|
5
|
+
def self.build(*args)
|
6
|
+
self.new(*args).build
|
7
|
+
end
|
8
|
+
|
9
|
+
def build
|
10
|
+
return_first? ? instances.first : instances
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
attr_reader :type, :key, :args
|
16
|
+
|
17
|
+
def initialize(type, key, *args)
|
18
|
+
@type, @key, @args = type, key, args
|
19
|
+
end
|
20
|
+
|
21
|
+
def klasses
|
22
|
+
Registrar.get(type, key)
|
23
|
+
end
|
24
|
+
|
25
|
+
def instances
|
26
|
+
@instances ||= klasses.map { |klass| klass&.new(*args) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def return_first?
|
30
|
+
instances.size < 2
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Manufacturable
|
2
|
+
class Config
|
3
|
+
class << self
|
4
|
+
attr_writer :require_method
|
5
|
+
|
6
|
+
def paths
|
7
|
+
@paths ||= []
|
8
|
+
end
|
9
|
+
|
10
|
+
def load_paths
|
11
|
+
paths.each { |path| require_path(path) }
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def require_method
|
17
|
+
@require_method || :require
|
18
|
+
end
|
19
|
+
|
20
|
+
def require_path(path)
|
21
|
+
Dir["#{path}/**/*.rb"].each { |file| require_file(file) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def require_file(file)
|
25
|
+
Kernel.public_send(require_method, file)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'manufacturable/registrar'
|
2
|
+
|
3
|
+
module Manufacturable
|
4
|
+
module Item
|
5
|
+
def corresponds_to(key, type = self.superclass)
|
6
|
+
key = key == type ? Registrar::ALL_KEY : key
|
7
|
+
Registrar.register(type, key, self)
|
8
|
+
end
|
9
|
+
|
10
|
+
def corresponds_to_all(type = self.superclass)
|
11
|
+
corresponds_to(Registrar::ALL_KEY, type)
|
12
|
+
end
|
13
|
+
|
14
|
+
def default_manufacturable(type = self.superclass)
|
15
|
+
corresponds_to(Registrar::DEFAULT_KEY, type)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Manufacturable
|
2
|
+
class Registrar
|
3
|
+
ALL_KEY = :__all__
|
4
|
+
DEFAULT_KEY = :__default__
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def register(type, key, klass)
|
8
|
+
self.new(registry, type, key).register(klass)
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(type, key)
|
12
|
+
self.new(registry, type, key).get
|
13
|
+
end
|
14
|
+
|
15
|
+
def registered_types
|
16
|
+
registry.keys
|
17
|
+
end
|
18
|
+
|
19
|
+
def registered_keys(type)
|
20
|
+
registry[type].keys
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def registry
|
26
|
+
@registry ||= Hash.new { |h,k| h[k] = Hash.new }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(registry, type, key)
|
31
|
+
@registry, @type, @key = registry, type, key
|
32
|
+
end
|
33
|
+
|
34
|
+
def register(klass)
|
35
|
+
assign_set if set.nil?
|
36
|
+
set.add(klass)
|
37
|
+
end
|
38
|
+
|
39
|
+
def get
|
40
|
+
merged_klasses.empty? ? default_klasses : merged_klasses
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def registry_key
|
46
|
+
@registry_key ||= (@key.respond_to?(:to_sym) && @key.to_sym) || @key
|
47
|
+
end
|
48
|
+
|
49
|
+
def assign_set
|
50
|
+
@registry[@type][registry_key] = Set.new
|
51
|
+
end
|
52
|
+
|
53
|
+
def set
|
54
|
+
@registry[@type][registry_key]
|
55
|
+
end
|
56
|
+
|
57
|
+
def merged_klasses
|
58
|
+
key_klasses.merge(all_klasses)
|
59
|
+
end
|
60
|
+
|
61
|
+
def key_klasses
|
62
|
+
get_for(registry_key)
|
63
|
+
end
|
64
|
+
|
65
|
+
def all_klasses
|
66
|
+
get_for(ALL_KEY)
|
67
|
+
end
|
68
|
+
|
69
|
+
def default_klasses
|
70
|
+
get_for(DEFAULT_KEY)
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_for(key)
|
74
|
+
@registry[@type][key] || Set.new
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative './lib/manufacturable/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "manufacturable"
|
5
|
+
spec.version = Manufacturable::VERSION
|
6
|
+
spec.authors = ["Alan Ridlehoover", "Fito von Zastrow"]
|
7
|
+
spec.email = ["administators@firsttry.software"]
|
8
|
+
|
9
|
+
spec.summary = %q{Manufacturable is a factory that builds self-registering objects.}
|
10
|
+
spec.description = %q{Manufacturable leverages self-registration to move factory setup from case statements, hashes, and configuration files to a simple DSL within the instantiable classes themselves. Giving classes the responsibility of registering themselves with the factory does two things. It allows the factory to be extended without modification. And, it leaves the factory with only one responsibility: building objects.}
|
11
|
+
spec.homepage = "https://github.com/first-try-software/manufacturable"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata = {
|
16
|
+
"bug_tracker_uri" => "https://github.com/first-try-software/manufacturable/issues",
|
17
|
+
"homepage_uri" => spec.homepage,
|
18
|
+
"source_code_uri" => "https://github.com/first-try-software/manufacturable"
|
19
|
+
}
|
20
|
+
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
30
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
spec.add_development_dependency "rspec_junit_formatter", "~>0.4"
|
33
|
+
spec.add_development_dependency "simplecov", "~>0.16"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: manufacturable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alan Ridlehoover
|
8
|
+
- Fito von Zastrow
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2020-06-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '2.0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '2.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '12.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '12.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec_junit_formatter
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0.4'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.4'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: simplecov
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0.16'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0.16'
|
84
|
+
description: 'Manufacturable leverages self-registration to move factory setup from
|
85
|
+
case statements, hashes, and configuration files to a simple DSL within the instantiable
|
86
|
+
classes themselves. Giving classes the responsibility of registering themselves
|
87
|
+
with the factory does two things. It allows the factory to be extended without modification.
|
88
|
+
And, it leaves the factory with only one responsibility: building objects.'
|
89
|
+
email:
|
90
|
+
- administators@firsttry.software
|
91
|
+
executables: []
|
92
|
+
extensions: []
|
93
|
+
extra_rdoc_files: []
|
94
|
+
files:
|
95
|
+
- ".gitignore"
|
96
|
+
- ".rspec"
|
97
|
+
- ".travis.yml"
|
98
|
+
- CODE_OF_CONDUCT.md
|
99
|
+
- Gemfile
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
- Rakefile
|
103
|
+
- assets/factory.png
|
104
|
+
- bin/console
|
105
|
+
- bin/setup
|
106
|
+
- lib/manufacturable.rb
|
107
|
+
- lib/manufacturable/builder.rb
|
108
|
+
- lib/manufacturable/config.rb
|
109
|
+
- lib/manufacturable/factory.rb
|
110
|
+
- lib/manufacturable/item.rb
|
111
|
+
- lib/manufacturable/object_factory.rb
|
112
|
+
- lib/manufacturable/railtie.rb
|
113
|
+
- lib/manufacturable/registrar.rb
|
114
|
+
- lib/manufacturable/version.rb
|
115
|
+
- manufacturable.gemspec
|
116
|
+
homepage: https://github.com/first-try-software/manufacturable
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata:
|
120
|
+
bug_tracker_uri: https://github.com/first-try-software/manufacturable/issues
|
121
|
+
homepage_uri: https://github.com/first-try-software/manufacturable
|
122
|
+
source_code_uri: https://github.com/first-try-software/manufacturable
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 2.3.0
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubygems_version: 3.1.2
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Manufacturable is a factory that builds self-registering objects.
|
142
|
+
test_files: []
|