hospodar 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +9 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +80 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +12 -0
  8. data/Gemfile.lock +86 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +49 -0
  11. data/Rakefile +12 -0
  12. data/bin/console +15 -0
  13. data/bin/setup +8 -0
  14. data/hospodar.gemspec +47 -0
  15. data/lib/hospodar/builder/assembler.rb +96 -0
  16. data/lib/hospodar/builder/error.rb +15 -0
  17. data/lib/hospodar/builder/exeptional.rb +20 -0
  18. data/lib/hospodar/builder/flatten.rb +36 -0
  19. data/lib/hospodar/builder/helpers.rb +21 -0
  20. data/lib/hospodar/builder/id.rb +15 -0
  21. data/lib/hospodar/builder/instantiation_error.rb +24 -0
  22. data/lib/hospodar/builder/nested.rb +18 -0
  23. data/lib/hospodar/builder/proxy.rb +34 -0
  24. data/lib/hospodar/builder/strategies/enumerate.rb +26 -0
  25. data/lib/hospodar/builder/strategies/execute.rb +76 -0
  26. data/lib/hospodar/builder/strategies/init.rb +55 -0
  27. data/lib/hospodar/builder/strategies/inject.rb +35 -0
  28. data/lib/hospodar/builder/strategies/link.rb +31 -0
  29. data/lib/hospodar/builder/strategies/mount.rb +56 -0
  30. data/lib/hospodar/builder/strategies/translate.rb +60 -0
  31. data/lib/hospodar/builder/wrapped.rb +40 -0
  32. data/lib/hospodar/builder.rb +113 -0
  33. data/lib/hospodar/dsl.rb +84 -0
  34. data/lib/hospodar/factories.rb +28 -0
  35. data/lib/hospodar/group_definition.rb +22 -0
  36. data/lib/hospodar/inheritance_helpers.rb +36 -0
  37. data/lib/hospodar/module_builder.rb +88 -0
  38. data/lib/hospodar/registry.rb +34 -0
  39. data/lib/hospodar/subclassing_helpers.rb +29 -0
  40. data/lib/hospodar/version.rb +5 -0
  41. data/lib/hospodar.rb +54 -0
  42. metadata +255 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 72c9a8eeaf5ea09a521f226d8e70b0105a816aa0396eabfb4faa6f26f23993e4
