rom 3.3.3 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -599
  3. data/lib/rom/version.rb +1 -1
  4. data/lib/rom.rb +3 -44
  5. metadata +19 -295
  6. data/.gitignore +0 -24
  7. data/.rspec +0 -3
  8. data/.rubocop.yml +0 -87
  9. data/.rubocop_todo.yml +0 -46
  10. data/.travis.yml +0 -24
  11. data/.yardopts +0 -2
  12. data/CODE_OF_CONDUCT.md +0 -13
  13. data/CONTRIBUTING.md +0 -1
  14. data/Gemfile +0 -54
  15. data/Guardfile +0 -24
  16. data/Rakefile +0 -28
  17. data/lib/rom/array_dataset.rb +0 -44
  18. data/lib/rom/association_set.rb +0 -49
  19. data/lib/rom/auto_curry.rb +0 -55
  20. data/lib/rom/command.rb +0 -494
  21. data/lib/rom/command_registry.rb +0 -148
  22. data/lib/rom/commands/class_interface.rb +0 -270
  23. data/lib/rom/commands/composite.rb +0 -53
  24. data/lib/rom/commands/create.rb +0 -13
  25. data/lib/rom/commands/delete.rb +0 -14
  26. data/lib/rom/commands/graph/builder.rb +0 -176
  27. data/lib/rom/commands/graph/class_interface.rb +0 -62
  28. data/lib/rom/commands/graph/input_evaluator.rb +0 -62
  29. data/lib/rom/commands/graph.rb +0 -95
  30. data/lib/rom/commands/lazy/create.rb +0 -23
  31. data/lib/rom/commands/lazy/delete.rb +0 -27
  32. data/lib/rom/commands/lazy/update.rb +0 -34
  33. data/lib/rom/commands/lazy.rb +0 -99
  34. data/lib/rom/commands/result.rb +0 -96
  35. data/lib/rom/commands/update.rb +0 -14
  36. data/lib/rom/commands.rb +0 -3
  37. data/lib/rom/configuration.rb +0 -90
  38. data/lib/rom/configuration_dsl/command.rb +0 -41
  39. data/lib/rom/configuration_dsl/command_dsl.rb +0 -35
  40. data/lib/rom/configuration_dsl/mapper.rb +0 -36
  41. data/lib/rom/configuration_dsl/mapper_dsl.rb +0 -43
  42. data/lib/rom/configuration_dsl/relation.rb +0 -26
  43. data/lib/rom/configuration_dsl.rb +0 -107
  44. data/lib/rom/configuration_plugin.rb +0 -17
  45. data/lib/rom/constants.rb +0 -36
  46. data/lib/rom/container.rb +0 -233
  47. data/lib/rom/create_container.rb +0 -60
  48. data/lib/rom/data_proxy.rb +0 -94
  49. data/lib/rom/enumerable_dataset.rb +0 -68
  50. data/lib/rom/environment.rb +0 -70
  51. data/lib/rom/gateway.rb +0 -196
  52. data/lib/rom/global/plugin_dsl.rb +0 -47
  53. data/lib/rom/global.rb +0 -58
  54. data/lib/rom/initializer.rb +0 -26
  55. data/lib/rom/lint/enumerable_dataset.rb +0 -54
  56. data/lib/rom/lint/gateway.rb +0 -120
  57. data/lib/rom/lint/linter.rb +0 -78
  58. data/lib/rom/lint/spec.rb +0 -20
  59. data/lib/rom/lint/test.rb +0 -98
  60. data/lib/rom/mapper_registry.rb +0 -35
  61. data/lib/rom/memory/commands.rb +0 -56
  62. data/lib/rom/memory/dataset.rb +0 -97
  63. data/lib/rom/memory/gateway.rb +0 -64
  64. data/lib/rom/memory/relation.rb +0 -62
  65. data/lib/rom/memory/schema.rb +0 -13
  66. data/lib/rom/memory/storage.rb +0 -59
  67. data/lib/rom/memory/types.rb +0 -9
  68. data/lib/rom/memory.rb +0 -4
  69. data/lib/rom/pipeline.rb +0 -122
  70. data/lib/rom/plugin.rb +0 -20
  71. data/lib/rom/plugin_base.rb +0 -40
  72. data/lib/rom/plugin_registry.rb +0 -173
  73. data/lib/rom/plugins/command/schema.rb +0 -37
  74. data/lib/rom/plugins/configuration/configuration_dsl.rb +0 -21
  75. data/lib/rom/plugins/relation/instrumentation.rb +0 -51
  76. data/lib/rom/plugins/relation/key_inference.rb +0 -48
  77. data/lib/rom/plugins/relation/registry_reader.rb +0 -33
  78. data/lib/rom/registry.rb +0 -50
  79. data/lib/rom/relation/class_interface.rb +0 -356
  80. data/lib/rom/relation/composite.rb +0 -46
  81. data/lib/rom/relation/curried.rb +0 -109
  82. data/lib/rom/relation/graph.rb +0 -125
  83. data/lib/rom/relation/loaded.rb +0 -127
  84. data/lib/rom/relation/materializable.rb +0 -66
  85. data/lib/rom/relation/name.rb +0 -102
  86. data/lib/rom/relation/view_dsl.rb +0 -64
  87. data/lib/rom/relation.rb +0 -250
  88. data/lib/rom/relation_registry.rb +0 -9
  89. data/lib/rom/schema/attribute.rb +0 -390
  90. data/lib/rom/schema/dsl.rb +0 -67
  91. data/lib/rom/schema.rb +0 -407
  92. data/lib/rom/setup/auto_registration.rb +0 -74
  93. data/lib/rom/setup/auto_registration_strategies/base.rb +0 -16
  94. data/lib/rom/setup/auto_registration_strategies/custom_namespace.rb +0 -63
  95. data/lib/rom/setup/auto_registration_strategies/no_namespace.rb +0 -20
  96. data/lib/rom/setup/auto_registration_strategies/with_namespace.rb +0 -18
  97. data/lib/rom/setup/finalize/finalize_commands.rb +0 -47
  98. data/lib/rom/setup/finalize/finalize_mappers.rb +0 -36
  99. data/lib/rom/setup/finalize/finalize_relations.rb +0 -83
  100. data/lib/rom/setup/finalize.rb +0 -152
  101. data/lib/rom/setup.rb +0 -65
  102. data/lib/rom/support/configurable.rb +0 -85
  103. data/lib/rom/transaction.rb +0 -24
  104. data/lib/rom/types.rb +0 -49
  105. data/log/.gitkeep +0 -0
  106. data/rakelib/benchmark.rake +0 -15
  107. data/rakelib/mutant.rake +0 -19
  108. data/rakelib/rubocop.rake +0 -18
  109. data/rom.gemspec +0 -25
  110. data/spec/fixtures/app/commands/create_user.rb +0 -2
  111. data/spec/fixtures/app/mappers/user_list.rb +0 -2
  112. data/spec/fixtures/app/my_commands/create_user.rb +0 -2
  113. data/spec/fixtures/app/my_mappers/user_list.rb +0 -2
  114. data/spec/fixtures/app/my_relations/users.rb +0 -2
  115. data/spec/fixtures/app/relations/users.rb +0 -2
  116. data/spec/fixtures/custom/commands/create_user.rb +0 -6
  117. data/spec/fixtures/custom/mappers/user_list.rb +0 -6
  118. data/spec/fixtures/custom/relations/users.rb +0 -6
  119. data/spec/fixtures/custom_namespace/commands/create_customer.rb +0 -8
  120. data/spec/fixtures/custom_namespace/mappers/customer_list.rb +0 -8
  121. data/spec/fixtures/custom_namespace/relations/customers.rb +0 -8
  122. data/spec/fixtures/lib/persistence/commands/create_user.rb +0 -6
  123. data/spec/fixtures/lib/persistence/mappers/user_list.rb +0 -6
  124. data/spec/fixtures/lib/persistence/my_commands/create_user.rb +0 -6
  125. data/spec/fixtures/lib/persistence/my_mappers/user_list.rb +0 -6
  126. data/spec/fixtures/lib/persistence/my_relations/users.rb +0 -6
  127. data/spec/fixtures/lib/persistence/relations/users.rb +0 -6
  128. data/spec/fixtures/wrong/commands/create_customer.rb +0 -8
  129. data/spec/fixtures/wrong/mappers/customer_list.rb +0 -8
  130. data/spec/fixtures/wrong/relations/customers.rb +0 -8
  131. data/spec/integration/command_registry_spec.rb +0 -47
  132. data/spec/integration/commands/create_spec.rb +0 -157
  133. data/spec/integration/commands/delete_spec.rb +0 -67
  134. data/spec/integration/commands/error_handling_spec.rb +0 -25
  135. data/spec/integration/commands/graph_builder_spec.rb +0 -213
  136. data/spec/integration/commands/graph_spec.rb +0 -294
  137. data/spec/integration/commands/update_spec.rb +0 -86
  138. data/spec/integration/commands_spec.rb +0 -67
  139. data/spec/integration/gateways/extending_relations_spec.rb +0 -58
  140. data/spec/integration/gateways/setting_logger_spec.rb +0 -34
  141. data/spec/integration/mappers/combine_spec.rb +0 -117
  142. data/spec/integration/mappers/deep_embedded_spec.rb +0 -44
  143. data/spec/integration/mappers/definition_dsl_spec.rb +0 -206
  144. data/spec/integration/mappers/embedded_spec.rb +0 -62
  145. data/spec/integration/mappers/exclude_spec.rb +0 -27
  146. data/spec/integration/mappers/fold_spec.rb +0 -71
  147. data/spec/integration/mappers/group_spec.rb +0 -163
  148. data/spec/integration/mappers/overwrite_attributes_value_spec.rb +0 -51
  149. data/spec/integration/mappers/prefix_separator_spec.rb +0 -52
  150. data/spec/integration/mappers/prefix_spec.rb +0 -48
  151. data/spec/integration/mappers/prefixing_attributes_spec.rb +0 -37
  152. data/spec/integration/mappers/registering_custom_mappers_spec.rb +0 -28
  153. data/spec/integration/mappers/renaming_attributes_spec.rb +0 -125
  154. data/spec/integration/mappers/reusing_mappers_spec.rb +0 -43
  155. data/spec/integration/mappers/step_spec.rb +0 -119
  156. data/spec/integration/mappers/symbolizing_attributes_spec.rb +0 -77
  157. data/spec/integration/mappers/unfold_spec.rb +0 -92
  158. data/spec/integration/mappers/ungroup_spec.rb +0 -126
  159. data/spec/integration/mappers/unwrap_spec.rb +0 -93
  160. data/spec/integration/mappers/wrap_spec.rb +0 -155
  161. data/spec/integration/memory/commands/create_spec.rb +0 -23
  162. data/spec/integration/memory/commands/delete_spec.rb +0 -23
  163. data/spec/integration/memory/commands/update_spec.rb +0 -23
  164. data/spec/integration/multi_env_spec.rb +0 -69
  165. data/spec/integration/multi_repo_spec.rb +0 -46
  166. data/spec/integration/relations/default_dataset_spec.rb +0 -38
  167. data/spec/integration/relations/inheritance_spec.rb +0 -37
  168. data/spec/integration/relations/reading_spec.rb +0 -169
  169. data/spec/integration/relations/registry_dsl_spec.rb +0 -45
  170. data/spec/integration/setup_spec.rb +0 -193
  171. data/spec/shared/command_behavior.rb +0 -28
  172. data/spec/shared/command_graph.rb +0 -54
  173. data/spec/shared/container.rb +0 -9
  174. data/spec/shared/enumerable_dataset.rb +0 -52
  175. data/spec/shared/gateway_only.rb +0 -6
  176. data/spec/shared/materializable.rb +0 -36
  177. data/spec/shared/no_container.rb +0 -16
  178. data/spec/shared/one_behavior.rb +0 -26
  179. data/spec/shared/proxy.rb +0 -0
  180. data/spec/shared/users_and_tasks.rb +0 -10
  181. data/spec/spec_helper.rb +0 -59
  182. data/spec/support/constant_leak_finder.rb +0 -14
  183. data/spec/support/mutant.rb +0 -10
  184. data/spec/support/schema.rb +0 -14
  185. data/spec/support/types.rb +0 -5
  186. data/spec/test/memory_repository_lint_test.rb +0 -27
  187. data/spec/unit/rom/array_dataset_spec.rb +0 -59
  188. data/spec/unit/rom/association_set_spec.rb +0 -48
  189. data/spec/unit/rom/auto_curry_spec.rb +0 -71
  190. data/spec/unit/rom/commands/graph_spec.rb +0 -192
  191. data/spec/unit/rom/commands/lazy_spec.rb +0 -310
  192. data/spec/unit/rom/commands/pre_and_post_processors_spec.rb +0 -343
  193. data/spec/unit/rom/commands/result_spec.rb +0 -70
  194. data/spec/unit/rom/commands_spec.rb +0 -188
  195. data/spec/unit/rom/configurable_spec.rb +0 -49
  196. data/spec/unit/rom/configuration_spec.rb +0 -61
  197. data/spec/unit/rom/container_spec.rb +0 -109
  198. data/spec/unit/rom/create_container_spec.rb +0 -151
  199. data/spec/unit/rom/enumerable_dataset_spec.rb +0 -15
  200. data/spec/unit/rom/environment_spec.rb +0 -123
  201. data/spec/unit/rom/gateway_spec.rb +0 -146
  202. data/spec/unit/rom/mapper_registry_spec.rb +0 -25
  203. data/spec/unit/rom/memory/commands_spec.rb +0 -43
  204. data/spec/unit/rom/memory/dataset_spec.rb +0 -31
  205. data/spec/unit/rom/memory/gateway_spec.rb +0 -12
  206. data/spec/unit/rom/memory/inheritance_spec.rb +0 -32
  207. data/spec/unit/rom/memory/relation_spec.rb +0 -121
  208. data/spec/unit/rom/memory/storage_spec.rb +0 -45
  209. data/spec/unit/rom/plugin_spec.rb +0 -150
  210. data/spec/unit/rom/plugins/command/schema_spec.rb +0 -66
  211. data/spec/unit/rom/plugins/relation/instrumentation_spec.rb +0 -44
  212. data/spec/unit/rom/plugins/relation/key_inference_spec.rb +0 -85
  213. data/spec/unit/rom/registry_spec.rb +0 -86
  214. data/spec/unit/rom/relation/attribute_reader_spec.rb +0 -17
  215. data/spec/unit/rom/relation/call_spec.rb +0 -51
  216. data/spec/unit/rom/relation/composite_spec.rb +0 -106
  217. data/spec/unit/rom/relation/curried_spec.rb +0 -67
  218. data/spec/unit/rom/relation/graph_spec.rb +0 -106
  219. data/spec/unit/rom/relation/lazy/combine_spec.rb +0 -165
  220. data/spec/unit/rom/relation/lazy/graph_spec.rb +0 -165
  221. data/spec/unit/rom/relation/lazy_spec.rb +0 -214
  222. data/spec/unit/rom/relation/loaded_spec.rb +0 -53
  223. data/spec/unit/rom/relation/name_spec.rb +0 -58
  224. data/spec/unit/rom/relation/output_schema_spec.rb +0 -28
  225. data/spec/unit/rom/relation/schema_spec.rb +0 -259
  226. data/spec/unit/rom/relation/view_spec.rb +0 -158
  227. data/spec/unit/rom/relation_spec.rb +0 -252
  228. data/spec/unit/rom/schema/accessing_attributes_spec.rb +0 -52
  229. data/spec/unit/rom/schema/append_spec.rb +0 -17
  230. data/spec/unit/rom/schema/exclude_spec.rb +0 -15
  231. data/spec/unit/rom/schema/finalize_spec.rb +0 -59
  232. data/spec/unit/rom/schema/key_predicate_spec.rb +0 -15
  233. data/spec/unit/rom/schema/merge_spec.rb +0 -17
  234. data/spec/unit/rom/schema/prefix_spec.rb +0 -16
  235. data/spec/unit/rom/schema/project_spec.rb +0 -15
  236. data/spec/unit/rom/schema/rename_spec.rb +0 -22
  237. data/spec/unit/rom/schema/type_spec.rb +0 -49
  238. data/spec/unit/rom/schema/uniq_spec.rb +0 -21
  239. data/spec/unit/rom/schema/wrap_spec.rb +0 -18
  240. data/spec/unit/rom/schema_spec.rb +0 -10
  241. data/spec/unit/rom/setup/auto_registration_spec.rb +0 -214
