rom 2.0.2 → 4.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +35 -430
  3. data/LICENSE +1 -1
  4. data/README.md +83 -11
  5. data/lib/rom/version.rb +1 -1
  6. data/lib/rom.rb +3 -46
  7. metadata +30 -236
  8. data/.gitignore +0 -24
  9. data/.rspec +0 -3
  10. data/.rubocop.yml +0 -87
  11. data/.rubocop_todo.yml +0 -46
  12. data/.travis.yml +0 -28
  13. data/CODE_OF_CONDUCT.md +0 -13
  14. data/CONTRIBUTING.md +0 -1
  15. data/Gemfile +0 -41
  16. data/Guardfile +0 -24
  17. data/Rakefile +0 -22
  18. data/lib/rom/association_set.rb +0 -26
  19. data/lib/rom/command.rb +0 -173
  20. data/lib/rom/command_registry.rb +0 -153
  21. data/lib/rom/commands/class_interface.rb +0 -148
  22. data/lib/rom/commands/composite.rb +0 -54
  23. data/lib/rom/commands/create.rb +0 -13
  24. data/lib/rom/commands/delete.rb +0 -14
  25. data/lib/rom/commands/graph/builder.rb +0 -176
  26. data/lib/rom/commands/graph/class_interface.rb +0 -62
  27. data/lib/rom/commands/graph/input_evaluator.rb +0 -62
  28. data/lib/rom/commands/graph.rb +0 -103
  29. data/lib/rom/commands/lazy/create.rb +0 -23
  30. data/lib/rom/commands/lazy/delete.rb +0 -27
  31. data/lib/rom/commands/lazy/update.rb +0 -34
  32. data/lib/rom/commands/lazy.rb +0 -99
  33. data/lib/rom/commands/result.rb +0 -96
  34. data/lib/rom/commands/update.rb +0 -14
  35. data/lib/rom/commands.rb +0 -3
  36. data/lib/rom/configuration.rb +0 -88
  37. data/lib/rom/configuration_dsl/command.rb +0 -43
  38. data/lib/rom/configuration_dsl/command_dsl.rb +0 -35
  39. data/lib/rom/configuration_dsl/mapper.rb +0 -37
  40. data/lib/rom/configuration_dsl/mapper_dsl.rb +0 -44
  41. data/lib/rom/configuration_dsl/relation.rb +0 -26
  42. data/lib/rom/configuration_dsl.rb +0 -78
  43. data/lib/rom/configuration_plugin.rb +0 -17
  44. data/lib/rom/constants.rb +0 -30
  45. data/lib/rom/container.rb +0 -231
  46. data/lib/rom/create_container.rb +0 -61
  47. data/lib/rom/environment.rb +0 -70
  48. data/lib/rom/gateway.rb +0 -154
  49. data/lib/rom/global/plugin_dsl.rb +0 -49
  50. data/lib/rom/global.rb +0 -60
  51. data/lib/rom/lint/enumerable_dataset.rb +0 -54
  52. data/lib/rom/lint/gateway.rb +0 -94
  53. data/lib/rom/lint/linter.rb +0 -78
  54. data/lib/rom/lint/spec.rb +0 -20
  55. data/lib/rom/lint/test.rb +0 -98
  56. data/lib/rom/mapper_registry.rb +0 -35
  57. data/lib/rom/memory/commands.rb +0 -58
  58. data/lib/rom/memory/dataset.rb +0 -98
  59. data/lib/rom/memory/gateway.rb +0 -64
  60. data/lib/rom/memory/relation.rb +0 -49
  61. data/lib/rom/memory/storage.rb +0 -59
  62. data/lib/rom/memory/types.rb +0 -9
  63. data/lib/rom/memory.rb +0 -4
  64. data/lib/rom/pipeline.rb +0 -106
  65. data/lib/rom/plugin.rb +0 -17
  66. data/lib/rom/plugin_base.rb +0 -31
  67. data/lib/rom/plugin_registry.rb +0 -173
  68. data/lib/rom/plugins/command/schema.rb +0 -37
  69. data/lib/rom/plugins/configuration/configuration_dsl.rb +0 -17
  70. data/lib/rom/plugins/relation/key_inference.rb +0 -46
  71. data/lib/rom/plugins/relation/registry_reader.rb +0 -32
  72. data/lib/rom/plugins/relation/view/dsl.rb +0 -32
  73. data/lib/rom/plugins/relation/view.rb +0 -95
  74. data/lib/rom/relation/class_interface.rb +0 -230
  75. data/lib/rom/relation/composite.rb +0 -46
  76. data/lib/rom/relation/curried.rb +0 -104
  77. data/lib/rom/relation/graph.rb +0 -115
  78. data/lib/rom/relation/loaded.rb +0 -118
  79. data/lib/rom/relation/materializable.rb +0 -66
  80. data/lib/rom/relation/name.rb +0 -102
  81. data/lib/rom/relation.rb +0 -172
  82. data/lib/rom/relation_registry.rb +0 -9
  83. data/lib/rom/schema/dsl.rb +0 -58
  84. data/lib/rom/schema.rb +0 -89
  85. data/lib/rom/setup/auto_registration.rb +0 -69
  86. data/lib/rom/setup/auto_registration_strategies/base.rb +0 -11
  87. data/lib/rom/setup/auto_registration_strategies/custom_namespace.rb +0 -22
  88. data/lib/rom/setup/auto_registration_strategies/no_namespace.rb +0 -19
  89. data/lib/rom/setup/auto_registration_strategies/with_namespace.rb +0 -18
  90. data/lib/rom/setup/finalize/finalize_commands.rb +0 -47
  91. data/lib/rom/setup/finalize/finalize_mappers.rb +0 -36
  92. data/lib/rom/setup/finalize/finalize_relations.rb +0 -60
  93. data/lib/rom/setup/finalize.rb +0 -146
  94. data/lib/rom/setup.rb +0 -55
  95. data/lib/rom/support/configurable.rb +0 -80
  96. data/lib/rom/types.rb +0 -18
  97. data/log/.gitkeep +0 -0
  98. data/rakelib/benchmark.rake +0 -15
  99. data/rakelib/mutant.rake +0 -19
  100. data/rakelib/rubocop.rake +0 -18
  101. data/rom.gemspec +0 -26
  102. data/spec/fixtures/app/commands/create_user.rb +0 -2
  103. data/spec/fixtures/app/mappers/user_list.rb +0 -2
  104. data/spec/fixtures/app/my_commands/create_user.rb +0 -2
  105. data/spec/fixtures/app/my_mappers/user_list.rb +0 -2
  106. data/spec/fixtures/app/my_relations/users.rb +0 -2
  107. data/spec/fixtures/app/relations/users.rb +0 -2
  108. data/spec/fixtures/custom/commands/create_user.rb +0 -6
  109. data/spec/fixtures/custom/mappers/user_list.rb +0 -6
  110. data/spec/fixtures/custom/relations/users.rb +0 -6
  111. data/spec/fixtures/lib/persistence/commands/create_user.rb +0 -6
  112. data/spec/fixtures/lib/persistence/mappers/user_list.rb +0 -6
  113. data/spec/fixtures/lib/persistence/my_commands/create_user.rb +0 -6
  114. data/spec/fixtures/lib/persistence/my_mappers/user_list.rb +0 -6
  115. data/spec/fixtures/lib/persistence/my_relations/users.rb +0 -6
  116. data/spec/fixtures/lib/persistence/relations/users.rb +0 -6
  117. data/spec/integration/command_registry_spec.rb +0 -60
  118. data/spec/integration/commands/create_spec.rb +0 -177
  119. data/spec/integration/commands/delete_spec.rb +0 -67
  120. data/spec/integration/commands/error_handling_spec.rb +0 -25
  121. data/spec/integration/commands/graph_builder_spec.rb +0 -213
  122. data/spec/integration/commands/graph_spec.rb +0 -288
  123. data/spec/integration/commands/update_spec.rb +0 -109
  124. data/spec/integration/commands_spec.rb +0 -67
  125. data/spec/integration/mappers/combine_spec.rb +0 -117
  126. data/spec/integration/mappers/deep_embedded_spec.rb +0 -44
  127. data/spec/integration/mappers/definition_dsl_spec.rb +0 -206
  128. data/spec/integration/mappers/embedded_spec.rb +0 -62
  129. data/spec/integration/mappers/exclude_spec.rb +0 -27
  130. data/spec/integration/mappers/fold_spec.rb +0 -71
  131. data/spec/integration/mappers/group_spec.rb +0 -163
  132. data/spec/integration/mappers/overwrite_attributes_value_spec.rb +0 -51
  133. data/spec/integration/mappers/prefix_separator_spec.rb +0 -52
  134. data/spec/integration/mappers/prefix_spec.rb +0 -48
  135. data/spec/integration/mappers/prefixing_attributes_spec.rb +0 -37
  136. data/spec/integration/mappers/registering_custom_mappers_spec.rb +0 -28
  137. data/spec/integration/mappers/renaming_attributes_spec.rb +0 -125
  138. data/spec/integration/mappers/reusing_mappers_spec.rb +0 -43
  139. data/spec/integration/mappers/step_spec.rb +0 -119
  140. data/spec/integration/mappers/symbolizing_attributes_spec.rb +0 -77
  141. data/spec/integration/mappers/unfold_spec.rb +0 -92
  142. data/spec/integration/mappers/ungroup_spec.rb +0 -126
  143. data/spec/integration/mappers/unwrap_spec.rb +0 -93
  144. data/spec/integration/mappers/wrap_spec.rb +0 -155
  145. data/spec/integration/memory/commands/create_spec.rb +0 -23
  146. data/spec/integration/memory/commands/delete_spec.rb +0 -23
  147. data/spec/integration/memory/commands/update_spec.rb +0 -23
  148. data/spec/integration/multi_env_spec.rb +0 -69
  149. data/spec/integration/multi_repo_spec.rb +0 -46
  150. data/spec/integration/relations/default_dataset_spec.rb +0 -38
  151. data/spec/integration/relations/inheritance_spec.rb +0 -37
  152. data/spec/integration/relations/reading_spec.rb +0 -169
  153. data/spec/integration/relations/registry_dsl_spec.rb +0 -45
  154. data/spec/integration/repositories/extending_relations_spec.rb +0 -58
  155. data/spec/integration/repositories/setting_logger_spec.rb +0 -34
  156. data/spec/integration/setup_spec.rb +0 -160
  157. data/spec/shared/command_behavior.rb +0 -28
  158. data/spec/shared/command_graph.rb +0 -50
  159. data/spec/shared/container.rb +0 -9
  160. data/spec/shared/enumerable_dataset.rb +0 -52
  161. data/spec/shared/gateway_only.rb +0 -6
  162. data/spec/shared/materializable.rb +0 -36
  163. data/spec/shared/no_container.rb +0 -16
  164. data/spec/shared/one_behavior.rb +0 -26
  165. data/spec/shared/proxy.rb +0 -0
  166. data/spec/shared/users_and_tasks.rb +0 -10
  167. data/spec/spec_helper.rb +0 -52
  168. data/spec/support/constant_leak_finder.rb +0 -14
  169. data/spec/support/mutant.rb +0 -10
  170. data/spec/support/types.rb +0 -5
  171. data/spec/test/memory_repository_lint_test.rb +0 -27
  172. data/spec/unit/rom/association_set_spec.rb +0 -23
  173. data/spec/unit/rom/commands/graph_spec.rb +0 -191
  174. data/spec/unit/rom/commands/lazy_spec.rb +0 -307
  175. data/spec/unit/rom/commands/result_spec.rb +0 -70
  176. data/spec/unit/rom/commands_spec.rb +0 -165
  177. data/spec/unit/rom/configurable_spec.rb +0 -49
  178. data/spec/unit/rom/configuration_spec.rb +0 -61
  179. data/spec/unit/rom/container_spec.rb +0 -99
  180. data/spec/unit/rom/create_container_spec.rb +0 -151
  181. data/spec/unit/rom/environment_spec.rb +0 -123
  182. data/spec/unit/rom/gateway_spec.rb +0 -146
  183. data/spec/unit/rom/mapper_registry_spec.rb +0 -25
  184. data/spec/unit/rom/memory/commands_spec.rb +0 -43
  185. data/spec/unit/rom/memory/dataset_spec.rb +0 -31
  186. data/spec/unit/rom/memory/relation_spec.rb +0 -109
  187. data/spec/unit/rom/memory/repository_spec.rb +0 -12
  188. data/spec/unit/rom/memory/storage_spec.rb +0 -45
  189. data/spec/unit/rom/plugin_spec.rb +0 -150
  190. data/spec/unit/rom/plugins/command/schema_spec.rb +0 -66
  191. data/spec/unit/rom/plugins/relation/key_inference_spec.rb +0 -85
  192. data/spec/unit/rom/plugins/relation/view_spec.rb +0 -51
  193. data/spec/unit/rom/relation/composite_spec.rb +0 -89
  194. data/spec/unit/rom/relation/curried_spec.rb +0 -52
  195. data/spec/unit/rom/relation/graph_spec.rb +0 -92
  196. data/spec/unit/rom/relation/lazy/combine_spec.rb +0 -165
  197. data/spec/unit/rom/relation/lazy_spec.rb +0 -214
  198. data/spec/unit/rom/relation/loaded_spec.rb +0 -53
  199. data/spec/unit/rom/relation/name_spec.rb +0 -58
  200. data/spec/unit/rom/relation/schema_spec.rb +0 -117
  201. data/spec/unit/rom/relation_spec.rb +0 -237
  202. data/spec/unit/rom/schema_spec.rb +0 -10
  203. data/spec/unit/rom/setup/auto_registration_spec.rb +0 -152
