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
@@ -1,148 +0,0 @@
1
- require 'dry/core/deprecations'
2
-
3
- require 'rom/types'
4
- require 'rom/commands/result'
5
-
6
- module ROM
7
- # Specialized registry class for commands
8
- #
9
- # @api public
10
- class CommandRegistry
11
- extend Initializer
12
- extend Dry::Core::Deprecations[:rom]
13
-
14
- include Commands
15
-
16
- CommandNotFoundError = Class.new(KeyError)
17
- RegistryType = Types.Definition(Registry) | Types.Constructor(Registry) { |r| Registry.new(r, self.class.name) }
18
-
19
- # Name of the relation from which commands are under
20
- #
21
- # @return [String]
22
- #
23
- # @api private
24
- param :relation_name
25
-
26
- # Internal command registry
27
- #
28
- # @return [Registry]
29
- #
30
- # @api private
31
- param :registry, type: RegistryType
32
-
33
- option :mappers, optional: true
34
- option :mapper, optional: true
35
-
36
- # Try to execute a command in a block
37
- #
38
- # @yield [command] Passes command to the block
39
- #
40
- # @example
41
- #
42
- # rom.command(:users).try { create(name: 'Jane') }
43
- # rom.command(:users).try { update(:by_id, 1).call(name: 'Jane Doe') }
44
- # rom.command(:users).try { delete(:by_id, 1) }
45
- #
46
- # @return [Commands::Result]
47
- #
48
- # @api public
49
- def try(&block)
50
- self.class.warn "CommandRegistry#try will be removed in rom 4.0"
51
-
52
- response = block.call
53
-
54
- if response.is_a?(Command) || response.is_a?(Composite)
55
- try { response.call }
56
- else
57
- Result::Success.new(response)
58
- end
59
- rescue CommandError => e
60
- Result::Failure.new(e)
61
- end
62
-
63
- # Return a command from the registry
64
- #
65
- # If mapper is set command will be turned into a composite command with
66
- # auto-mapping
67
- #
68
- # @example
69
- # create_user = rom.command(:users)[:create]
70
- # create_user[name: 'Jane']
71
- #
72
- # # with mapping, assuming :entity mapper is registered for :users relation
73
- # create_user = rom.command(:users).as(:entity)[:create]
74
- # create_user[name: 'Jane'] # => result is send through :entity mapper
75
- #
76
- # @param [Symbol] name The name of a registered command
77
- #
78
- # @return [Command,Command::Composite]
79
- #
80
- # @api public
81
- def [](name)
82
- command = fetch_command(name)
83
- mapper = options[:mapper]
84
-
85
- if mapper
86
- command.curry >> mapper
87
- else
88
- command
89
- end
90
- end
91
-
92
- # Specify a mapper that should be used for commands from this registry
93
- #
94
- # @example
95
- # entity_commands = rom.command(:users).as(:entity)
96
- #
97
- #
98
- # @param [Symbol] mapper_name The name of a registered mapper
99
- #
100
- # @return [CommandRegistry]
101
- #
102
- # @api public
103
- def as(mapper_name)
104
- with(mapper: mappers[mapper_name])
105
- end
106
-
107
- # Return new instance of this registry with updated options
108
- #
109
- # @return [CommandRegistry]
110
- #
111
- # @api private
112
- def with(new_options)
113
- self.class.new(relation_name, registry, options.merge(new_options))
114
- end
115
-
116
- private
117
-
118
- # Fetch a command from the registry and raise a specialized error
119
- # in case the command is not found
120
- #
121
- # @api private
122
- def fetch_command(name)
123
- registry.fetch(name) do
124
- raise CommandNotFoundError.new(
125
- "There is no :#{name} command for :#{relation_name} relation"
126
- )
127
- end
128
- end
129
-
130
- # Allow checking if a certain command is available using dot-notation
131
- #
132
- # @api private
133
- def respond_to_missing?(name, include_private = false)
134
- registry.key?(name) || super
135
- end
136
-
137
- # Allow retrieving commands using dot-notation
138
- #
139
- # @api private
140
- def method_missing(name, *)
141
- if registry.key?(name)
142
- self[name]
143
- else
144
- super
145
- end
146
- end
147
- end
148
- end
@@ -1,270 +0,0 @@
1
- require 'dry/core/class_builder'
2
- require 'dry/core/inflector'
3
-
4
- module ROM
5
- # Base command class with factory class-level interface and setup-related logic
6
- #
7
- # @private
8
- class Command
9
- module ClassInterface
10
- # This hook sets up default class state
11
- #
12
- # @api private
13
- def inherited(klass)
14
- super
15
- klass.instance_variable_set(:'@before', before.dup)
16
- klass.instance_variable_set(:'@after', after.dup)
17
- end
18
-
19
- # Sets up the base class
20
- #
21
- # @api private
22
- def self.extended(klass)
23
- super
24
- klass.set_hooks(:before, [])
25
- klass.set_hooks(:after, [])
26
- end
27
-
28
- # Return adapter specific sub-class based on the adapter identifier
29
- #
30
- # This is a syntax sugar to make things consistent
31
- #
32
- # @example
33
- # ROM::Commands::Create[:memory]
34
- # # => ROM::Memory::Commands::Create
35
- #
36
- # @param [Symbol] adapter identifier
37
- #
38
- # @return [Class]
39
- #
40
- # @api public
41
- def [](adapter)
42
- adapter_namespace(adapter).const_get(Dry::Core::Inflector.demodulize(name))
43
- end
44
-
45
- # Return namespaces that contains command subclasses of a specific adapter
46
- #
47
- # @param [Symbol] adapter identifier
48
- #
49
- # @return [Module]
50
- #
51
- # @api private
52
- def adapter_namespace(adapter)
53
- ROM.adapters.fetch(adapter).const_get(:Commands)
54
- rescue KeyError
55
- raise AdapterNotPresentError.new(adapter, :relation)
56
- end
57
-
58
- # Build a command class for a specific relation with options
59
- #
60
- # @example
61
- # class CreateUser < ROM::Commands::Create[:memory]
62
- # end
63
- #
64
- # command = CreateUser.build(rom.relations[:users])
65
- #
66
- # @param [Relation] relation
67
- # @param [Hash] options
68
- #
69
- # @return [Command]
70
- #
71
- # @api public
72
- def build(relation, options = EMPTY_HASH)
73
- new(relation, self.options.merge(options))
74
- end
75
-
76
- # Create a command class with a specific type
77
- #
78
- # @param [Symbol] command name
79
- # @param [Class] parent class
80
- #
81
- # @yield [Class] create class
82
- #
83
- # @return [Class, Object] return result of the block if it was provided
84
- #
85
- # @api public
86
- def create_class(name, type, &block)
87
- klass = Dry::Core::ClassBuilder
88
- .new(name: "#{Dry::Core::Inflector.classify(type)}[:#{name}]", parent: type)
89
- .call
90
-
91
- if block
92
- yield(klass)
93
- else
94
- klass
95
- end
96
- end
97
-
98
- # Use a configured plugin in this relation
99
- #
100
- # @example
101
- # class CreateUser < ROM::Commands::Create[:memory]
102
- # use :pagintion
103
- #
104
- # per_page 30
105
- # end
106
- #
107
- # @param [Symbol] plugin
108
- # @param [Hash] options
109
- # @option options [Symbol] :adapter (:default) first adapter to check for plugin
110
- #
111
- # @api public
112
- def use(plugin, _options = EMPTY_HASH)
113
- ROM.plugin_registry.commands.fetch(plugin, adapter).apply_to(self)
114
- end
115
-
116
- # Extend a command class with relation view methods
117
- #
118
- # @param [Relation]
119
- #
120
- # @return [Class]
121
- #
122
- # @api public
123
- def extend_for_relation(relation)
124
- include(relation_methods_mod(relation.class))
125
- end
126
-
127
- # Set before-execute hooks
128
- #
129
- # @overload before(hook)
130
- # Set an before hook as a method name
131
- #
132
- # @example
133
- # class CreateUser < ROM::Commands::Create[:sql]
134
- # relation :users
135
- # register_as :create
136
- #
137
- # before :my_hook
138
- #
139
- # def my_hook(tuple, *)
140
- # puts "hook called#
141
- # end
142
- # end
143
- #
144
- # @overload before(hook_opts)
145
- # Set an before hook as a method name with arguments
146
- #
147
- # @example
148
- # class CreateUser < ROM::Commands::Create[:sql]
149
- # relation :users
150
- # register_as :create
151
- #
152
- # before my_hook: { arg1: 1, arg1: 2 }
153
- #
154
- # def my_hook(tuple, arg1:, arg2:)
155
- # puts "hook called with args: #{arg1} and #{arg2}"
156
- # end
157
- # end
158
- #
159
- # @param [Hash<Symbol=>Hash>] hook Options with method name and pre-set args
160
- #
161
- # @return [Array<Hash, Symbol>] A list of all configured before hooks
162
- #
163
- # @api public
164
- def before(*hooks)
165
- if hooks.size > 0
166
- set_hooks(:before, hooks)
167
- else
168
- @before
169
- end
170
- end
171
-
172
- # Set after-execute hooks
173
- #
174
- # @overload after(hook)
175
- # Set an after hook as a method name
176
- #
177
- # @example
178
- # class CreateUser < ROM::Commands::Create[:sql]
179
- # relation :users
180
- # register_as :create
181
- #
182
- # after :my_hook
183
- #
184
- # def my_hook(tuple, *)
185
- # puts "hook called#
186
- # end
187
- # end
188
- #
189
- # @overload after(hook_opts)
190
- # Set an after hook as a method name with arguments
191
- #
192
- # @example
193
- # class CreateUser < ROM::Commands::Create[:sql]
194
- # relation :users
195
- # register_as :create
196
- #
197
- # after my_hook: { arg1: 1, arg1: 2 }
198
- #
199
- # def my_hook(tuple, arg1:, arg2:)
200
- # puts "hook called with args: #{arg1} and #{arg2}"
201
- # end
202
- # end
203
- #
204
- # @param [Hash<Symbol=>Hash>] hook Options with method name and pre-set args
205
- #
206
- # @return [Array<Hash, Symbol>] A list of all configured after hooks
207
- #
208
- # @api public
209
- def after(*hooks)
210
- if hooks.size > 0
211
- set_hooks(:after, hooks)
212
- else
213
- @after
214
- end
215
- end
216
-
217
- # Set new or more hooks
218
- #
219
- # @api private
220
- def set_hooks(type, hooks)
221
- ivar = :"@#{type}"
222
-
223
- if instance_variable_defined?(ivar)
224
- instance_variable_get(ivar).concat(hooks)
225
- else
226
- instance_variable_set(ivar, hooks)
227
- end
228
- end
229
-
230
- # Return default name of the command class based on its name
231
- #
232
- # During setup phase this is used by defalut as `register_as` option
233
- #
234
- # @return [Symbol]
235
- #
236
- # @api private
237
- def default_name
238
- Dry::Core::Inflector.underscore(Dry::Core::Inflector.demodulize(name)).to_sym
239
- end
240
-
241
- # Return default options based on class macros
242
- #
243
- # @return [Hash]
244
- #
245
- # @api private
246
- def options
247
- { input: input, result: result, before: before, after: after }
248
- end
249
-
250
- # @api private
251
- def relation_methods_mod(relation_class)
252
- Module.new do
253
- relation_class.view_methods.each do |meth|
254
- module_eval <<-RUBY, __FILE__, __LINE__ + 1
255
- def #{meth}(*args)
256
- response = relation.public_send(:#{meth}, *args)
257
-
258
- if response.is_a?(relation.class)
259
- new(response)
260
- else
261
- response
262
- end
263
- end
264
- RUBY
265
- end
266
- end
267
- end
268
- end
269
- end
270
- end
@@ -1,53 +0,0 @@
1
- require 'rom/pipeline'
2
-
3
- module ROM
4
- module Commands
5
- # Composite command that consists of left and right commands
6
- #
7
- # @api public
8
- class Composite < Pipeline::Composite
9
- # Calls the composite command
10
- #
11
- # Right command is called with a result from the left one
12
- #
13
- # @return [Object]
14
- #
15
- # @api public
16
- def call(*args)
17
- response = left.call(*args)
18
-
19
- if response.nil? || (many? && response.size == 0)
20
- return one? ? nil : EMPTY_ARRAY
21
- end
22
-
23
- if one? && !graph?
24
- if right.is_a?(Command) || right.is_a?(Commands::Composite)
25
- right.call([response].first)
26
- else
27
- right.call([response]).first
28
- end
29
- elsif one? && graph?
30
- right.call(response).first
31
- else
32
- right.call(response)
33
- end
34
- end
35
- alias_method :[], :call
36
-
37
- # @api private
38
- def graph?
39
- left.is_a?(Graph)
40
- end
41
-
42
- # @api private
43
- def result
44
- left.result
45
- end
46
-
47
- # @api private
48
- def decorate?(response)
49
- super || response.is_a?(Graph)
50
- end
51
- end
52
- end
53
- end
@@ -1,13 +0,0 @@
1
- require 'rom/command'
2
-
3
- module ROM
4
- module Commands
5
- # Create command
6
- #
7
- # This command inserts a new tuple into a relation
8
- #
9
- # @abstract
10
- class Create < Command
11
- end
12
- end
13
- end
@@ -1,14 +0,0 @@
1
- require 'rom/command'
2
-
3
- module ROM
4
- module Commands
5
- # Delete command
6
- #
7
- # This command removes tuples from its target relation
8
- #
9
- # @abstract
10
- class Delete < Command
11
- restrictable true
12
- end
13
- end
14
- end
@@ -1,176 +0,0 @@
1
- module ROM
2
- module Commands
3
- class Graph
4
- # Command graph builder DSL
5
- #
6
- # @api public
7
- class Builder
8
- # @api private
9
- UnspecifiedRelationError = Class.new(StandardError)
10
- DoubleRestrictionError = Class.new(StandardError)
11
-
12
- # @api private
13
- class Node
14
- # @api private
15
- Restriction = Struct.new(:relation, :proc)
16
-
17
- # @api private
18
- Command = Struct.new(:name, :relation, :key, :proc)
19
-
20
- # @api private
21
- def initialize
22
- @nodes = []
23
- end
24
-
25
- # @api public
26
- def to_ast
27
- []
28
- end
29
-
30
- # Any missing method called on this is treated as a ROM command
31
- #
32
- # @api private
33
- def method_missing(*args, &block)
34
- command(*args, &block)
35
- end
36
-
37
- # @api public
38
- def command(name, relation = nil, key = nil, proc = nil, &block)
39
- if relation.is_a?(Hash)
40
- key, relation = relation.to_a.first
41
- end
42
-
43
- raise UnspecifiedRelationError if relation.nil?
44
-
45
- if relation.is_a?(RestrictionNode)
46
- RestrictionNode.new(relation.restriction).command(name, from: key, &block)
47
- else
48
- key ||= relation
49
-
50
- command = Command.new(name, relation, key, proc)
51
- node = CommandNode.new(command)
52
- block.call(node) if block
53
- node
54
- end
55
- end
56
-
57
- # @api public
58
- def restrict(name, &block)
59
- RestrictionNode.new(Restriction.new(name, block), self)
60
- end
61
- end
62
-
63
- # @api private
64
- class CommandNode < Node
65
- # @api private
66
- def initialize(command = nil)
67
- @command = command
68
- @nodes = []
69
- end
70
-
71
- # Tiny bit of synctactic sugar
72
- #
73
- # @api public
74
- def each(&block)
75
- block.call(self)
76
- end
77
-
78
- # Return command ast from this union node
79
- #
80
- # @return [Array]
81
- #
82
- # @api private
83
- def to_ast
84
- if @command.proc
85
- command = [@command.name => @command.proc]
86
- else
87
- command = [@command.name]
88
- end
89
-
90
- key_relation_map = { @command.key => @command.relation }
91
-
92
- command << @nodes.map(&:to_ast) unless @nodes.empty?
93
-
94
- [key_relation_map, command]
95
- end
96
-
97
- # @api public
98
- def command(*args, &block)
99
- node = super
100
- @nodes << node
101
- node
102
- end
103
- end
104
-
105
- # @api private
106
- class RestrictionNode < Node
107
- attr_reader :restriction
108
-
109
- def initialize(restriction, parent_node = nil)
110
- super()
111
- @restriction = restriction
112
- @parent_node = parent_node
113
- end
114
-
115
- # @api public
116
- def command(name, options = {}, &block)
117
- relation = @restriction.relation
118
- key = options[:from] || relation
119
- proc = @restriction.proc
120
-
121
- if @parent_node
122
- @parent_node.command(name, relation, key, proc, &block)
123
- else
124
- super(name, relation, key, proc, &block)
125
- end
126
- end
127
-
128
- # @api private
129
- def restrict(*args, &block)
130
- raise DoubleRestrictionError
131
- end
132
- end
133
-
134
- # @api private
135
- class RootNode < Node
136
- def to_ast
137
- if @nodes.size > 0
138
- @nodes.first.to_ast
139
- else
140
- []
141
- end
142
- end
143
-
144
- def command(*args, &block)
145
- node = super
146
- @nodes << node
147
- node
148
- end
149
- end
150
-
151
- # @api private
152
- class BuilderNode < RootNode
153
- def initialize(container)
154
- super()
155
- @container = container
156
- end
157
-
158
- def command(*args, &block)
159
- super
160
- @container.command(to_ast)
161
- end
162
- end
163
-
164
- # @api public
165
- def initialize(container)
166
- @container = container
167
- end
168
-
169
- # @api private
170
- def method_missing(*args, &block)
171
- BuilderNode.new(@container).send(*args, &block)
172
- end
173
- end
174
- end
175
- end
176
- end