4
+ data.tar.gz: dba323ec9d842255ffb3aeb571587f1929cc9c3d231c0f451c2c836ff58cfe4c
5
+ SHA512:
6
+ metadata.gz: dbdd91ddc40fcc1e4528d091c50f7f1c5700e89a8e6bc0998fce3d2677d7003bbd0319534ca4bcc6458c8acab4f413d9455d89eaf30466839edefa6ea3eb36ed
7
+ data.tar.gz: 0f2a8b6a1720cfaec71d985e118d6e70e6f94c96ec32753cc02b6761a12f3eae38beb5f459fbb4c443eaae665bf4a38c5af7efd66dfca31ef68f3262c5137de3
data/.codeclimate.yml ADDED
@@ -0,0 +1,9 @@
1
+ version: "2"
2
+ plugins:
3
+ rubocop:
4
+ enabled: true
5
+ channel: rubocop-0-67
6
+ config:
7
+ file: .rubocop.yml
8
+ exclude-patterns:
9
+ - spec/**/*.rb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,80 @@
1
+ require: rubocop-performance
2
+ AllCops:
3
+ NewCops: enable
4
+ Exclude:
5
+ - hospodar.gemspec
6
+ - !ruby/regexp /(db|bin|script|config|vendor)\/.*/
7
+ Gemspec/DateAssignment: # (new in 1.10)
8
+ Enabled: true
9
+ Layout/SpaceBeforeBrackets: # (new in 1.7)
10
+ Enabled: true
11
+ Lint/AmbiguousAssignment: # (new in 1.7)
12
+ Enabled: true
13
+ Lint/DeprecatedConstants: # (new in 1.8)
14
+ Enabled: true
15
+ Lint/DuplicateBranch: # (new in 1.3)
16
+ Enabled: true
17
+ Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1)
18
+ Enabled: true
19
+ Lint/EmptyBlock: # (new in 1.1)
20
+ Enabled: true
21
+ Lint/EmptyClass: # (new in 1.3)
22
+ Enabled: true
23
+ Lint/LambdaWithoutLiteralBlock: # (new in 1.8)
24
+ Enabled: true
25
+ Lint/NoReturnInBeginEndBlocks: # (new in 1.2)
26
+ Enabled: true
27
+ Lint/NumberedParameterAssignment: # (new in 1.9)
28
+ Enabled: true
29
+ Lint/OrAssignmentToConstant: # (new in 1.9)
30
+ Enabled: true
31
+ Lint/RedundantDirGlobSort: # (new in 1.8)
32
+ Enabled: true
33
+ Lint/SymbolConversion: # (new in 1.9)
34
+ Enabled: true
35
+ Lint/ToEnumArguments: # (new in 1.1)
36
+ Enabled: true
37
+ Lint/TripleQuotes: # (new in 1.9)
38
+ Enabled: true
39
+ Lint/UnexpectedBlockArity: # (new in 1.5)
40
+ Enabled: true
41
+ Lint/UnmodifiedReduceAccumulator: # (new in 1.1)
42
+ Enabled: true
43
+ Style/ArgumentsForwarding: # (new in 1.1)
44
+ Enabled: true
45
+ Style/CollectionCompact: # (new in 1.2)
46
+ Enabled: true
47
+ Style/DocumentDynamicEvalDefinition: # (new in 1.1)
48
+ Enabled: true
49
+ Style/EndlessMethod: # (new in 1.8)
50
+ Enabled: true
51
+ Style/HashConversion: # (new in 1.10)
52
+ Enabled: true
53
+ Style/HashExcept: # (new in 1.7)
54
+ Enabled: true
55
+ Style/IfWithBooleanLiteralBranches: # (new in 1.9)
56
+ Enabled: true
57
+ Style/NegatedIfElseCondition: # (new in 1.2)
58
+ Enabled: true
59
+ Style/NilLambda: # (new in 1.3)
60
+ Enabled: true
61
+ Style/RedundantArgument: # (new in 1.4)
62
+ Enabled: true
63
+ Style/StringChars: # (new in 1.12)
64
+ Enabled: true
65
+ Style/SwapValues: # (new in 1.1)
66
+ Enabled: true
67
+ Metrics/BlockLength:
68
+ Exclude:
69
+ - spec/**/*.rb
70
+ Naming/MethodParameterName:
71
+ Exclude:
72
+ - spec/**/*.rb
73
+ Metrics/MethodLength:
74
+ Max: 15
75
+ IgnoredMethods:
76
+ - define_component_adding_method
77
+ Metrics/AbcSize:
78
+ Max: 20
79
+ Metrics/ParameterLists:
80
+ Max: 6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-11-04
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. 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in hospodar.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.12'
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hospodar (1.0.0)
5
+ dry-inflector (~> 0.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ byebug (11.1.3)
12
+ coderay (1.1.3)
13
+ diff-lcs (1.4.4)
14
+ docile (1.3.5)
15
+ dry-inflector (0.2.0)
16
+ json (2.6.1)
17
+ method_source (0.9.2)
18
+ parallel (1.20.1)
19
+ parser (3.0.2.0)
20
+ ast (~> 2.4.1)
21
+ pry (0.12.0)
22
+ coderay (~> 1.1.0)
23
+ method_source (~> 0.9.0)
24
+ pry-byebug (3.8.0)
25
+ byebug (~> 11.0)
26
+ pry (~> 0.10)
27
+ rainbow (3.0.0)
28
+ rake (13.0.6)
29
+ regexp_parser (2.1.1)
30
+ rexml (3.2.5)
31
+ rspec (3.10.0)
32
+ rspec-core (~> 3.10.0)
33
+ rspec-expectations (~> 3.10.0)
34
+ rspec-mocks (~> 3.10.0)
35
+ rspec-core (3.10.1)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-expectations (3.10.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-mocks (3.10.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.10.0)
43
+ rspec-support (3.10.3)
44
+ rspec_vars_helper (0.1.0)
45
+ rspec (>= 2.4)
46
+ rubocop (1.12.1)
47
+ parallel (~> 1.10)
48
+ parser (>= 3.0.0.0)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml
52
+ rubocop-ast (>= 1.2.0, < 2.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 3.0)
55
+ rubocop-ast (1.4.1)
56
+ parser (>= 2.7.1.5)
57
+ rubocop-performance (1.10.2)
58
+ rubocop (>= 0.90.0, < 2.0)
59
+ rubocop-ast (>= 0.4.0)
60
+ ruby-progressbar (1.11.0)
61
+ simplecov (0.17.0)
62
+ docile (~> 1.1)
63
+ json (>= 1.8, < 3)
64
+ simplecov-html (~> 0.10.0)
65
+ simplecov-html (0.10.2)
66
+ unicode-display_width (2.1.0)
67
+
68
+ PLATFORMS
69
+ x86_64-linux
70
+
71
+ DEPENDENCIES
72
+ bundler (~> 2)
73
+ byebug
74
+ hospodar!
75
+ pry (= 0.12)
76
+ pry-byebug
77
+ rake (~> 13.0)
78
+ rspec (~> 3.0)
79
+ rspec_vars_helper (~> 0.1)
80
+ rubocop (~> 1.12)
81
+ rubocop-performance
82
+ simplecov (= 0.17)
83
+ simplecov-html (~> 0.10.0)
84
+
85
+ BUNDLED WITH
86
+ 2.2.30
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 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,49 @@
1
+ # Hospodar
2
+
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/81f4cf3fc22fbe2ee346/maintainability)](https://codeclimate.com/github/andriy-baran/hospodar/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/81f4cf3fc22fbe2ee346/test_coverage)](https://codeclimate.com/github/andriy-baran/hospodar/test_coverage)
5
+
6
+ This library provides you with DSL that is common for an operation that is usually made by factory classes.
7
+ * Define a group of components
8
+ * Define component in a specific groupList components in a specific group
9
+ * Query specific component class
10
+ * Create an instance of a specific component
11
+ * Describe methods of a specific component
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'hospodar'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install hospodar
28
+
29
+ ## Usage
30
+
31
+ TODO: Write usage instructions here
32
+
33
+ ## Development
34
+
35
+ 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.
36
+
37
+ 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).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hospodar. 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]/hospodar/blob/master/CODE_OF_CONDUCT.md).
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
46
+
47
+ ## Code of Conduct
48
+
49
+ Everyone interacting in the Hospodar project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/hospodar/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 'hospodar'
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
data/hospodar.gemspec ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/hospodar/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'hospodar'
7
+ spec.version = Hospodar::VERSION
8
+ spec.authors = ['Andrii Baran']
9
+ spec.email = ['andriy.baran.v@gmail.com']
10
+
11
+ spec.summary = 'Create complex object easily'
12
+ spec.description = 'Simple DSL that supports creating complex strutures of objects'
13
+ spec.homepage = 'https://github.com/andriy-baran/hospodar'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.4.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/hospodar/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 do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_dependency 'dry-inflector', '~> 0.1'
35
+
36
+ spec.add_development_dependency 'bundler', '~> 2'
37
+ spec.add_development_dependency 'byebug'
38
+ spec.add_development_dependency 'pry', '0.12'
39
+ spec.add_development_dependency 'pry-byebug'
40
+ spec.add_development_dependency 'rake', '>= 12.3.3'
41
+ spec.add_development_dependency 'rspec', '~> 3.0'
42
+ spec.add_development_dependency 'rspec_vars_helper', '~> 0.1'
43
+ spec.add_development_dependency 'rubocop'
44
+ spec.add_development_dependency 'rubocop-performance'
45
+ spec.add_development_dependency 'simplecov', '0.17'
46
+ spec.add_development_dependency 'simplecov-html', '~> 0.10.0'
47
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hospodar/builder/strategies/execute'
4
+ require 'hospodar/builder/strategies/enumerate'
5
+ require 'hospodar/builder/strategies/translate'
6
+ require 'hospodar/builder/strategies/inject'
7
+ require 'hospodar/builder/strategies/init'
8
+ require 'hospodar/builder/strategies/link'
9
+ require 'hospodar/builder/strategies/mount'
10
+
11
+ module Hospodar
12
+ module Builder
13
+ BuilderParams = Struct.new(:object, :title, :group) do
14
+ def to_a
15
+ [group, title, proc { object }]
16
+ end
17
+
18
+ def check!
19
+ return unless invalid?
20
+
21
+ raise(ArgumentError, 'Please provide object: and title: arguments')
22
+ end
23
+
24
+ def invalid?
25
+ (group.nil? && title.nil?) ^ object.nil?
26
+ end
27
+ end
28
+
29
+ # Base class for writing methods that are supported in schemas
30
+ class Assembler
31
+ attr_accessor :builder_params, :target, :on_exception, :planing_method,
32
+ :execution_method, :build_method, :new_struct_method
33
+
34
+ def initialize(factory, on_exception)
35
+ @factory = factory
36
+ @on_exception = on_exception
37
+ end
38
+
39
+ def for(name, &block)
40
+ @name = name
41
+ @planing_method = :"hospodar_perform_planing_for_#{name}"
42
+ @execution_method = :"hospodar_execute_plan_for_#{name}"
43
+ @build_method = :"build_#{name}"
44
+ @new_struct_method = :"new_#{name}_#{type}_struct_instance"
45
+ @dsl_block = block
46
+ self
47
+ end
48
+
49
+ def inject_method
50
+ define_planing_method
51
+ define_plan_execution_method
52
+ define_building_method
53
+ end
54
+
55
+ private
56
+
57
+ attr_reader :factory, :name, :dsl_block
58
+
59
+ def define_planing_method
60
+ assembler = self
61
+ factory.define_singleton_method(@planing_method) do |object, title, group|
62
+ assembler.builder_params = BuilderParams.new(object, title, group).tap(&:check!)
63
+ assembler.target = public_send(assembler.new_struct_method)
64
+ assembler.target.on_exception = assembler.on_exception
65
+ assembler.on_planing(self)
66
+ end
67
+ factory.private_class_method @planing_method
68
+ end
69
+
70
+ def define_plan_execution_method
71
+ assembler = self
72
+ factory.define_singleton_method(@execution_method) do |plan, &on_create|
73
+ process = assembler.on_building(self, plan, &on_create)
74
+ Proxy.new(process)
75
+ end
76
+ factory.private_class_method @execution_method
77
+ end
78
+
79
+ def define_building_method
80
+ assembler = self
81
+ factory.define_singleton_method(build_method) do |object: nil, title: nil, group: nil, &on_create|
82
+ plan = send(assembler.planing_method, object, title, group)
83
+ send(assembler.execution_method, plan, &on_create)
84
+ end
85
+ end
86
+
87
+ def creation_matrix_from_dsl(factory, reverse: nil, &block)
88
+ trace_dsl_block(reverse: reverse, &block).with_creation_procs(factory)
89
+ end
90
+
91
+ def trace_dsl_block(reverse: nil, &block)
92
+ Strategies::Translate.call(reverse: reverse, &block)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hospodar
4
+ module Builder
5
+ # General error that has step ID object
6
+ class Error < StandardError
7
+ attr_reader :step_id
8
+
9
+ def initialize(msg, id)
10
+ super(msg)
11
+ @step_id = id
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hospodar
4
+ module Builder
5
+ # Adds integration for exceptions handling
6
+ module Exceptional
7
+ def self.included(received)
8
+ received.send(:attr_accessor, :exception, :on_exception)
9
+ end
10
+
11
+ def ignore_exceptions?
12
+ on_exception == :ignore
13
+ end
14
+
15
+ def exceptional?
16
+ !exception.nil?
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hospodar
4
+ module Builder
5
+ # Implements flat(&block) strategy in schemas
6
+ class Flatten < Assembler
7
+ def on_planing(receiver)
8
+ execution_plan(receiver, &dsl_block)
9
+ end
10
+
11
+ def on_building(receiver, plan, &on_create)
12
+ building_steps(receiver, plan, &on_create)
13
+ end
14
+
15
+ private
16
+
17
+ def type
18
+ :flatten
19
+ end
20
+
21
+ def building_steps(receiver, plan, &block)
22
+ inject = Strategies::Inject.new(plan.map(&:first), &block)
23
+ mount = Strategies::Mount.new(inject, plan, target, builder_params.title)
24
+ Strategies::Execute.new(mount, target, receiver)
25
+ end
26
+
27
+ def execution_plan(receiver)
28
+ plan = creation_matrix_from_dsl(receiver, &dsl_block).collection
29
+ plan.unshift(builder_params.to_a) unless builder_params.object.nil?
30
+ plan = plan.transpose
31
+ ids = plan[0..1].transpose.map { |g, e| Hospodar::Builder::Id.new(g, e) }
32
+ ids.zip(plan[-1])
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hospodar
4
+ module Builder
5
+ # Helps determine object creation procedure for given attributes
6
+ module Helpers
7
+ def mother_ship_assembler_new_instance(group, step, *_attrs)
8
+ return public_send(:"build_#{step}") if %i[flat wrap nest].include?(group)
9
+
10
+ mod = included_modules.detect { |m| m.respond_to?(:component_name) && m.component_name == group }
11
+ return if mod.nil?
12
+
13
+ init_method = public_send(mod.component_class_reader(step)).method(:__ms_init__)
14
+ return method(mod.new_instance_method_name(step)) if init_method.arity < 2
15
+
16
+ create_method = method(mod.new_instance_method_name(step))
17
+ create_method.curry(create_method.arity.abs)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hospodar
4
+ module Builder
5
+ Id = Struct.new(:group, :title) do
6
+ def to_s
7
+ to_a.compact.reverse.join('_')
8
+ end
9
+
10
+ def to_sym
11
+ to_s.to_sym
12
+ end
13
+ end
14
+ end
15
+ end