nina 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: c948dd7e2dadd58475f4153f5dd7162912d41706e4fbec08118470d69f8783db
4
+ data.tar.gz: 9ad7e8f6dd835220d21d2afe5120bfcea1cf5dcd13241f531f14d3fcb448edaf
5
+ SHA512:
6
+ metadata.gz: b24e8b48fa8744df3395b5f936cacd723c97285d5acc2b9f0e5d5edb5fac6e86caad86a8ef20968bd53210f37f663d56520c2a2becaeda38d92b95162aa2112b
7
+ data.tar.gz: 64eac812b99e18afa87ce146d0eb2cf77ad08efba781303b397b70d6edb320fa66856879635af1d98ae6cd86d9970790e1b419b0d08c190e330336ea0f27cb24
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.6
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
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-18
4
+
5
+ - 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 andrii.baran@newfireglobal.com. 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 nina.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,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nina (0.1.0)
5
+ toritori (= 0.1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.5.0)
13
+ docile (1.4.0)
14
+ json (2.6.3)
15
+ method_source (1.0.0)
16
+ parallel (1.23.0)
17
+ parser (3.2.2.3)
18
+ ast (~> 2.4.1)
19
+ racc
20
+ pry (0.14.2)
21
+ coderay (~> 1.1)
22
+ method_source (~> 1.0)
23
+ racc (1.7.1)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.8.1)
27
+ rexml (3.2.5)
28
+ rspec (3.12.0)
29
+ rspec-core (~> 3.12.0)
30
+ rspec-expectations (~> 3.12.0)
31
+ rspec-mocks (~> 3.12.0)
32
+ rspec-core (3.12.2)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-expectations (3.12.3)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-mocks (3.12.5)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-support (3.12.0)
41
+ rspec_vars_helper (0.1.0)
42
+ rspec (>= 2.4)
43
+ rubocop (1.52.1)
44
+ json (~> 2.3)
45
+ parallel (~> 1.10)
46
+ parser (>= 3.2.2.3)
47
+ rainbow (>= 2.2.2, < 4.0)
48
+ regexp_parser (>= 1.8, < 3.0)
49
+ rexml (>= 3.2.5, < 4.0)
50
+ rubocop-ast (>= 1.28.0, < 2.0)
51
+ ruby-progressbar (~> 1.7)
52
+ unicode-display_width (>= 2.4.0, < 3.0)
53
+ rubocop-ast (1.29.0)
54
+ parser (>= 3.2.1.0)
55
+ ruby-progressbar (1.13.0)
56
+ simplecov (0.17.0)
57
+ docile (~> 1.1)
58
+ json (>= 1.8, < 3)
59
+ simplecov-html (~> 0.10.0)
60
+ simplecov-html (0.10.2)
61
+ toritori (0.1.0)
62
+ unicode-display_width (2.4.2)
63
+
64
+ PLATFORMS
65
+ arm64-darwin-21
66
+ x86_64-linux
67
+
68
+ DEPENDENCIES
69
+ nina!
70
+ pry
71
+ rake (~> 13.0)
72
+ rspec (~> 3.0)
73
+ rspec_vars_helper (~> 0.1)
74
+ rubocop
75
+ simplecov (= 0.17)
76
+
77
+ BUNDLED WITH
78
+ 2.4.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Andrii Baran
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,127 @@
1
+ # Nina
2
+
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/435ee6e0ae846e9deb88/maintainability)](https://codeclimate.com/github/andriy-baran/nina/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/435ee6e0ae846e9deb88/test_coverage)](https://codeclimate.com/github/andriy-baran/nina/test_coverage)
5
+
6
+ DSL for simplifying complex objects compositions. Also it reduce biolerplate code when you need to create complex OOD compositions. It's based on https://github.com/andriy-baran/toritori so please check it first
7
+
8
+ ## Installation
9
+
10
+ Install the gem and add to the application's Gemfile by executing:
11
+
12
+ $ bundle add nina
13
+
14
+ If bundler is not being used to manage dependencies, install the gem by executing:
15
+
16
+ $ gem install nina
17
+
18
+ ## Usage
19
+
20
+ Let's define two builders: main and secondary
21
+
22
+ ```ruby
23
+ Params = Class.new
24
+ Query = Class.new
25
+ Command = Class.new
26
+
27
+ A = Struct.new(:a)
28
+ B = Struct.new(:b)
29
+ C = Struct.new(:c)
30
+
31
+ class Flow
32
+ include Nina
33
+
34
+ builder :main do
35
+ factory :params, produces: Params
36
+ factory :query, produces: Query
37
+ factory :command, produces: Command
38
+ end
39
+
40
+ builder :secondary do
41
+ factory :params, produces: A
42
+ factory :query, produces: B
43
+ factory :command, produces: C
44
+ end
45
+ end
46
+ ```
47
+ Each builder has three factories: params, query, and command. Please check https://github.com/andriy-baran/toritori for related documentation.
48
+
49
+ With this setup we are able to compose objects in two different ways. Taking the definition block as an ordered list we can traverse it `top->bottom` or `bottom->top` connecting objects at each step
50
+
51
+ Wrapping `top->bottom`
52
+ ```mermaid
53
+ graph TD;
54
+ query-->params;
55
+ command-->query;
56
+ ```
57
+ Nesting `bottom->top`
58
+ ```mermaid
59
+ graph TD;
60
+ query-->command;
61
+ params-->query;
62
+ ```
63
+ Lets explore what we have as a result
64
+ ```ruby
65
+ # Wrapping strategy
66
+ builder = abstract_factory.main_builder
67
+ instance = builder.wrap
68
+ instance # => #<Command>
69
+ instance.query # => #<Query>
70
+ instance.query.params # => #<Params>
71
+
72
+ # Nesting strategy
73
+ builder = abstract_factory.secondary_builder
74
+ instance = builder.nest
75
+ instance # => #<A>
76
+ instance.query # => #<B>
77
+ instance.query.command # => #<C>
78
+ ```
79
+ We may apply delegation techique from OOD to hide deeper layers of resulted object
80
+ ```ruby
81
+ builder = abstract_factory.secondary_builder
82
+ instance = builder.nest(delegate: true)
83
+ instance.a # => nil
84
+ instance.b # => nil
85
+ instance.c # => nil
86
+ ```
87
+ If you need provide an initalization parameters for the objects
88
+ ```ruby
89
+ instance = builder.wrap(delegate: true) do |b|
90
+ b.params(1)
91
+ b.query(2)
92
+ b.command(3)
93
+ end
94
+ instance.a # => 1
95
+ instance.b # => 2
96
+ instance.c # => 3
97
+ instance.query.c # => 3
98
+ ```
99
+ To do something between stages (after creation of object)
100
+ ```ruby
101
+ instance = builder.wrap(delegate: true) do |b|
102
+ b.params { _1.a = 'a' }
103
+ b.query { _1.b = 'b' }
104
+ b.command { _1.c = 'c' }
105
+ end
106
+ instance.a # => 'a'
107
+ instance.b # => 'b'
108
+ instance.c # => 'c'
109
+ ```
110
+
111
+ ## Development
112
+
113
+ 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.
114
+
115
+ 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).
116
+
117
+ ## Contributing
118
+
119
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nina. 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/[USERNAME]/nina/blob/master/CODE_OF_CONDUCT.md).
120
+
121
+ ## License
122
+
123
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
124
+
125
+ ## Code of Conduct
126
+
127
+ Everyone interacting in the Nina project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/nina/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]
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nina
4
+ # Generates module that adds support for objects creation
5
+ class Assembler
6
+ NOOP_PROC = proc {}
7
+
8
+ # Adds ability to delegeate methods via method_missing
9
+ module MethodMissingDelegation
10
+ def method_missing(name, *attrs, &block)
11
+ return super unless methods.detect { |m| m == :__predecessor }
12
+
13
+ public_send(__predecessor).public_send(name, *attrs, &block)
14
+ end
15
+
16
+ def respond_to_missing?(method_name, _include_private = false)
17
+ return super unless methods.detect { |m| m == :__predecessor }
18
+
19
+ public_send(__predecessor).respond_to?(method_name)
20
+ end
21
+ end
22
+
23
+ def self.def_accessor(accessor, on:, to:, delegate: false)
24
+ on.define_singleton_method(:__predecessor) { accessor }
25
+ on.define_singleton_method(accessor) { to }
26
+ on.extend(MethodMissingDelegation) if delegate
27
+ end
28
+
29
+ def initialize(abstract_factory)
30
+ @abstract_factory = abstract_factory
31
+ end
32
+
33
+ def inject(build_order, initialization = {}, callbacks: nil, delegate: false)
34
+ build_order.each.with_index(-1).inject(nil) do |prev, (name, idx)|
35
+ object = create_object(name, initialization)
36
+ self.class.def_accessor(build_order[idx], on: object, to: prev, delegate: delegate) if prev
37
+ callbacks.to_h.fetch(name, NOOP_PROC)[object] if callbacks
38
+ object
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def create_object(name, initialization = {})
45
+ return @abstract_factory.send("#{name}_factory").create if initialization[name].nil?
46
+
47
+ args, kwargs, block = initialization[name]
48
+ @abstract_factory.send("#{name}_factory").create(*args, **kwargs, &block)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This should be a kind of factory that creates complex objects
4
+ # from simple ones. It should use torirori to create objects.
5
+ # It also enriches objects with some methods that make them more
6
+ # like linked lists.
7
+ module Nina
8
+ # Generates module that adds support for objects creation
9
+ class Builder
10
+ attr_reader :name, :abstract_factory, :def_block
11
+ attr_accessor :callbacks
12
+
13
+ # A way to call methods from initalization proc on base_class
14
+ class Initialization < BasicObject
15
+ def initialize(list)
16
+ @list = list
17
+ @atts = {}
18
+ end
19
+
20
+ def method_missing(method, *args, **kwargs, &block)
21
+ return super unless @list.include?(method)
22
+
23
+ @atts[method] = [args, kwargs, block]
24
+ end
25
+
26
+ def respond_to_missing?(method, include_private = false)
27
+ @list.include?(method) || super
28
+ end
29
+
30
+ def to_h
31
+ @atts
32
+ end
33
+ end
34
+
35
+ # Utility to get user defined callbacks
36
+ class Callbacks < Initialization
37
+ def method_missing(method, *args, **kwargs, &block)
38
+ return super unless @list.include?(method)
39
+
40
+ @atts[method] unless block
41
+ @atts[method] = block
42
+ end
43
+ end
44
+
45
+ # Definaes support methods and variables for concrete builder
46
+ module ClassMethods
47
+ def build_order_list
48
+ @build_order_list ||= []
49
+ end
50
+
51
+ def build_order_list=(other)
52
+ @build_order_list = other.dup.freeze
53
+ end
54
+
55
+ def inherited(subclass)
56
+ super
57
+ subclass.build_order_list = build_order_list.dup.freeze
58
+ end
59
+
60
+ def factory(name, *args, **kwargs, &block)
61
+ build_order_list << name
62
+ super
63
+ define_singleton_method(name) do |klass = nil, &definition|
64
+ factories[__method__].subclass.base_class(klass) if klass
65
+ factories[__method__].subclass(&definition) if definition
66
+ end
67
+ end
68
+ end
69
+
70
+ def copy
71
+ self.class.new(name, abstract_factory: abstract_factory)
72
+ end
73
+
74
+ def with_callbacks(&block)
75
+ yield c = Callbacks.new(abstract_factory.factories.keys) if block
76
+
77
+ self.class.new(name, abstract_factory: abstract_factory).tap { _1.callbacks = c }
78
+ end
79
+
80
+ def initialize(name, abstract_factory: nil, &def_block)
81
+ @name = name
82
+ @def_block = def_block
83
+ @abstract_factory = abstract_factory.include(Toritori).extend(ClassMethods)
84
+ @abstract_factory.class_eval(&def_block) if def_block
85
+ @abstract_factory.build_order_list.freeze
86
+ @initialization = Initialization.new(@abstract_factory.factories.keys)
87
+ @assembler = Assembler.new(@abstract_factory)
88
+ end
89
+
90
+ def wrap(delegate: false, &block)
91
+ yield @initialization if block
92
+
93
+ @assembler.inject(
94
+ @abstract_factory.build_order_list,
95
+ @initialization.to_h,
96
+ callbacks: callbacks,
97
+ delegate: delegate
98
+ )
99
+ end
100
+
101
+ def nest(delegate: false, &block)
102
+ yield @initialization if block
103
+
104
+ @assembler.inject(
105
+ @abstract_factory.build_order_list.reverse,
106
+ @initialization.to_h,
107
+ callbacks: callbacks,
108
+ delegate: delegate
109
+ )
110
+ end
111
+
112
+ def subclass(&def_block)
113
+ return unless def_block
114
+
115
+ @abstract_factory = Class.new(abstract_factory)
116
+ @abstract_factory.class_eval(&def_block)
117
+ @abstract_factory.build_order_list.freeze
118
+ @initialization = Initialization.new(@abstract_factory.factories.keys)
119
+ @assembler = Assembler.new(@abstract_factory)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nina
4
+ VERSION = '0.1.0'
5
+ end
data/lib/nina.rb ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'toritori'
4
+ require 'nina/assembler'
5
+ require 'nina/builder'
6
+
7
+ require_relative 'nina/version'
8
+
9
+ # Module that provides DSL for builders
10
+ module Nina
11
+ class Error < StandardError; end
12
+
13
+ # Definaes support methods and variables
14
+ module ClassMethods
15
+ def builders
16
+ @builders ||= {}
17
+ end
18
+
19
+ def builders=(other)
20
+ @builders = other
21
+ end
22
+
23
+ def builder(name, &block)
24
+ builders[name] = Nina::Builder.new(name, abstract_factory: Class.new, &block)
25
+ define_singleton_method(:"#{name}_builder") { builders[name] }
26
+ end
27
+
28
+ def inherited(subclass)
29
+ super
30
+ subclass.builders = builders.transform_values(&:copy)
31
+ end
32
+ end
33
+
34
+ def self.included(receiver)
35
+ receiver.extend ClassMethods
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nina
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
+ - !ruby/object:Gem::Dependency
14
+ name: toritori
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ description: Reduce biolerplate code when you need to create complex OOD composition
28
+ email:
29
+ - andriy.baran.v@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/nina.rb
44
+ - lib/nina/assembler.rb
45
+ - lib/nina/builder.rb
46
+ - lib/nina/version.rb
47
+ homepage: https://github.com/andriy-baran/nina
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ allowed_push_host: https://rubygems.org
52
+ homepage_uri: https://github.com/andriy-baran/nina
53
+ source_code_uri: https://github.com/andriy-baran/nina
54
+ changelog_uri: https://github.com/andriy-baran/Toritori/CHANGELOG.md
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.6.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.3.26
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: DSL for simplifying complex objects compositions
74
+ test_files: []