data/.rubocop_todo.yml DELETED
@@ -1,46 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2015-09-26 16:17:55 +0200 using RuboCop version 0.34.2.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 3
10
- Lint/NestedMethodDefinition:
11
- Exclude:
12
- - 'lib/rom/relation/class_interface.rb'
13
- - 'spec/unit/rom/commands_spec.rb'
14
-
15
- # Offense count: 13
16
- Metrics/AbcSize:
17
- Max: 38
18
-
19
- # Offense count: 3
20
- Metrics/CyclomaticComplexity:
21
- Max: 9
22
-
23
- # Offense count: 65
24
- # Configuration parameters: AllowURI, URISchemes.
25
- Metrics/LineLength:
26
- Max: 113
27
-
28
- # Offense count: 20
29
- # Configuration parameters: CountComments.
30
- Metrics/MethodLength:
31
- Max: 42
32
-
33
- # Offense count: 3
34
- Metrics/PerceivedComplexity:
35
- Max: 11
36
-
37
- # Offense count: 1
38
- # Configuration parameters: EnforcedStyle, SupportedStyles.
39
- Style/MethodName:
40
- Enabled: false
41
-
42
- # Offense count: 1
43
- # Configuration parameters: Methods.
44
- Style/SingleLineBlockParams:
45
- Exclude:
46
- - 'lib/rom/commands/graph/class_interface.rb'
data/.travis.yml DELETED
@@ -1,28 +0,0 @@
1
- language: ruby
2
- dist: trusty
3
- sudo: required
4
- cache: bundler
5
- bundler_args: --without sql benchmarks console tools
6
- script: "bundle exec rake ci"
7
- after_success:
8
- - '[ "$TRAVIS_RUBY_VERSION" = "2.3.1" ] && [ "$TRAVIS_BRANCH" = "master" ] && bundle exec codeclimate-test-reporter'
9
- rvm:
10
- - 2.1
11
- - 2.2
12
- - 2.3.1
13
- - rbx-3
14
- - jruby-9.1.5.0
15
- env:
16
- global:
17
- - JRUBY_OPTS='--dev -J-Xmx1024M'
18
- - COVERAGE='true'
19
- matrix:
20
- allow_failures:
21
- - rvm: rbx-3
22
- notifications:
23
- webhooks:
24
- urls:
25
- - https://webhooks.gitter.im/e/39e1225f489f38b0bd09
26
- on_success: change
27
- on_failure: always
28
- on_start: false
data/CODE_OF_CONDUCT.md DELETED
@@ -1,13 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
-
5
- We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
-
7
- Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
-
9
- Project maintainers 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. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
-
11
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
-
13
- This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/CONTRIBUTING.md DELETED
@@ -1 +0,0 @@
1
- For information about contributing please refer to [Contribute](http://rom-rb.org/contribute) section on ROM website.
data/Gemfile DELETED
@@ -1,41 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- group :console do
6
- gem 'pry'
7
- gem 'pg', platforms: [:mri]
8
- end
9
-
10
- group :test do
11
- gem 'dry-equalizer'
12
- gem 'dry-struct'
13
- gem 'minitest'
14
- gem 'inflecto', '~> 0.0', '>= 0.0.2'
15
-
16
- platforms :mri do
17
- gem 'codeclimate-test-reporter', require: false
18
- gem 'simplecov', require: false
19
- end
20
-
21
- platforms :rbx do
22
- gem 'rubysl-bigdecimal'
23
- end
24
- end
25
-
26
- group :sql do
27
- gem 'rom-sql', github: 'rom-rb/rom-sql', branch: 'master'
28
- gem 'sequel'
29
- gem 'jdbc-sqlite3', platforms: :jruby
30
- gem 'sqlite3', platforms: [:mri, :rbx]
31
- end
32
-
33
- group :benchmarks do
34
- gem 'activerecord', '~> 5.0'
35
- gem 'benchmark-ips', '~> 2.2.0'
36
- gem 'rom-repository', github: 'rom-rb/rom-repository', branch: 'master'
37
- end
38
-
39
- group :tools do
40
- gem 'byebug', platform: :mri
41
- end
data/Guardfile DELETED
@@ -1,24 +0,0 @@
1
- group :red_green_refactor, halt_on_fail: true do
2
- guard :rspec, cmd: "rspec", all_on_start: true do
3
- # run all specs if Gemfile.lock is modified
4
- watch('Gemfile.lock') { 'spec' }
5
-
6
- # run all specs if any library code is modified
7
- watch(%r{\Alib/.+\.rb\z}) { 'spec' }
8
-
9
- # run all specs if supporting files are modified
10
- watch('spec/spec_helper.rb') { 'spec' }
11
- watch(%r{\Aspec/(?:lib|support|shared)/.+\.rb\z}) { 'spec' }
12
-
13
- # run a spec if it is modified
14
- watch(%r{\Aspec/(?:unit|integration)/.+_spec\.rb\z})
15
-
16
- notification :tmux, display_message: true if ENV.key?('TMUX')
17
- end
18
-
19
- guard :rubocop do
20
- # run rubocop on modified file
21
- watch(%r{\Alib/.+\.rb\z})
22
- watch(%r{\Aspec/.+\.rb\z})
23
- end
24
- end
data/Rakefile DELETED
@@ -1,22 +0,0 @@
1
- require "rspec/core/rake_task"
2
- require "rake/testtask"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
- task default: [:ci]
6
-
7
- desc 'Run specs in isolation'
8
- task :"spec:isolation" do
9
- FileList["spec/**/*_spec.rb"].each do |spec|
10
- sh "COVERAGE=false bundle exec rspec #{spec}"
11
- end
12
- end
13
-
14
- desc "Run CI tasks"
15
- task ci: [:spec, :lint, :"spec:isolation"]
16
-
17
- Rake::TestTask.new(:lint) do |test|
18
- test.description = "Run adapter lint tests against memory adapter"
19
- test.test_files = FileList.new('spec/test/*_test.rb')
20
- test.libs << 'test'
21
- test.verbose = true
22
- end
@@ -1,26 +0,0 @@
1
- require 'rom/support/registry'
2
- require 'rom/support/inflector'
3
-
4
- module ROM
5
- class AssociationSet < ROM::Registry
6
- # @api private
7
- def try(name, &block)
8
- if key?(name)
9
- yield(self[name])
10
- else
11
- false
12
- end
13
- end
14
-
15
- # @api private
16
- def [](name)
17
- key = name.to_sym
18
-
19
- if key?(key)
20
- super
21
- else
22
- super(Inflector.singularize(key))
23
- end
24
- end
25
- end
26
- end
data/lib/rom/command.rb DELETED
@@ -1,173 +0,0 @@
1
- require 'rom/support/deprecations'
2
- require 'rom/support/options'
3
- require 'rom/pipeline'
4
-
5
- require 'rom/commands/class_interface'
6
- require 'rom/commands/composite'
7
- require 'rom/commands/graph'
8
- require 'rom/commands/lazy'
9
-
10
- module ROM
11
- # Abstract command class
12
- #
13
- # Provides a constructor accepting relation with options and basic behavior
14
- # for calling, currying and composing commands.
15
- #
16
- # Typically command subclasses should inherit from specialized
17
- # Create/Update/Delete, not this one.
18
- #
19
- # @abstract
20
- #
21
- # @private
22
- class Command
23
- DEFAULT_VALIDATOR = proc {}
24
-
25
- include Dry::Equalizer(:relation, :options)
26
- include Commands
27
- include Pipeline::Operator
28
-
29
- extend ClassMacros
30
- extend ClassInterface
31
-
32
- include Options
33
-
34
- defines :adapter, :relation, :result, :input, :validator, :register_as, :restrictable
35
-
36
- option :type, allow: [:create, :update, :delete]
37
- option :source, reader: true
38
- option :result, reader: true, allow: [:one, :many]
39
- option :validator, reader: true
40
- option :input, reader: true
41
- option :curry_args, type: Array, reader: true, default: EMPTY_ARRAY
42
-
43
- input Hash
44
- validator DEFAULT_VALIDATOR
45
- result :many
46
-
47
- # @deprecated
48
- #
49
- # @api public
50
- def self.validator(vp = nil)
51
- if defined?(@validator) && vp.nil?
52
- @validator
53
- else
54
- unless vp.equal?(DEFAULT_VALIDATOR)
55
- Deprecations.announce(
56
- "#{name}.validator",
57
- 'Please handle validation before calling commands'
58
- )
59
- end
60
- super
61
- end
62
- end
63
-
64
- # @attr_reader [Relation] relation The command's relation
65
- attr_reader :relation
66
-
67
- # @api private
68
- def initialize(relation, options = EMPTY_HASH)
69
- super
70
- @relation = relation
71
- @source = options[:source] || relation
72
- end
73
-
74
- # Return name of this command's relation
75
- #
76
- # @return [ROM::Relation::Name]
77
- #
78
- # @api public
79
- def name
80
- relation.name
81
- end
82
-
83
- # Return gateway of this command's relation
84
- #
85
- # @return [Symbol]
86
- #
87
- # @api public
88
- def gateway
89
- relation.gateway
90
- end
91
-
92
- # Execute the command
93
- #
94
- # @abstract
95
- #
96
- # @return [Array] an array with inserted tuples
97
- #
98
- # @api private
99
- def execute(*)
100
- raise(
101
- NotImplementedError,
102
- "#{self.class}##{__method__} must be implemented"
103
- )
104
- end
105
-
106
- # Call the command and return one or many tuples
107
- #
108
- # @api public
109
- def call(*args, &block)
110
- tuples = execute(*(curry_args + args), &block)
111
-
112
- if one?
113
- tuples.first
114
- else
115
- tuples
116
- end
117
- end
118
- alias_method :[], :call
119
-
120
- # Curry this command with provided args
121
- #
122
- # Curried command can be called without args
123
- #
124
- # @return [Command]
125
- #
126
- # @api public
127
- def curry(*args)
128
- if curry_args.empty? && args.first.is_a?(Graph::InputEvaluator)
129
- Lazy[self].new(self, *args)
130
- else
131
- self.class.build(relation, options.merge(curry_args: args))
132
- end
133
- end
134
- alias_method :with, :curry
135
-
136
- # @api public
137
- def combine(*others)
138
- Graph.new(self, others)
139
- end
140
-
141
- # @api private
142
- def lazy?
143
- false
144
- end
145
-
146
- # @api private
147
- def graph?
148
- false
149
- end
150
-
151
- # @api private
152
- def one?
153
- result.equal?(:one)
154
- end
155
-
156
- # @api private
157
- def many?
158
- result.equal?(:many)
159
- end
160
-
161
- # @api private
162
- def new(new_relation)
163
- self.class.build(new_relation, options.merge(source: relation))
164
- end
165
-
166
- private
167
-
168
- # @api private
169
- def composite_class
170
- Command::Composite
171
- end
172
- end
173
- end
@@ -1,153 +0,0 @@
1
- require 'rom/commands/result'
2
-
3
- module ROM
4
- # Specialized registry class for commands
5
- #
6
- # @api public
7
- class CommandRegistry
8
- include Commands
9
- include Options
10
-
11
- CommandNotFoundError = Class.new(KeyError)
12
-
13
- # Name of the relation from which commands are under
14
- #
15
- # @return [String]
16
- #
17
- # @api private
18
- attr_reader :relation_name
19
-
20
- # Internal command registry
21
- #
22
- # @return [Registry]
23
- #
24
- # @api private
25
- attr_reader :registry
26
-
27
- option :mappers, reader: true
28
- option :mapper, reader: true
29
-
30
- # @api private
31
- def initialize(relation_name, elements, options = EMPTY_HASH)
32
- super
33
-
34
- @relation_name = relation_name
35
- @registry =
36
- if elements.is_a?(Registry)
37
- elements
38
- else
39
- Registry.new(elements, self.class.name)
40
- end
41
- end
42
-
43
- # Try to execute a command in a block
44
- #
45
- # @yield [command] Passes command to the block
46
- #
47
- # @example
48
- #
49
- # rom.command(:users).try { create(name: 'Jane') }
50
- # rom.command(:users).try { update(:by_id, 1).call(name: 'Jane Doe') }
51
- # rom.command(:users).try { delete(:by_id, 1) }
52
- #
53
- # @return [Commands::Result]
54
- #
55
- # @api public
56
- def try(&block)
57
- response = block.call
58
-
59
- if response.is_a?(Command) || response.is_a?(Composite)
60
- try { response.call }
61
- else
62
- Result::Success.new(response)
63
- end
64
- rescue CommandError => e
65
- Result::Failure.new(e)
66
- end
67
-
68
- # Return a command from the registry
69
- #
70
- # If mapper is set command will be turned into a composite command with
71
- # auto-mapping
72
- #
73
- # @example
74
- # create_user = rom.command(:users)[:create]
75
- # create_user[name: 'Jane']
76
- #
77
- # # with mapping, assuming :entity mapper is registered for :users relation
78
- # create_user = rom.command(:users).as(:entity)[:create]
79
- # create_user[name: 'Jane'] # => result is send through :entity mapper
80
- #
81
- # @param [Symbol] name The name of a registered command
82
- #
83
- # @return [Command,Command::Composite]
84
- #
85
- # @api public
86
- def [](name)
87
- command = fetch_command(name)
88
- mapper = options[:mapper]
89
-
90
- if mapper
91
- command.curry >> mapper
92
- else
93
- command
94
- end
95
- end
96
-
97
- # Specify a mapper that should be used for commands from this registry
98
- #
99
- # @example
100
- # entity_commands = rom.command(:users).as(:entity)
101
- #
102
- #
103
- # @param [Symbol] mapper_name The name of a registered mapper
104
- #
105
- # @return [CommandRegistry]
106
- #
107
- # @api public
108
- def as(mapper_name)
109
- with(mapper: mappers[mapper_name])
110
- end
111
-
112
- # Return new instance of this registry with updated options
113
- #
114
- # @return [CommandRegistry]
115
- #
116
- # @api private
117
- def with(new_options)
118
- self.class.new(relation_name, registry, options.merge(new_options))
119
- end
120
-
121
- private
122
-
123
- # Fetch a command from the registry and raise a specialized error
124
- # in case the command is not found
125
- #
126
- # @api private
127
- def fetch_command(name)
128
- registry.fetch(name) do
129
- raise CommandNotFoundError.new(
130
- "There is no :#{name} command for :#{relation_name} relation"
131
- )
132
- end
133
- end
134
-
135
- # Allow checking if a certain command is available using dot-notation
136
- #
137
- # @api private
138
- def respond_to_missing?(name, include_private = false)
139
- registry.key?(name) || super
140
- end
141
-
142
- # Allow retrieving commands using dot-notation
143
- #
144
- # @api private
145
- def method_missing(name, *)
146
- if registry.key?(name)
147
- self[name]
148
- else
149
- super
150
- end
151
- end
152
- end
153
- end
@@ -1,148 +0,0 @@
1
- require 'rom/support/class_builder'
2
-
3
- module ROM
4
- # Base command class with factory class-level interface and setup-related logic
5
- #
6
- # @private
7
- class Command
8
- module ClassInterface
9
- # Return adapter specific sub-class based on the adapter identifier
10
- #
11
- # This is a syntax sugar to make things consistent
12
- #
13
- # @example
14
- # ROM::Commands::Create[:memory]
15
- # # => ROM::Memory::Commands::Create
16
- #
17
- # @param [Symbol] adapter identifier
18
- #
19
- # @return [Class]
20
- #
21
- # @api public
22
- def [](adapter)
23
- adapter_namespace(adapter).const_get(Inflector.demodulize(name))
24
- end
25
-
26
- # Return namespaces that contains command subclasses of a specific adapter
27
- #
28
- # @param [Symbol] adapter identifier
29
- #
30
- # @return [Module]
31
- #
32
- # @api private
33
- def adapter_namespace(adapter)
34
- ROM.adapters.fetch(adapter).const_get(:Commands)
35
- rescue KeyError
36
- raise AdapterNotPresentError.new(adapter, :relation)
37
- end
38
-
39
- # Build a command class for a specific relation with options
40
- #
41
- # @example
42
- # class CreateUser < ROM::Commands::Create[:memory]
43
- # end
44
- #
45
- # command = CreateUser.build(rom.relations[:users])
46
- #
47
- # @param [Relation] relation
48
- # @param [Hash] options
49
- #
50
- # @return [Command]
51
- #
52
- # @api public
53
- def build(relation, options = EMPTY_HASH)
54
- new(relation, self.options.merge(options))
55
- end
56
-
57
- # Create a command class with a specific type
58
- #
59
- # @param [Symbol] command name
60
- # @param [Class] parent class
61
- #
62
- # @yield [Class] create class
63
- #
64
- # @return [Class, Object] return result of the block if it was provided
65
- #
66
- # @api public
67
- def create_class(name, type, &block)
68
- klass = ClassBuilder
69
- .new(name: "#{Inflector.classify(type)}[:#{name}]", parent: type)
70
- .call
71
-
72
- if block
73
- yield(klass)
74
- else
75
- klass
76
- end
77
- end
78
-
79
- # Use a configured plugin in this relation
80
- #
81
- # @example
82
- # class CreateUser < ROM::Commands::Create[:memory]
83
- # use :pagintion
84
- #
85
- # per_page 30
86
- # end
87
- #
88
- # @param [Symbol] plugin
89
- # @param [Hash] options
90
- # @option options [Symbol] :adapter (:default) first adapter to check for plugin
91
- #
92
- # @api public
93
- def use(plugin, _options = EMPTY_HASH)
94
- ROM.plugin_registry.commands.fetch(plugin, adapter).apply_to(self)
95
- end
96
-
97
- # Extend a command class with relation view methods
98
- #
99
- # @param [Relation]
100
- #
101
- # @return [Class]
102
- #
103
- # @api public
104
- def extend_for_relation(relation)
105
- include(relation_methods_mod(relation.class))
106
- end
107
-
108
- # Return default name of the command class based on its name
109
- #
110
- # During setup phase this is used by defalut as `register_as` option
111
- #
112
- # @return [Symbol]
113
- #
114
- # @api private
115
- def default_name
116
- Inflector.underscore(Inflector.demodulize(name)).to_sym
117
- end
118
-
119
- # Return default options based on class macros
120
- #
121
- # @return [Hash]
122
- #
123
- # @api private
124
- def options
125
- { input: input, validator: validator, result: result }
126
- end
127
-
128
- # @api private
129
- def relation_methods_mod(relation_class)
130
- Module.new do
131
- relation_class.view_methods.each do |meth|
132
- module_eval <<-RUBY, __FILE__, __LINE__ + 1
133
- def #{meth}(*args)
134
- response = relation.public_send(:#{meth}, *args)
135
-
136
- if response.is_a?(relation.class)
137
- new(response)
138
- else
139
- response
140
- end
141
- end
142
- RUBY
143
- end
144
- end
145
- end
146
- end
147
- end
148
- end