data/Rakefile DELETED
@@ -1,28 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
- require "rake/testtask"
4
-
5
- RSpec::Core::RakeTask.new(:spec)
6
- task default: [:ci]
7
-
8
- desc 'Run specs in isolation'
9
- task :"spec:isolation" do
10
- FileList["spec/**/*_spec.rb"].each do |spec|
11
- sh "COVERAGE=false bundle exec rspec #{spec}"
12
- end
13
- end
14
-
15
- if RUBY_ENGINE != 'ruby'
16
- desc "Run CI tasks"
17
- task ci: [:spec, :lint]
18
- else
19
- desc "Run CI tasks"
20
- task ci: [:spec, :lint, :"spec:isolation"]
21
- end
22
-
23
- Rake::TestTask.new(:lint) do |test|
24
- test.description = "Run adapter lint tests against memory adapter"
25
- test.test_files = FileList.new('spec/test/*_test.rb')
26
- test.libs << 'test'
27
- test.verbose = true
28
- end
@@ -1,44 +0,0 @@
1
- require 'rom/initializer'
2
- require 'rom/enumerable_dataset'
3
-
4
- module ROM
5
- # A helper module that adds data-proxy behavior to an array-like object
6
- #
7
- # @see EnumerableDataset
8
- #
9
- # @api public
10
- module ArrayDataset
11
- extend DataProxy::ClassMethods
12
- include EnumerableDataset
13
-
14
- # Extends the class with data-proxy behavior
15
- #
16
- # @api private
17
- def self.included(klass)
18
- klass.class_eval do
19
- extend Initializer
20
- include DataProxy
21
-
22
- param :data
23
- end
24
- end
25
-
26
- forward(
27
- :*, :+, :-, :compact, :compact!, :flatten, :flatten!, :length, :pop,
28
- :reverse, :reverse!, :sample, :size, :shift, :shuffle, :shuffle!,
29
- :slice, :slice!, :sort!, :uniq, :uniq!, :unshift, :values_at
30
- )
31
-
32
- [
33
- :map!, :combination, :cycle, :delete_if, :keep_if, :permutation, :reject!,
34
- :select!, :sort_by!
35
- ].each do |method|
36
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
37
- def #{method}(*args, &block)
38
- return to_enum unless block
39
- self.class.new(data.send(:#{method}, *args, &block), options)
40
- end
41
- RUBY
42
- end
43
- end
44
- end
@@ -1,49 +0,0 @@
1
- require 'dry/core/deprecations'
2
- require 'dry/core/inflector'
3
-
4
- require 'rom/registry'
5
-
6
- module ROM
7
- class AssociationSet < ROM::Registry
8
- # @api private
9
- def try(name, &block)
10
- key = name.to_sym
11
-
12
- if key?(key) || key?(singularize(key))
13
- yield(self[key])
14
- else
15
- msg = <<-STR
16
- Key inference will be removed in rom 4.0. You need to define :#{key} association.
17
- => Called at:
18
- #{caller.join("\n")}
19
- STR
20
-
21
- Dry::Core::Deprecations.warn(msg)
22
-
23
- false
24
- end
25
- end
26
-
27
- # @api private
28
- def [](name)
29
- key = name.to_sym
30
-
31
- if key?(key)
32
- super
33
- else
34
- sk = singularize(key)
35
-
36
- if key?(sk)
37
- super(sk)
38
- else
39
- super
40
- end
41
- end
42
- end
43
-
44
- # @api private
45
- def singularize(key)
46
- Dry::Core::Inflector.singularize(key).to_sym
47
- end
48
- end
49
- end
@@ -1,55 +0,0 @@
1
- module ROM
2
- module AutoCurry
3
- def self.extended(klass)
4
- klass.define_singleton_method(:method_added) do |name|
5
- return if auto_curry_busy?
6
- auto_curry_guard { auto_curry(name) }
7
- super(name)
8
- end
9
- end
10
-
11
- def auto_curry_guard
12
- @__auto_curry_busy__ = true
13
- yield
14
- ensure
15
- @__auto_curry_busy__ = false
16
- end
17
-
18
- def auto_curry_busy?
19
- @__auto_curry_busy__ ||= false
20
- end
21
-
22
- def auto_curried_methods
23
- @__auto_curried_methods__ ||= []
24
- end
25
-
26
- def auto_curry(name, &block)
27
- arity = instance_method(name).arity
28
-
29
- return unless public_instance_methods.include?(name) && arity != 0
30
-
31
- mod = Module.new
32
-
33
- mod.module_eval do
34
- define_method(name) do |*args, &mblock|
35
- response =
36
- if arity < 0 || arity == args.size
37
- super(*args, &mblock)
38
- else
39
- self.class.curried.new(self, name: name, curry_args: args, arity: arity)
40
- end
41
-
42
- if block
43
- response.instance_exec(&block)
44
- else
45
- response
46
- end
47
- end
48
- end
49
-
50
- auto_curried_methods << name
51
-
52
- prepend(mod)
53
- end
54
- end
55
- end
data/lib/rom/command.rb DELETED
@@ -1,494 +0,0 @@
1
- require 'dry/core/deprecations'
2
- require 'dry/core/class_attributes'
3
-
4
- require 'rom/types'
5
- require 'rom/initializer'
6
- require 'rom/pipeline'
7
-
8
- require 'rom/commands/class_interface'
9
- require 'rom/commands/composite'
10
- require 'rom/commands/graph'
11
- require 'rom/commands/lazy'
12
-
13
- module ROM
14
- # Abstract command class
15
- #
16
- # Provides a constructor accepting relation with options and basic behavior
17
- # for calling, currying and composing commands.
18
- #
19
- # Typically command subclasses should inherit from specialized
20
- # Create/Update/Delete, not this one.
21
- #
22
- # @abstract
23
- #
24
- # @api public
25
- class Command
26
- extend Initializer
27
- include Dry::Equalizer(:relation, :options)
28
- include Commands
29
- include Pipeline::Operator
30
-
31
- extend Dry::Core::ClassAttributes
32
- extend Dry::Core::Deprecations[:rom]
33
- extend ClassInterface
34
-
35
- # @!method self.adapter
36
- # Get or set adapter identifier
37
- #
38
- # @overload adapter
39
- # Get adapter identifier
40
- #
41
- # @example
42
- # ROM::Memory::Commands::Create.adapter
43
- # # => :memory
44
- #
45
- # @return [Symbol]
46
- #
47
- # @overload adapter(identifier)
48
- # Set adapter identifier. This must always match actual adapter identifier
49
- # that was used to register an adapter.
50
- #
51
- # @example
52
- # module MyAdapter
53
- # class CreateCommand < ROM::Commands::Memory::Create
54
- # adapter :my_adapter
55
- # end
56
- # end
57
- #
58
- # @api public
59
- defines :adapter
60
-
61
- # @!method self.relation
62
- # Get or set relation identifier
63
- #
64
- # @overload relation
65
- # Get relation identifier
66
- #
67
- # @example
68
- # class CreateUser < ROM::Commands::Create[:memory]
69
- # relation :users
70
- # end
71
- #
72
- # CreateUser.relation
73
- # # => :users
74
- #
75
- # @return [Symbol]
76
- #
77
- # @overload relation(identifier)
78
- # Set relation identifier.
79
- #
80
- # @example
81
- # class CreateUser < ROM::Commands::Create[:memory]
82
- # relation :users
83
- # end
84
- #
85
- # @api public
86
- defines :relation
87
-
88
- # @!method self.relation
89
- # Get or set result type
90
- #
91
- # @overload result
92
- # Get result type
93
- #
94
- # @example
95
- # class CreateUser < ROM::Commands::Create[:memory]
96
- # result :one
97
- # end
98
- #
99
- # CreateUser.result
100
- # # => :one
101
- #
102
- # @return [Symbol]
103
- #
104
- # @overload relation(identifier)
105
- # Set result type
106
- #
107
- # @example
108
- # class CreateUser < ROM::Commands::Create[:memory]
109
- # result :one
110
- # end
111
- #
112
- # @api public
113
- defines :result
114
-
115
- # @!method self.relation
116
- # Get or set input processing function. This is typically set during setup
117
- # to relation's input_schema
118
- #
119
- # @overload input
120
- # Get input processing function
121
- #
122
- # @example
123
- # class CreateUser < ROM::Commands::Create[:memory]
124
- # input -> tuple { .. }
125
- # end
126
- #
127
- # CreateUser.input
128
- # # Your custom function
129
- #
130
- # @return [Proc,#call]
131
- #
132
- # @overload input(identifier)
133
- # Set input processing function
134
- #
135
- # @example
136
- # class CreateUser < ROM::Commands::Create[:memory]
137
- # input -> tuple { .. }
138
- # end
139
- #
140
- # @api public
141
- defines :input
142
-
143
- # @!method self.register_as
144
- # Get or set identifier that should be used to register a command in a container
145
- #
146
- # @overload register_as
147
- # Get registration identifier
148
- #
149
- # @example
150
- # class CreateUser < ROM::Commands::Create[:memory]
151
- # register_as :create_user
152
- # end
153
- #
154
- # CreateUser.register_as
155
- # # => :create_user
156
- #
157
- # @return [Symbol]
158
- #
159
- # @overload register_as(identifier)
160
- # Set registration identifier
161
- #
162
- # @example
163
- # class CreateUser < ROM::Commands::Create[:memory]
164
- # register_as :create_user
165
- # end
166
- #
167
- # @api public
168
- defines :register_as
169
-
170
- # @!method self.restrictable
171
- # @overload restrictable
172
- # Check if a command class is restrictable
173
- #
174
- # @example
175
- # class UpdateUser < ROM::Commands::Update[:memory]
176
- # restrictable true
177
- # end
178
- #
179
- # CreateUser.restrictable
180
- # # => true
181
- #
182
- # @return [FalseClass, TrueClass]
183
- #
184
- # @overload restrictable(value)
185
- # Set if a command is restrictable
186
- #
187
- # @example
188
- # class UpdateUser < ROM::Commands::Update[:memory]
189
- # restrictable true
190
- # end
191
- #
192
- # @api public
193
- defines :restrictable
194
-
195
- # @!attribute [r] relation
196
- # @return [Relation] Command's relation
197
- param :relation
198
-
199
- CommandType = Types::Strict::Symbol.enum(:create, :update, :delete)
200
- Result = Types::Strict::Symbol.enum(:one, :many)
201
-
202
- # @!attribute [r] type
203
- # @return [Symbol] The command type, one of :create, :update or :delete
204
- option :type, type: CommandType, optional: true
205
-
206
- # @!attribute [r] source
207
- # @return [Relation] The source relation
208
- option :source, default: -> { relation }
209
-
210
- # @!attribute [r] result
211
- # @return [Symbol] Result type, either :one or :many
212
- option :result, type: Result
213
-
214
- # @!attribute [r] input
215
- # @return [Proc, #call] Tuple processing function, typically uses Relation#input_schema
216
- option :input
217
-
218
- # @!attribute [r] curry_args
219
- # @return [Array] Curried args
220
- option :curry_args, default: -> { EMPTY_ARRAY }
221
-
222
- # @!attribute [r] before
223
- # @return [Array<Hash>] An array with before hooks configuration
224
- option :before, Types::Coercible::Array, reader: false, default: -> { self.class.before }
225
-
226
- # @!attribute [r] after
227
- # @return [Array<Hash>] An array with after hooks configuration
228
- option :after, Types::Coercible::Array, reader: false, default: -> { self.class.after }
229
-
230
- input Hash
231
- result :many
232
-
233
- # Return name of this command's relation
234
- #
235
- # @return [ROM::Relation::Name]
236
- #
237
- # @api public
238
- def name
239
- relation.name
240
- end
241
-
242
- # Return gateway of this command's relation
243
- #
244
- # @return [Symbol]
245
- #
246
- # @api public
247
- def gateway
248
- relation.gateway
249
- end
250
-
251
- # Execute the command
252
- #
253
- # @abstract
254
- #
255
- # @return [Array] an array with inserted tuples
256
- #
257
- # @api private
258
- def execute(*)
259
- raise(
260
- NotImplementedError,
261
- "#{self.class}##{__method__} must be implemented"
262
- )
263
- end
264
-
265
- # Call the command and return one or many tuples
266
- #
267
- # This method will apply before/after hooks automatically
268
- #
269
- # @api public
270
- def call(*args, &block)
271
- tuples =
272
- if hooks?
273
- prepared =
274
- if curried?
275
- apply_hooks(before_hooks, *(curry_args + args))
276
- else
277
- apply_hooks(before_hooks, *args)
278
- end
279
-
280
- result = prepared ? execute(prepared, &block) : execute(&block)
281
-
282
- if curried?
283
- if args.size > 0
284
- apply_hooks(after_hooks, result, *args)
285
- elsif curry_args.size > 1
286
- apply_hooks(after_hooks, result, curry_args[1])
287
- else
288
- apply_hooks(after_hooks, result)
289
- end
290
- else
291
- apply_hooks(after_hooks, result, *args[1..args.size-1])
292
- end
293
- else
294
- execute(*(curry_args + args), &block)
295
- end
296
-
297
- if one?
298
- tuples.first
299
- else
300
- tuples
301
- end
302
- end
303
- alias_method :[], :call
304
-
305
- # Curry this command with provided args
306
- #
307
- # Curried command can be called without args. If argument is a graph input processor,
308
- # lazy command will be returned, which is used for handling nested input hashes.
309
- #
310
- # @return [Command, Lazy]
311
- #
312
- # @api public
313
- def curry(*args)
314
- if curry_args.empty? && args.first.is_a?(Graph::InputEvaluator)
315
- Lazy[self].new(self, *args)
316
- else
317
- self.class.build(relation, **options, curry_args: args)
318
- end
319
- end
320
-
321
- # @api public
322
- def with(*args)
323
- self.class.warn "Command#with will change its behavior in rom 4.0. Use Command#curry instead.\n\n#{caller[0..5].join("\n")}"
324
- curry(*args)
325
- end
326
-
327
- # Compose this command with other commands
328
- #
329
- # Composed commands can handle nested input
330
- #
331
- # @return [Command::Graph]
332
- #
333
- # @api public
334
- def combine(*others)
335
- Graph.new(self, others)
336
- end
337
-
338
- # Check if this command is curried
339
- #
340
- # @return [TrueClass, FalseClass]
341
- #
342
- # @api public
343
- def curried?
344
- curry_args.size > 0
345
- end
346
-
347
- # Return a new command with new options
348
- #
349
- # @param [Hash] new_opts A hash with new options
350
- #
351
- # @return [Command]
352
- #
353
- # @api public
354
- def with_opts(new_opts)
355
- self.class.new(relation, options.merge(new_opts))
356
- end
357
-
358
- # Return a new command with appended before hooks
359
- #
360
- # @param [Array<Hash>] hooks A list of before hooks configurations
361
- #
362
- # @return [Command]
363
- #
364
- # @api public
365
- def before(*hooks)
366
- self.class.new(relation, **options, before: before_hooks + hooks)
367
- end
368
-
369
- # Return a new command with appended after hooks
370
- #
371
- # @param [Array<Hash>] hooks A list of after hooks configurations
372
- #
373
- # @return [Command]
374
- #
375
- # @api public
376
- def after(*hooks)
377
- self.class.new(relation, **options, after: after_hooks + hooks)
378
- end
379
-
380
- # List of before hooks
381
- #
382
- # @return [Array]
383
- #
384
- # @api public
385
- def before_hooks
386
- options[:before]
387
- end
388
-
389
- # List of after hooks
390
- #
391
- # @return [Array]
392
- #
393
- # @api public
394
- def after_hooks
395
- options[:after]
396
- end
397
-
398
- # Return a new command with other source relation
399
- #
400
- # This can be used to restrict command with a specific relation
401
- #
402
- # @return [Command]
403
- #
404
- # @api public
405
- def new(new_relation)
406
- self.class.build(new_relation, **options, source: relation)
407
- end
408
-
409
- # Check if this command has any hooks
410
- #
411
- # @api private
412
- def hooks?
413
- before_hooks.size > 0 || after_hooks.size > 0
414
- end
415
-
416
- # Check if this command is lazy
417
- #
418
- # @return [false]
419
- #
420
- # @api private
421
- def lazy?
422
- false
423
- end
424
-
425
- # Check if this command is a graph
426
- #
427
- # @return [false]
428
- #
429
- # @api private
430
- def graph?
431
- false
432
- end
433
-
434
- # Check if this command returns a single tuple
435
- #
436
- # @return [TrueClass,FalseClass]
437
- #
438
- # @api private
439
- def one?
440
- result.equal?(:one)
441
- end
442
-
443
- # Check if this command returns many tuples
444
- #
445
- # @return [TrueClass,FalseClass]
446
- #
447
- # @api private
448
- def many?
449
- result.equal?(:many)
450
- end
451
-
452
- private
453
-
454
- # Hook called by Pipeline to get composite class for commands
455
- #
456
- # @return [Class]
457
- #
458
- # @api private
459
- def composite_class
460
- Command::Composite
461
- end
462
-
463
- # Apply provided hooks
464
- #
465
- # Used by #call
466
- #
467
- # @return [Array<Hash>]
468
- #
469
- # @api private
470
- def apply_hooks(hooks, tuples, *args)
471
- hooks.reduce(tuples) do |a, e|
472
- if e.is_a?(Hash)
473
- hook_meth, hook_args = e.to_a.flatten(1)
474
- __send__(hook_meth, a, *args, **hook_args)
475
- else
476
- __send__(e, a, *args)
477
- end
478
- end
479
- end
480
-
481
- # Pipes a dataset through command's relation
482
- #
483
- # @return [Array]
484
- #
485
- # @api private
486
- def wrap_dataset(dataset)
487
- if relation.is_a?(Relation::Composite)
488
- relation.new(dataset).to_a
489
- else
490
- dataset
491
- end
492
- end
493
- end
494
- end