toritori 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 03d2eef669e357b0d84585f3f04ed0c53ae37674edda3ed875c5983cf0963cb7
4
+ data.tar.gz: 0a4a405fab280b0b3cc124503dbfc6c21ca4736f606e2672ae0327b41e9c7dc7
5
+ SHA512:
6
+ metadata.gz: a172fc3f47049d3dcd804bc6904d6ec34ea9e4beb5400cae0e3eeb1c25051598ba1c0d380b1172b8af2c311e207f6649fc989596389b0a95c9015100f4ee6c32
7
+ data.tar.gz: 7e8b7ac71257f8f108093ba50a5a937c80c22dc17ab0150bc5867e4927a08523e39b073a4f8add5358685684a634b76c24c89220141506d53ed904daa77be878
@@ -0,0 +1,32 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Install dependencies
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ - name: Setup Code Climate test-reporter
19
+ run: |
20
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
21
+ chmod +x ./cc-test-reporter
22
+ ./cc-test-reporter before-build
23
+ - name: Run the default task
24
+ env:
25
+ CC_TEST_REPORTER_ID: 6d12ad52d3aa809a0492eddffd7ca6e43a25982f81dcdcccb3204497b3242bc3
26
+ run: bundle exec rake
27
+ - name: Publish code coverage
28
+ run: |
29
+ export GIT_BRANCH="master"
30
+ pwd
31
+ ls -l coverage
32
+ ./cc-test-reporter after-build -d -t simplecov -r 6d12ad52d3aa809a0492eddffd7ca6e43a25982f81dcdcccb3204497b3242bc3
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .vscode/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,185 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ NewCops: enable
4
+ Exclude:
5
+ - ./spec/spec_helper.rb
6
+
7
+ Layout/LineLength:
8
+ Max: 120
9
+
10
+ Metrics/BlockLength:
11
+ Exclude:
12
+ - ./spec/*
13
+
14
+ Gemspec/DeprecatedAttributeAssignment: # new in 1.30
15
+ Enabled: true
16
+ Gemspec/DevelopmentDependencies: # new in 1.44
17
+ Enabled: true
18
+ Gemspec/RequireMFA: # new in 1.23
19
+ Enabled: false
20
+ Layout/LineContinuationLeadingSpace: # new in 1.31
21
+ Enabled: true
22
+ Layout/LineContinuationSpacing: # new in 1.31
23
+ Enabled: true
24
+ Layout/LineEndStringConcatenationIndentation: # new in 1.18
25
+ Enabled: true
26
+ Layout/SpaceBeforeBrackets: # new in 1.7
27
+ Enabled: true
28
+ Lint/AmbiguousAssignment: # new in 1.7
29
+ Enabled: true
30
+ Lint/AmbiguousOperatorPrecedence: # new in 1.21
31
+ Enabled: true
32
+ Lint/AmbiguousRange: # new in 1.19
33
+ Enabled: true
34
+ Lint/ConstantOverwrittenInRescue: # new in 1.31
35
+ Enabled: true
36
+ Lint/DeprecatedConstants: # new in 1.8
37
+ Enabled: true
38
+ Lint/DuplicateBranch: # new in 1.3
39
+ Enabled: true
40
+ Lint/DuplicateMagicComment: # new in 1.37
41
+ Enabled: true
42
+ Lint/DuplicateMatchPattern: # new in 1.50
43
+ Enabled: true
44
+ Lint/DuplicateRegexpCharacterClassElement: # new in 1.1
45
+ Enabled: true
46
+ Lint/EmptyBlock: # new in 1.1
47
+ Enabled: true
48
+ Lint/EmptyClass: # new in 1.3
49
+ Enabled: true
50
+ Lint/EmptyInPattern: # new in 1.16
51
+ Enabled: true
52
+ Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
53
+ Enabled: true
54
+ Lint/LambdaWithoutLiteralBlock: # new in 1.8
55
+ Enabled: true
56
+ Lint/NoReturnInBeginEndBlocks: # new in 1.2
57
+ Enabled: true
58
+ Lint/NonAtomicFileOperation: # new in 1.31
59
+ Enabled: true
60
+ Lint/NumberedParameterAssignment: # new in 1.9
61
+ Enabled: true
62
+ Lint/OrAssignmentToConstant: # new in 1.9
63
+ Enabled: true
64
+ Lint/RedundantDirGlobSort: # new in 1.8
65
+ Enabled: true
66
+ Lint/RefinementImportMethods: # new in 1.27
67
+ Enabled: true
68
+ Lint/RequireRangeParentheses: # new in 1.32
69
+ Enabled: true
70
+ Lint/RequireRelativeSelfPath: # new in 1.22
71
+ Enabled: true
72
+ Lint/SymbolConversion: # new in 1.9
73
+ Enabled: true
74
+ Lint/ToEnumArguments: # new in 1.1
75
+ Enabled: true
76
+ Lint/TripleQuotes: # new in 1.9
77
+ Enabled: true
78
+ Lint/UnexpectedBlockArity: # new in 1.5
79
+ Enabled: true
80
+ Lint/UnmodifiedReduceAccumulator: # new in 1.1
81
+ Enabled: true
82
+ Lint/UselessRescue: # new in 1.43
83
+ Enabled: true
84
+ Lint/UselessRuby2Keywords: # new in 1.23
85
+ Enabled: true
86
+ Metrics/CollectionLiteralLength: # new in 1.47
87
+ Enabled: true
88
+ Naming/BlockForwarding: # new in 1.24
89
+ Enabled: true
90
+ Security/CompoundHash: # new in 1.28
91
+ Enabled: true
92
+ Security/IoMethods: # new in 1.22
93
+ Enabled: true
94
+ Style/ArgumentsForwarding: # new in 1.1
95
+ Enabled: true
96
+ Style/ArrayIntersect: # new in 1.40
97
+ Enabled: true
98
+ Style/CollectionCompact: # new in 1.2
99
+ Enabled: true
100
+ Style/ComparableClamp: # new in 1.44
101
+ Enabled: true
102
+ Style/ConcatArrayLiterals: # new in 1.41
103
+ Enabled: true
104
+ Style/DataInheritance: # new in 1.49
105
+ Enabled: true
106
+ Style/DirEmpty: # new in 1.48
107
+ Enabled: true
108
+ Style/DocumentDynamicEvalDefinition: # new in 1.1
109
+ Enabled: true
110
+ Style/EmptyHeredoc: # new in 1.32
111
+ Enabled: true
112
+ Style/EndlessMethod: # new in 1.8
113
+ Enabled: true
114
+ Style/EnvHome: # new in 1.29
115
+ Enabled: true
116
+ Style/FetchEnvVar: # new in 1.28
117
+ Enabled: true
118
+ Style/FileEmpty: # new in 1.48
119
+ Enabled: true
120
+ Style/FileRead: # new in 1.24
121
+ Enabled: true
122
+ Style/FileWrite: # new in 1.24
123
+ Enabled: true
124
+ Style/HashConversion: # new in 1.10
125
+ Enabled: true
126
+ Style/HashExcept: # new in 1.7
127
+ Enabled: true
128
+ Style/IfWithBooleanLiteralBranches: # new in 1.9
129
+ Enabled: true
130
+ Style/InPatternThen: # new in 1.16
131
+ Enabled: true
132
+ Style/MagicCommentFormat: # new in 1.35
133
+ Enabled: true
134
+ Style/MapCompactWithConditionalBlock: # new in 1.30
135
+ Enabled: true
136
+ Style/MapToHash: # new in 1.24
137
+ Enabled: true
138
+ Style/MapToSet: # new in 1.42
139
+ Enabled: true
140
+ Style/MinMaxComparison: # new in 1.42
141
+ Enabled: true
142
+ Style/MultilineInPatternThen: # new in 1.16
143
+ Enabled: true
144
+ Style/NegatedIfElseCondition: # new in 1.2
145
+ Enabled: true
146
+ Style/NestedFileDirname: # new in 1.26
147
+ Enabled: true
148
+ Style/NilLambda: # new in 1.3
149
+ Enabled: true
150
+ Style/NumberedParameters: # new in 1.22
151
+ Enabled: true
152
+ Style/NumberedParametersLimit: # new in 1.22
153
+ Enabled: true
154
+ Style/ObjectThen: # new in 1.28
155
+ Enabled: true
156
+ Style/OpenStructUse: # new in 1.23
157
+ Enabled: true
158
+ Style/OperatorMethodCall: # new in 1.37
159
+ Enabled: true
160
+ Style/QuotedSymbols: # new in 1.16
161
+ Enabled: true
162
+ Style/RedundantArgument: # new in 1.4
163
+ Enabled: true
164
+ Style/RedundantConstantBase: # new in 1.40
165
+ Enabled: true
166
+ Style/RedundantDoubleSplatHashBraces: # new in 1.41
167
+ Enabled: true
168
+ Style/RedundantEach: # new in 1.38
169
+ Enabled: true
170
+ Style/RedundantHeredocDelimiterQuotes: # new in 1.45
171
+ Enabled: true
172
+ Style/RedundantInitialize: # new in 1.27
173
+ Enabled: true
174
+ Style/RedundantLineContinuation: # new in 1.49
175
+ Enabled: true
176
+ Style/RedundantSelfAssignmentBranch: # new in 1.19
177
+ Enabled: true
178
+ Style/RedundantStringEscape: # new in 1.37
179
+ Enabled: true
180
+ Style/SelectByRegexp: # new in 1.22
181
+ Enabled: true
182
+ Style/StringChars: # new in 1.12
183
+ Enabled: true
184
+ Style/SwapValues: # new in 1.1
185
+ Enabled: true
data/CHANGELOG.md ADDED
File without changes
@@ -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 Andrii Baran. 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/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in toritori.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
9
+ gem 'rake', '~> 13.0'
10
+ gem 'rspec', '~> 3.0'
11
+ gem 'rspec_vars_helper', '~> 0.1'
12
+ gem 'rubocop'
13
+ gem 'simplecov', '0.17'
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ toritori (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.0)
12
+ docile (1.4.0)
13
+ json (2.6.3)
14
+ method_source (1.0.0)
15
+ parallel (1.23.0)
16
+ parser (3.2.2.1)
17
+ ast (~> 2.4.1)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.8.0)
24
+ rexml (3.2.5)
25
+ rspec (3.11.0)
26
+ rspec-core (~> 3.11.0)
27
+ rspec-expectations (~> 3.11.0)
28
+ rspec-mocks (~> 3.11.0)
29
+ rspec-core (3.11.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-expectations (3.11.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-mocks (3.11.1)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.11.0)
37
+ rspec-support (3.11.1)
38
+ rspec_vars_helper (0.1.0)
39
+ rspec (>= 2.4)
40
+ rubocop (1.51.0)
41
+ json (~> 2.3)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.2.0.0)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml (>= 3.2.5, < 4.0)
47
+ rubocop-ast (>= 1.28.0, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 2.4.0, < 3.0)
50
+ rubocop-ast (1.28.1)
51
+ parser (>= 3.2.1.0)
52
+ ruby-progressbar (1.13.0)
53
+ simplecov (0.17.0)
54
+ docile (~> 1.1)
55
+ json (>= 1.8, < 3)
56
+ simplecov-html (~> 0.10.0)
57
+ simplecov-html (0.10.2)
58
+ unicode-display_width (2.4.2)
59
+
60
+ PLATFORMS
61
+ arm64-darwin-21
62
+ x86_64-linux
63
+
64
+ DEPENDENCIES
65
+ pry
66
+ rake (~> 13.0)
67
+ rspec (~> 3.0)
68
+ rspec_vars_helper (~> 0.1)
69
+ rubocop
70
+ simplecov (= 0.17)
71
+ toritori!
72
+
73
+ BUNDLED WITH
74
+ 2.3.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TODO: Write your name
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,158 @@
1
+ # Toritori
2
+
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/4e5138d5018b81671692/maintainability)](https://codeclimate.com/github/andriy-baran/toritori/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/4e5138d5018b81671692/test_coverage)](https://codeclimate.com/github/andriy-baran/toritori/test_coverage)
5
+
6
+ Simple tool to work with Abstract Factories.
7
+ It provides the DSL for defining a set factories and produce objects.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'toritori'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install toritori
24
+
25
+ ## Usage
26
+
27
+ ### Basics
28
+ Add the module to the class
29
+ ```ruby
30
+ require 'toritori'
31
+
32
+ class MyAbstractFactory
33
+ include Toritori
34
+ end
35
+ ```
36
+ It will provide an ablility to declare and refer individual factories
37
+ ```ruby
38
+ # Declaration
39
+ MyAbstractFactory.factory(:chair)
40
+ MyAbstractFactory.factory(:table)
41
+ # Access all factories
42
+ MyAbstractFactory.factories # => { chair: #<Toritori::Factory @name: :chair>, table: #<Toritori::Factory @name: :table> }
43
+ # Access individual factory
44
+ MyAbstractFactory.chair_factory # => #<Toritori::Factory @name: :chair>
45
+ MyAbstractFactory.table_factory # => #<Toritori::Factory @name: :table>
46
+ ```
47
+ Creating objects is also easy
48
+ ```ruby
49
+ MyAbstractFactory.table_factory.create(<attrs>) # => #<Class>
50
+ ```
51
+ To provide a specific class for factory to create instances
52
+ ```ruby
53
+ Sofa = Struct.new(:width)
54
+
55
+ class MyAbstractFactory
56
+ include Toritori
57
+
58
+ factory :sofa, produces: Sofa
59
+ end
60
+
61
+ MyAbstractFactory.sofa_factory # =>
62
+ MyAbstractFactory.sofa_factory.create(2300) # => #<Sofa @width=2300>
63
+ ```
64
+ The library defaults is to use `new` method for instantiation and bypass parameters from `create` method. But if you need to customize this behaviour
65
+ ```ruby
66
+ class MyAbstractFactory
67
+ include Toritori
68
+
69
+ factory :file, produces: File do |file_name|
70
+ # Every method is called on File
71
+ open(file_name, 'r')
72
+ end
73
+ end
74
+
75
+ MyAbstractFactory.file_factory.create('/dev/null') # => #<File @path='/dev/null'>
76
+ ```
77
+ ### Subclassing
78
+ For example:
79
+ ```ruby
80
+ class ModernFactory < MyAbstractFactory
81
+ factories # => { chair: #<Toritori::Factory @name: :chair>,
82
+ # table: #<Toritori::Factory @name: :table>,
83
+ # sofa: #<Toritori::Factory @name: :sofa, @base_class=Sofa> }
84
+ end
85
+ ```
86
+ If we need to add a wifi option to sofa
87
+ ```ruby
88
+ class ModernFactory < MyAbstractFactory
89
+ sofa_factory.subclass do
90
+ def add_wifi
91
+ @wifi = true
92
+ end
93
+
94
+ attr_reader :wifi
95
+ end
96
+ end
97
+
98
+ modern_sofa = ModernFactory.sofa_factory.create(2500)
99
+ modern_sofa.wifi # => nil
100
+ modern_sofa.add_wifi
101
+ modern_sofa.wifi # => true
102
+ ```
103
+ If we need to add wifi option to initializer
104
+ ```ruby
105
+ class ModernFactory < MyAbstractFactory
106
+ # Update initialize method
107
+ chair_factory.subclass do
108
+ def initialize(width, wifi)
109
+ super(width)
110
+ @wifi = wifi
111
+ end
112
+
113
+ attr_reader :wifi
114
+ end
115
+
116
+ # Notify factory about new way to create objects
117
+ chair_factory.subclass.init do |width, wifi:|
118
+ new(width, wifi)
119
+ end
120
+ end
121
+
122
+ modern_chair = ModernFactory.chair_factory.create(2500, wifi: false)
123
+ modern_chair.wifi # => false
124
+ ```
125
+ The subclass (`ModernFactory`) will gen a copy of `factories` so you can customize sublasses without side effects on a base class (`MyAbstractFactory`).
126
+
127
+ Sometimes when subclass definition is big it is better to put it into a separate file. To make the library to use that sub-class:
128
+ ```ruby
129
+ class ModernTable < MyAbstractFactory.table_factory.base_class
130
+ # ... omitied ...
131
+ end
132
+
133
+ class ModernFactory < MyAbstractFactory
134
+ # Update initialize method
135
+ table_factory.subclass.base_class ModernTable
136
+
137
+ table_factory.create # => #<ModernTable>
138
+ end
139
+ ```
140
+ The sub-class should be a child of a base class we specified in factory of the parent abstract factory, otherwise you'll get `Toritori::SubclassError`. We recommend to have base classes (`produces:` option) for parent abstract factory defined explicitly to have an ability to refer them in sub-class files.
141
+
142
+ ## Development
143
+
144
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
145
+
146
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
147
+
148
+ ## Contributing
149
+
150
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andriy-baran/toritori. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/andriy-baran/toritori/blob/master/CODE_OF_CONDUCT.md).
151
+
152
+ ## License
153
+
154
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
155
+
156
+ ## Code of Conduct
157
+
158
+ Everyone interacting in the Toritori project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/andriy-baran/toritori/blob/master/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]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'toritori'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Toritori
4
+ class Factory
5
+ # Utility class that evaluates initalization procs
6
+ class Instantiator < BasicObject
7
+ def initialize(subclass)
8
+ @subclass = subclass
9
+ @type = subclass.base_class
10
+ define_singleton_method(:__create__, &subclass.init)
11
+ end
12
+
13
+ def method_missing(method, *args, **kwargs, &block)
14
+ return super unless @type.respond_to?(method)
15
+
16
+ @type.public_send(method, *args, **kwargs, &block)
17
+ end
18
+
19
+ def respond_to_missing?(method, include_private = false)
20
+ @type.respond_to?(method, include_private) || super
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Toritori
4
+ class Factory
5
+ # Utility class that stores initalization procs
6
+ class Subclass < BasicObject
7
+ def initialize(base_class, init)
8
+ @init = init || ::Toritori.default_init
9
+ @base_class = base_class
10
+ end
11
+
12
+ def init(&block)
13
+ return @init unless block
14
+
15
+ @init = block
16
+ end
17
+
18
+ def base_class(subclass_const = nil)
19
+ return @base_class unless subclass_const
20
+
21
+ ::Kernel.raise NotAClassError unless subclass_const.is_a?(::Class)
22
+ unless subclass_const <= @base_class
23
+ ::Kernel.raise(SubclassError, "must be a subclass of #{@base_class.inspect}")
24
+ end
25
+
26
+ @base_class = subclass_const
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Toritori
4
+ # Generates module that adds support for objects creation
5
+ class Factory
6
+ attr_reader :name
7
+
8
+ def cached_instantiator(subclass)
9
+ id = [subclass.base_class.object_id, subclass.init.object_id].join('_')
10
+ @cache ||= ::Hash.new do |h, key|
11
+ h[key] = Instantiator.new(subclass)
12
+ end
13
+ @cache[id]
14
+ end
15
+
16
+ def copy
17
+ self.class.new(name, base_class: base_class, &subclass.init)
18
+ end
19
+
20
+ def initialize(name, base_class: nil, &block)
21
+ @name = name
22
+ @subclass = Subclass.new(base_class, block)
23
+ end
24
+
25
+ def subclass(&block)
26
+ return @subclass unless block
27
+
28
+ @subclass = Subclass.new(Class.new(base_class, &block), @subclass.init)
29
+ end
30
+
31
+ def create(*args, **kwargs, &block)
32
+ cached_instantiator(@subclass).__create__(*args, **kwargs, &block)
33
+ end
34
+
35
+ def base_class
36
+ @subclass.base_class
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Toritori
4
+ VERSION = '0.1.0'
5
+ end
data/lib/toritori.rb ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'toritori/factory/instantiator'
4
+ require 'toritori/factory/subclass'
5
+ require 'toritori/factory'
6
+ require 'toritori/version'
7
+
8
+ # Main namespace
9
+ module Toritori
10
+ class Error < StandardError; end
11
+ class SubclassError < Error; end
12
+ class NotAClassError < Error; end
13
+
14
+ # Defines high level interface
15
+ module ClassMethods
16
+ def factories
17
+ @factories ||= {}
18
+ end
19
+
20
+ def factories=(other)
21
+ @factories = other
22
+ end
23
+
24
+ def factory(name, produces: Class.new, &block)
25
+ factories[name] = Toritori::Factory.new(name, base_class: produces, &block)
26
+ define_singleton_method(:"#{name}_factory") { factories[name] }
27
+ end
28
+
29
+ def inherited(subclass)
30
+ super
31
+ subclass.factories = factories.transform_values(&:copy)
32
+ end
33
+ end
34
+
35
+ def self.default_init
36
+ @default_init ||= ->(*args, **kwargs, &block) { new(*args, **kwargs, &block) }
37
+ end
38
+
39
+ def self.included(receiver)
40
+ receiver.extend ClassMethods
41
+ end
42
+ end
data/toritori.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/toritori/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'toritori'
7
+ spec.version = Toritori::VERSION
8
+ spec.authors = ['Andrii Baran']
9
+ spec.email = ['andriy.baran.v@gmail.com']
10
+
11
+ spec.summary = 'DSL for factories'
12
+ spec.description = 'Create factories with DSL'
13
+ spec.homepage = 'https://github.com/andriy-baran/toritori'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+ spec.metadata['changelog_uri'] = 'https://github.com/andriy-baran/Toritori/CHANGELOG.md'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toritori
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrii Baran
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Create factories with DSL
14
+ email:
15
+ - andriy.baran.v@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - CHANGELOG.md
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/toritori.rb
34
+ - lib/toritori/factory.rb
35
+ - lib/toritori/factory/instantiator.rb
36
+ - lib/toritori/factory/subclass.rb
37
+ - lib/toritori/version.rb
38
+ - toritori.gemspec
39
+ homepage: https://github.com/andriy-baran/toritori
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ allowed_push_host: https://rubygems.org
44
+ homepage_uri: https://github.com/andriy-baran/toritori
45
+ source_code_uri: https://github.com/andriy-baran/toritori
46
+ changelog_uri: https://github.com/andriy-baran/Toritori/CHANGELOG.md
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.5.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.3.26
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: DSL for factories
66
+ test_files: []