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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -599
- data/lib/rom/version.rb +1 -1
- data/lib/rom.rb +3 -44
- metadata +19 -295
- data/.gitignore +0 -24
- data/.rspec +0 -3
- data/.rubocop.yml +0 -87
- data/.rubocop_todo.yml +0 -46
- data/.travis.yml +0 -24
- data/.yardopts +0 -2
- data/CODE_OF_CONDUCT.md +0 -13
- data/CONTRIBUTING.md +0 -1
- data/Gemfile +0 -54
- data/Guardfile +0 -24
- data/Rakefile +0 -28
- data/lib/rom/array_dataset.rb +0 -44
- data/lib/rom/association_set.rb +0 -49
- data/lib/rom/auto_curry.rb +0 -55
- data/lib/rom/command.rb +0 -494
- data/lib/rom/command_registry.rb +0 -148
- data/lib/rom/commands/class_interface.rb +0 -270
- data/lib/rom/commands/composite.rb +0 -53
- data/lib/rom/commands/create.rb +0 -13
- data/lib/rom/commands/delete.rb +0 -14
- data/lib/rom/commands/graph/builder.rb +0 -176
- data/lib/rom/commands/graph/class_interface.rb +0 -62
- data/lib/rom/commands/graph/input_evaluator.rb +0 -62
- data/lib/rom/commands/graph.rb +0 -95
- data/lib/rom/commands/lazy/create.rb +0 -23
- data/lib/rom/commands/lazy/delete.rb +0 -27
- data/lib/rom/commands/lazy/update.rb +0 -34
- data/lib/rom/commands/lazy.rb +0 -99
- data/lib/rom/commands/result.rb +0 -96
- data/lib/rom/commands/update.rb +0 -14
- data/lib/rom/commands.rb +0 -3
- data/lib/rom/configuration.rb +0 -90
- data/lib/rom/configuration_dsl/command.rb +0 -41
- data/lib/rom/configuration_dsl/command_dsl.rb +0 -35
- data/lib/rom/configuration_dsl/mapper.rb +0 -36
- data/lib/rom/configuration_dsl/mapper_dsl.rb +0 -43
- data/lib/rom/configuration_dsl/relation.rb +0 -26
- data/lib/rom/configuration_dsl.rb +0 -107
- data/lib/rom/configuration_plugin.rb +0 -17
- data/lib/rom/constants.rb +0 -36
- data/lib/rom/container.rb +0 -233
- data/lib/rom/create_container.rb +0 -60
- data/lib/rom/data_proxy.rb +0 -94
- data/lib/rom/enumerable_dataset.rb +0 -68
- data/lib/rom/environment.rb +0 -70
- data/lib/rom/gateway.rb +0 -196
- data/lib/rom/global/plugin_dsl.rb +0 -47
- data/lib/rom/global.rb +0 -58
- data/lib/rom/initializer.rb +0 -26
- data/lib/rom/lint/enumerable_dataset.rb +0 -54
- data/lib/rom/lint/gateway.rb +0 -120
- data/lib/rom/lint/linter.rb +0 -78
- data/lib/rom/lint/spec.rb +0 -20
- data/lib/rom/lint/test.rb +0 -98
- data/lib/rom/mapper_registry.rb +0 -35
- data/lib/rom/memory/commands.rb +0 -56
- data/lib/rom/memory/dataset.rb +0 -97
- data/lib/rom/memory/gateway.rb +0 -64
- data/lib/rom/memory/relation.rb +0 -62
- data/lib/rom/memory/schema.rb +0 -13
- data/lib/rom/memory/storage.rb +0 -59
- data/lib/rom/memory/types.rb +0 -9
- data/lib/rom/memory.rb +0 -4
- data/lib/rom/pipeline.rb +0 -122
- data/lib/rom/plugin.rb +0 -20
- data/lib/rom/plugin_base.rb +0 -40
- data/lib/rom/plugin_registry.rb +0 -173
- data/lib/rom/plugins/command/schema.rb +0 -37
- data/lib/rom/plugins/configuration/configuration_dsl.rb +0 -21
- data/lib/rom/plugins/relation/instrumentation.rb +0 -51
- data/lib/rom/plugins/relation/key_inference.rb +0 -48
- data/lib/rom/plugins/relation/registry_reader.rb +0 -33
- data/lib/rom/registry.rb +0 -50
- data/lib/rom/relation/class_interface.rb +0 -356
- data/lib/rom/relation/composite.rb +0 -46
- data/lib/rom/relation/curried.rb +0 -109
- data/lib/rom/relation/graph.rb +0 -125
- data/lib/rom/relation/loaded.rb +0 -127
- data/lib/rom/relation/materializable.rb +0 -66
- data/lib/rom/relation/name.rb +0 -102
- data/lib/rom/relation/view_dsl.rb +0 -64
- data/lib/rom/relation.rb +0 -250
- data/lib/rom/relation_registry.rb +0 -9
- data/lib/rom/schema/attribute.rb +0 -390
- data/lib/rom/schema/dsl.rb +0 -67
- data/lib/rom/schema.rb +0 -407
- data/lib/rom/setup/auto_registration.rb +0 -74
- data/lib/rom/setup/auto_registration_strategies/base.rb +0 -16
- data/lib/rom/setup/auto_registration_strategies/custom_namespace.rb +0 -63
- data/lib/rom/setup/auto_registration_strategies/no_namespace.rb +0 -20
- data/lib/rom/setup/auto_registration_strategies/with_namespace.rb +0 -18
- data/lib/rom/setup/finalize/finalize_commands.rb +0 -47
- data/lib/rom/setup/finalize/finalize_mappers.rb +0 -36
- data/lib/rom/setup/finalize/finalize_relations.rb +0 -83
- data/lib/rom/setup/finalize.rb +0 -152
- data/lib/rom/setup.rb +0 -65
- data/lib/rom/support/configurable.rb +0 -85
- data/lib/rom/transaction.rb +0 -24
- data/lib/rom/types.rb +0 -49
- data/log/.gitkeep +0 -0
- data/rakelib/benchmark.rake +0 -15
- data/rakelib/mutant.rake +0 -19
- data/rakelib/rubocop.rake +0 -18
- data/rom.gemspec +0 -25
- data/spec/fixtures/app/commands/create_user.rb +0 -2
- data/spec/fixtures/app/mappers/user_list.rb +0 -2
- data/spec/fixtures/app/my_commands/create_user.rb +0 -2
- data/spec/fixtures/app/my_mappers/user_list.rb +0 -2
- data/spec/fixtures/app/my_relations/users.rb +0 -2
- data/spec/fixtures/app/relations/users.rb +0 -2
- data/spec/fixtures/custom/commands/create_user.rb +0 -6
- data/spec/fixtures/custom/mappers/user_list.rb +0 -6
- data/spec/fixtures/custom/relations/users.rb +0 -6
- data/spec/fixtures/custom_namespace/commands/create_customer.rb +0 -8
- data/spec/fixtures/custom_namespace/mappers/customer_list.rb +0 -8
- data/spec/fixtures/custom_namespace/relations/customers.rb +0 -8
- data/spec/fixtures/lib/persistence/commands/create_user.rb +0 -6
- data/spec/fixtures/lib/persistence/mappers/user_list.rb +0 -6
- data/spec/fixtures/lib/persistence/my_commands/create_user.rb +0 -6
- data/spec/fixtures/lib/persistence/my_mappers/user_list.rb +0 -6
- data/spec/fixtures/lib/persistence/my_relations/users.rb +0 -6
- data/spec/fixtures/lib/persistence/relations/users.rb +0 -6
- data/spec/fixtures/wrong/commands/create_customer.rb +0 -8
- data/spec/fixtures/wrong/mappers/customer_list.rb +0 -8
- data/spec/fixtures/wrong/relations/customers.rb +0 -8
- data/spec/integration/command_registry_spec.rb +0 -47
- data/spec/integration/commands/create_spec.rb +0 -157
- data/spec/integration/commands/delete_spec.rb +0 -67
- data/spec/integration/commands/error_handling_spec.rb +0 -25
- data/spec/integration/commands/graph_builder_spec.rb +0 -213
- data/spec/integration/commands/graph_spec.rb +0 -294
- data/spec/integration/commands/update_spec.rb +0 -86
- data/spec/integration/commands_spec.rb +0 -67
- data/spec/integration/gateways/extending_relations_spec.rb +0 -58
- data/spec/integration/gateways/setting_logger_spec.rb +0 -34
- data/spec/integration/mappers/combine_spec.rb +0 -117
- data/spec/integration/mappers/deep_embedded_spec.rb +0 -44
- data/spec/integration/mappers/definition_dsl_spec.rb +0 -206
- data/spec/integration/mappers/embedded_spec.rb +0 -62
- data/spec/integration/mappers/exclude_spec.rb +0 -27
- data/spec/integration/mappers/fold_spec.rb +0 -71
- data/spec/integration/mappers/group_spec.rb +0 -163
- data/spec/integration/mappers/overwrite_attributes_value_spec.rb +0 -51
- data/spec/integration/mappers/prefix_separator_spec.rb +0 -52
- data/spec/integration/mappers/prefix_spec.rb +0 -48
- data/spec/integration/mappers/prefixing_attributes_spec.rb +0 -37
- data/spec/integration/mappers/registering_custom_mappers_spec.rb +0 -28
- data/spec/integration/mappers/renaming_attributes_spec.rb +0 -125
- data/spec/integration/mappers/reusing_mappers_spec.rb +0 -43
- data/spec/integration/mappers/step_spec.rb +0 -119
- data/spec/integration/mappers/symbolizing_attributes_spec.rb +0 -77
- data/spec/integration/mappers/unfold_spec.rb +0 -92
- data/spec/integration/mappers/ungroup_spec.rb +0 -126
- data/spec/integration/mappers/unwrap_spec.rb +0 -93
- data/spec/integration/mappers/wrap_spec.rb +0 -155
- data/spec/integration/memory/commands/create_spec.rb +0 -23
- data/spec/integration/memory/commands/delete_spec.rb +0 -23
- data/spec/integration/memory/commands/update_spec.rb +0 -23
- data/spec/integration/multi_env_spec.rb +0 -69
- data/spec/integration/multi_repo_spec.rb +0 -46
- data/spec/integration/relations/default_dataset_spec.rb +0 -38
- data/spec/integration/relations/inheritance_spec.rb +0 -37
- data/spec/integration/relations/reading_spec.rb +0 -169
- data/spec/integration/relations/registry_dsl_spec.rb +0 -45
- data/spec/integration/setup_spec.rb +0 -193
- data/spec/shared/command_behavior.rb +0 -28
- data/spec/shared/command_graph.rb +0 -54
- data/spec/shared/container.rb +0 -9
- data/spec/shared/enumerable_dataset.rb +0 -52
- data/spec/shared/gateway_only.rb +0 -6
- data/spec/shared/materializable.rb +0 -36
- data/spec/shared/no_container.rb +0 -16
- data/spec/shared/one_behavior.rb +0 -26
- data/spec/shared/proxy.rb +0 -0
- data/spec/shared/users_and_tasks.rb +0 -10
- data/spec/spec_helper.rb +0 -59
- data/spec/support/constant_leak_finder.rb +0 -14
- data/spec/support/mutant.rb +0 -10
- data/spec/support/schema.rb +0 -14
- data/spec/support/types.rb +0 -5
- data/spec/test/memory_repository_lint_test.rb +0 -27
- data/spec/unit/rom/array_dataset_spec.rb +0 -59
- data/spec/unit/rom/association_set_spec.rb +0 -48
- data/spec/unit/rom/auto_curry_spec.rb +0 -71
- data/spec/unit/rom/commands/graph_spec.rb +0 -192
- data/spec/unit/rom/commands/lazy_spec.rb +0 -310
- data/spec/unit/rom/commands/pre_and_post_processors_spec.rb +0 -343
- data/spec/unit/rom/commands/result_spec.rb +0 -70
- data/spec/unit/rom/commands_spec.rb +0 -188
- data/spec/unit/rom/configurable_spec.rb +0 -49
- data/spec/unit/rom/configuration_spec.rb +0 -61
- data/spec/unit/rom/container_spec.rb +0 -109
- data/spec/unit/rom/create_container_spec.rb +0 -151
- data/spec/unit/rom/enumerable_dataset_spec.rb +0 -15
- data/spec/unit/rom/environment_spec.rb +0 -123
- data/spec/unit/rom/gateway_spec.rb +0 -146
- data/spec/unit/rom/mapper_registry_spec.rb +0 -25
- data/spec/unit/rom/memory/commands_spec.rb +0 -43
- data/spec/unit/rom/memory/dataset_spec.rb +0 -31
- data/spec/unit/rom/memory/gateway_spec.rb +0 -12
- data/spec/unit/rom/memory/inheritance_spec.rb +0 -32
- data/spec/unit/rom/memory/relation_spec.rb +0 -121
- data/spec/unit/rom/memory/storage_spec.rb +0 -45
- data/spec/unit/rom/plugin_spec.rb +0 -150
- data/spec/unit/rom/plugins/command/schema_spec.rb +0 -66
- data/spec/unit/rom/plugins/relation/instrumentation_spec.rb +0 -44
- data/spec/unit/rom/plugins/relation/key_inference_spec.rb +0 -85
- data/spec/unit/rom/registry_spec.rb +0 -86
- data/spec/unit/rom/relation/attribute_reader_spec.rb +0 -17
- data/spec/unit/rom/relation/call_spec.rb +0 -51
- data/spec/unit/rom/relation/composite_spec.rb +0 -106
- data/spec/unit/rom/relation/curried_spec.rb +0 -67
- data/spec/unit/rom/relation/graph_spec.rb +0 -106
- data/spec/unit/rom/relation/lazy/combine_spec.rb +0 -165
- data/spec/unit/rom/relation/lazy/graph_spec.rb +0 -165
- data/spec/unit/rom/relation/lazy_spec.rb +0 -214
- data/spec/unit/rom/relation/loaded_spec.rb +0 -53
- data/spec/unit/rom/relation/name_spec.rb +0 -58
- data/spec/unit/rom/relation/output_schema_spec.rb +0 -28
- data/spec/unit/rom/relation/schema_spec.rb +0 -259
- data/spec/unit/rom/relation/view_spec.rb +0 -158
- data/spec/unit/rom/relation_spec.rb +0 -252
- data/spec/unit/rom/schema/accessing_attributes_spec.rb +0 -52
- data/spec/unit/rom/schema/append_spec.rb +0 -17
- data/spec/unit/rom/schema/exclude_spec.rb +0 -15
- data/spec/unit/rom/schema/finalize_spec.rb +0 -59
- data/spec/unit/rom/schema/key_predicate_spec.rb +0 -15
- data/spec/unit/rom/schema/merge_spec.rb +0 -17
- data/spec/unit/rom/schema/prefix_spec.rb +0 -16
- data/spec/unit/rom/schema/project_spec.rb +0 -15
- data/spec/unit/rom/schema/rename_spec.rb +0 -22
- data/spec/unit/rom/schema/type_spec.rb +0 -49
- data/spec/unit/rom/schema/uniq_spec.rb +0 -21
- data/spec/unit/rom/schema/wrap_spec.rb +0 -18
- data/spec/unit/rom/schema_spec.rb +0 -10
- data/spec/unit/rom/setup/auto_registration_spec.rb +0 -214
data/lib/rom/schema/attribute.rb
DELETED
@@ -1,390 +0,0 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
require 'dry/equalizer'
|
3
|
-
require 'dry/types/decorator'
|
4
|
-
require 'rom/initializer'
|
5
|
-
|
6
|
-
module ROM
|
7
|
-
class Schema
|
8
|
-
# Schema attributes provide meta information about types and an API
|
9
|
-
# for additional operations. This class can be extended by adapters to provide
|
10
|
-
# database-specific features. In example rom-sql provides SQL::Attribute
|
11
|
-
# with more features like creating SQL expressions for queries.
|
12
|
-
#
|
13
|
-
# Schema attributes are accessible through canonical relation schemas and
|
14
|
-
# instance-level schemas.
|
15
|
-
#
|
16
|
-
# @api public
|
17
|
-
class Attribute
|
18
|
-
include Dry::Equalizer(:type, :options)
|
19
|
-
|
20
|
-
extend Initializer
|
21
|
-
|
22
|
-
# !@attribute [r] type
|
23
|
-
# @return [Dry::Types::Definition, Dry::Types::Sum, Dry::Types::Constrained]
|
24
|
-
param :type
|
25
|
-
|
26
|
-
# @api private
|
27
|
-
def [](input)
|
28
|
-
type[input]
|
29
|
-
end
|
30
|
-
|
31
|
-
# Return true if this attribute type is a primary key
|
32
|
-
#
|
33
|
-
# @example
|
34
|
-
# class Users < ROM::Relation[:memory]
|
35
|
-
# schema do
|
36
|
-
# attribute :id, Types::Int
|
37
|
-
# attribute :name, Types::String
|
38
|
-
#
|
39
|
-
# primary_key :id
|
40
|
-
# end
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# Users.schema[:id].primary_key?
|
44
|
-
# # => true
|
45
|
-
#
|
46
|
-
# Users.schema[:name].primary_key?
|
47
|
-
# # => false
|
48
|
-
#
|
49
|
-
# @return [TrueClass,FalseClass]
|
50
|
-
#
|
51
|
-
# @api public
|
52
|
-
def primary_key?
|
53
|
-
meta[:primary_key].equal?(true)
|
54
|
-
end
|
55
|
-
|
56
|
-
# Return true if this attribute type is a foreign key
|
57
|
-
#
|
58
|
-
# @example
|
59
|
-
# class Tasks < ROM::Relation[:memory]
|
60
|
-
# schema do
|
61
|
-
# attribute :id, Types::Int
|
62
|
-
# attribute :user_id, Types.ForeignKey(:users)
|
63
|
-
# end
|
64
|
-
# end
|
65
|
-
#
|
66
|
-
# Users.schema[:user_id].foreign_key?
|
67
|
-
# # => true
|
68
|
-
#
|
69
|
-
# Users.schema[:id].foreign_key?
|
70
|
-
# # => false
|
71
|
-
#
|
72
|
-
# @return [TrueClass,FalseClass]
|
73
|
-
#
|
74
|
-
# @api public
|
75
|
-
def foreign_key?
|
76
|
-
meta[:foreign_key].equal?(true)
|
77
|
-
end
|
78
|
-
|
79
|
-
# Return true if this attribute type is a foreign key
|
80
|
-
#
|
81
|
-
# @example
|
82
|
-
# class Tasks < ROM::Relation[:memory]
|
83
|
-
# schema do
|
84
|
-
# attribute :user_id, Types::Int.meta(alias: :id)
|
85
|
-
# attribute :name, Types::String
|
86
|
-
# end
|
87
|
-
# end
|
88
|
-
#
|
89
|
-
# Users.schema[:user_id].aliased?
|
90
|
-
# # => true
|
91
|
-
#
|
92
|
-
# Users.schema[:name].aliased?
|
93
|
-
# # => false
|
94
|
-
#
|
95
|
-
# @return [TrueClass,FalseClass]
|
96
|
-
#
|
97
|
-
# @api public
|
98
|
-
def aliased?
|
99
|
-
!meta[:alias].nil?
|
100
|
-
end
|
101
|
-
|
102
|
-
# Return source relation of this attribute type
|
103
|
-
#
|
104
|
-
# @example
|
105
|
-
# class Tasks < ROM::Relation[:memory]
|
106
|
-
# schema do
|
107
|
-
# attribute :id, Types::Int
|
108
|
-
# attribute :user_id, Types.ForeignKey(:users)
|
109
|
-
# end
|
110
|
-
# end
|
111
|
-
#
|
112
|
-
# Users.schema[:id].source
|
113
|
-
# # => :tasks
|
114
|
-
#
|
115
|
-
# Users.schema[:user_id].source
|
116
|
-
# # => :tasks
|
117
|
-
#
|
118
|
-
# @return [Symbol, Relation::Name]
|
119
|
-
#
|
120
|
-
# @api public
|
121
|
-
def source
|
122
|
-
meta[:source]
|
123
|
-
end
|
124
|
-
|
125
|
-
# Return target relation of this attribute type
|
126
|
-
#
|
127
|
-
# @example
|
128
|
-
# class Tasks < ROM::Relation[:memory]
|
129
|
-
# schema do
|
130
|
-
# attribute :id, Types::Int
|
131
|
-
# attribute :user_id, Types.ForeignKey(:users)
|
132
|
-
# end
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# Users.schema[:id].target
|
136
|
-
# # => nil
|
137
|
-
#
|
138
|
-
# Users.schema[:user_id].target
|
139
|
-
# # => :users
|
140
|
-
#
|
141
|
-
# @return [NilClass, Symbol, Relation::Name]
|
142
|
-
#
|
143
|
-
# @api public
|
144
|
-
def target
|
145
|
-
meta[:target]
|
146
|
-
end
|
147
|
-
|
148
|
-
# Return the canonical name of this attribute name
|
149
|
-
#
|
150
|
-
# This *always* returns the name that is used in the datastore, even when
|
151
|
-
# an attribute is aliased
|
152
|
-
#
|
153
|
-
# @example
|
154
|
-
# class Tasks < ROM::Relation[:memory]
|
155
|
-
# schema do
|
156
|
-
# attribute :user_id, Types::Int.meta(alias: :id)
|
157
|
-
# attribute :name, Types::String
|
158
|
-
# end
|
159
|
-
# end
|
160
|
-
#
|
161
|
-
# Users.schema[:id].name
|
162
|
-
# # => :id
|
163
|
-
#
|
164
|
-
# Users.schema[:user_id].name
|
165
|
-
# # => :user_id
|
166
|
-
#
|
167
|
-
# @return [Symbol]
|
168
|
-
#
|
169
|
-
# @api public
|
170
|
-
def name
|
171
|
-
meta[:name]
|
172
|
-
end
|
173
|
-
|
174
|
-
# Return tuple key
|
175
|
-
#
|
176
|
-
# When schemas are projected with aliased attributes, we need a simple access to tuple keys
|
177
|
-
#
|
178
|
-
# @example
|
179
|
-
# class Tasks < ROM::Relation[:memory]
|
180
|
-
# schema do
|
181
|
-
# attribute :user_id, Types::Int.meta(alias: :id)
|
182
|
-
# attribute :name, Types::String
|
183
|
-
# end
|
184
|
-
# end
|
185
|
-
#
|
186
|
-
# Users.schema[:id].key
|
187
|
-
# # :id
|
188
|
-
#
|
189
|
-
# Users.schema.project(Users.schema[:id].aliased(:user_id)).key
|
190
|
-
# # :user_id
|
191
|
-
#
|
192
|
-
# @return [Symbol]
|
193
|
-
#
|
194
|
-
# @api public
|
195
|
-
def key
|
196
|
-
meta[:alias] || name
|
197
|
-
end
|
198
|
-
|
199
|
-
# Return attribute's alias
|
200
|
-
#
|
201
|
-
# @example
|
202
|
-
# class Tasks < ROM::Relation[:memory]
|
203
|
-
# schema do
|
204
|
-
# attribute :user_id, Types::Int.meta(alias: :id)
|
205
|
-
# attribute :name, Types::String
|
206
|
-
# end
|
207
|
-
# end
|
208
|
-
#
|
209
|
-
# Users.schema[:user_id].alias
|
210
|
-
# # => :user_id
|
211
|
-
#
|
212
|
-
# Users.schema[:name].alias
|
213
|
-
# # => nil
|
214
|
-
#
|
215
|
-
# @return [NilClass,Symbol]
|
216
|
-
#
|
217
|
-
# @api public
|
218
|
-
def alias
|
219
|
-
meta[:alias]
|
220
|
-
end
|
221
|
-
|
222
|
-
# Return new attribute type with provided alias
|
223
|
-
#
|
224
|
-
# @example
|
225
|
-
# class Tasks < ROM::Relation[:memory]
|
226
|
-
# schema do
|
227
|
-
# attribute :user_id, Types::Int
|
228
|
-
# attribute :name, Types::String
|
229
|
-
# end
|
230
|
-
# end
|
231
|
-
#
|
232
|
-
# aliased_user_id = Users.schema[:user_id].aliased(:id)
|
233
|
-
#
|
234
|
-
# aliased_user_id.aliased?
|
235
|
-
# # => true
|
236
|
-
#
|
237
|
-
# aliased_user_id.name
|
238
|
-
# # => :user_id
|
239
|
-
#
|
240
|
-
# aliased_user_id.alias
|
241
|
-
# # => :id
|
242
|
-
#
|
243
|
-
# @param [Symbol] name The alias
|
244
|
-
#
|
245
|
-
# @return [Schema::Attribute]
|
246
|
-
#
|
247
|
-
# @api public
|
248
|
-
def aliased(name)
|
249
|
-
meta(alias: name)
|
250
|
-
end
|
251
|
-
alias_method :as, :aliased
|
252
|
-
|
253
|
-
# Return new attribute type with an alias using provided prefix
|
254
|
-
#
|
255
|
-
# @example
|
256
|
-
# class Users < ROM::Relation[:memory]
|
257
|
-
# schema do
|
258
|
-
# attribute :id, Types::Int
|
259
|
-
# attribute :name, Types::String
|
260
|
-
# end
|
261
|
-
# end
|
262
|
-
#
|
263
|
-
# prefixed_id = Users.schema[:id].prefixed
|
264
|
-
#
|
265
|
-
# prefixed_id.aliased?
|
266
|
-
# # => true
|
267
|
-
#
|
268
|
-
# prefixed_id.name
|
269
|
-
# # => :id
|
270
|
-
#
|
271
|
-
# prefixed_id.alias
|
272
|
-
# # => :users_id
|
273
|
-
#
|
274
|
-
# prefixed_id = Users.schema[:id].prefixed(:user)
|
275
|
-
#
|
276
|
-
# prefixed_id.alias
|
277
|
-
# # => :user_id
|
278
|
-
#
|
279
|
-
# @param [Symbol] prefix The prefix (defaults to source.dataset)
|
280
|
-
#
|
281
|
-
# @return [Schema::Attribute]
|
282
|
-
#
|
283
|
-
# @api public
|
284
|
-
def prefixed(prefix = source.dataset)
|
285
|
-
aliased(:"#{prefix}_#{name}")
|
286
|
-
end
|
287
|
-
|
288
|
-
# Return if the attribute type is from a wrapped relation
|
289
|
-
#
|
290
|
-
# Wrapped attributes are used when two schemas from different relations
|
291
|
-
# are merged together. This way we can identify them easily and handle
|
292
|
-
# correctly in places like auto-mapping.
|
293
|
-
#
|
294
|
-
# @api public
|
295
|
-
def wrapped?
|
296
|
-
meta[:wrapped].equal?(true)
|
297
|
-
end
|
298
|
-
|
299
|
-
# Return attribute type wrapped for the specified relation name
|
300
|
-
#
|
301
|
-
# @param [Symbol] name The name of the source relation (defaults to source.dataset)
|
302
|
-
#
|
303
|
-
# @return [Schema::Attribute]
|
304
|
-
#
|
305
|
-
# @api public
|
306
|
-
def wrapped(name = source.dataset)
|
307
|
-
self.class.new(prefixed(name).meta(wrapped: true))
|
308
|
-
end
|
309
|
-
|
310
|
-
# Return attribute type with additional meta information
|
311
|
-
#
|
312
|
-
# Return meta information hash if no opts are provided
|
313
|
-
#
|
314
|
-
# @param [Hash] opts The meta options
|
315
|
-
#
|
316
|
-
# @return [Schema::Attribute]
|
317
|
-
#
|
318
|
-
# @api public
|
319
|
-
def meta(opts = nil)
|
320
|
-
if opts
|
321
|
-
self.class.new(type.meta(opts))
|
322
|
-
else
|
323
|
-
type.meta
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
# Return string representation of the attribute type
|
328
|
-
#
|
329
|
-
# @return [String]
|
330
|
-
#
|
331
|
-
# @api public
|
332
|
-
def inspect
|
333
|
-
%(#<#{self.class}[#{type.name}] #{meta.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')}>)
|
334
|
-
end
|
335
|
-
alias_method :pretty_inspect, :inspect
|
336
|
-
|
337
|
-
# Check if the attribute type is equal to another
|
338
|
-
#
|
339
|
-
# @param [Dry::Type, Schema::Attribute]
|
340
|
-
#
|
341
|
-
# @return [TrueClass,FalseClass]
|
342
|
-
#
|
343
|
-
# @api public
|
344
|
-
def eql?(other)
|
345
|
-
other.is_a?(self.class) ? super : type.eql?(other)
|
346
|
-
end
|
347
|
-
|
348
|
-
# Return if this attribute type has additional attribute type for reading
|
349
|
-
# tuple values
|
350
|
-
#
|
351
|
-
# @return [TrueClass, FalseClass]
|
352
|
-
#
|
353
|
-
# @api private
|
354
|
-
def read?
|
355
|
-
! meta[:read].nil?
|
356
|
-
end
|
357
|
-
|
358
|
-
# Return read type or self
|
359
|
-
#
|
360
|
-
# @return [Schema::Attribute]
|
361
|
-
#
|
362
|
-
# @api private
|
363
|
-
def to_read_type
|
364
|
-
read? ? meta[:read] : type
|
365
|
-
end
|
366
|
-
|
367
|
-
# @api private
|
368
|
-
def respond_to_missing?(name, include_private = false)
|
369
|
-
type.respond_to?(name) || super
|
370
|
-
end
|
371
|
-
|
372
|
-
private
|
373
|
-
|
374
|
-
# @api private
|
375
|
-
def method_missing(meth, *args, &block)
|
376
|
-
if type.respond_to?(meth)
|
377
|
-
response = type.__send__(meth, *args, &block)
|
378
|
-
|
379
|
-
if response.is_a?(type.class)
|
380
|
-
self.class.new(type, options)
|
381
|
-
else
|
382
|
-
response
|
383
|
-
end
|
384
|
-
else
|
385
|
-
super
|
386
|
-
end
|
387
|
-
end
|
388
|
-
end
|
389
|
-
end
|
390
|
-
end
|
data/lib/rom/schema/dsl.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'dry-equalizer'
|
2
|
-
require 'rom/types'
|
3
|
-
|
4
|
-
module ROM
|
5
|
-
# Relation schema
|
6
|
-
#
|
7
|
-
# @api public
|
8
|
-
class Schema
|
9
|
-
AttributeAlreadyDefinedError = Class.new(StandardError)
|
10
|
-
|
11
|
-
include Dry::Equalizer(:name, :attributes)
|
12
|
-
include Enumerable
|
13
|
-
|
14
|
-
attr_reader :name, :attributes, :inferrer
|
15
|
-
|
16
|
-
# @api public
|
17
|
-
class DSL < BasicObject
|
18
|
-
attr_reader :relation, :attributes, :inferrer, :schema_class
|
19
|
-
|
20
|
-
# @api private
|
21
|
-
def initialize(relation, schema_class: Schema, inferrer: Schema::DEFAULT_INFERRER, &block)
|
22
|
-
@relation = relation
|
23
|
-
@inferrer = inferrer
|
24
|
-
@schema_class = schema_class
|
25
|
-
@attributes = {}
|
26
|
-
|
27
|
-
if block
|
28
|
-
instance_exec(&block)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# Defines a relation attribute with its type
|
33
|
-
#
|
34
|
-
# @see Relation.schema
|
35
|
-
#
|
36
|
-
# @api public
|
37
|
-
def attribute(name, type, options = EMPTY_HASH)
|
38
|
-
if @attributes.key?(name)
|
39
|
-
::Kernel.raise ::ROM::Schema::AttributeAlreadyDefinedError,
|
40
|
-
"Attribute #{ name.inspect } already defined"
|
41
|
-
end
|
42
|
-
|
43
|
-
@attributes[name] =
|
44
|
-
if options[:read]
|
45
|
-
type.meta(name: name, source: relation, read: options[:read])
|
46
|
-
else
|
47
|
-
type.meta(name: name, source: relation)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# Specify which key(s) should be the primary key
|
52
|
-
#
|
53
|
-
# @api public
|
54
|
-
def primary_key(*names)
|
55
|
-
names.each do |name|
|
56
|
-
attributes[name] = attributes[name].meta(primary_key: true)
|
57
|
-
end
|
58
|
-
self
|
59
|
-
end
|
60
|
-
|
61
|
-
# @api private
|
62
|
-
def call
|
63
|
-
schema_class.define(relation, attributes: attributes.values, inferrer: inferrer)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|