foobara 0.0.30 → 0.0.31

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -1
  3. data/projects/command_connectors/src/serializers/entities_to_primary_keys_serializer.rb +2 -0
  4. data/projects/detached_entity/lib/foobara/detached_entity.rb +19 -0
  5. data/projects/{entity → detached_entity}/src/concerns/associations.rb +4 -59
  6. data/projects/detached_entity/src/concerns/equality.rb +24 -0
  7. data/projects/{entity → detached_entity}/src/concerns/primary_key.rb +2 -2
  8. data/projects/{entity → detached_entity}/src/concerns/reflection.rb +15 -1
  9. data/projects/detached_entity/src/concerns/types.rb +41 -0
  10. data/projects/detached_entity/src/detached_entity.rb +15 -0
  11. data/projects/detached_entity/src/extensions/builtin_types/detached_entity/casters/hash.rb +39 -0
  12. data/projects/detached_entity/src/extensions/builtin_types/detached_entity/validators/attributes_declaration.rb +36 -0
  13. data/projects/detached_entity/src/extensions/builtin_types/detached_entity.rb +6 -0
  14. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/attributes_handler_desugarizer.rb +13 -0
  15. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/hash_desugarizer.rb +43 -0
  16. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/model_class_desugarizer.rb +21 -0
  17. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/primary_key_desugarizer.rb +19 -0
  18. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/to_type_transformer.rb +26 -0
  19. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/validate_primary_key_is_symbol.rb +35 -0
  20. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/validate_primary_key_present.rb +27 -0
  21. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration/validate_primary_key_references_attribute.rb +36 -0
  22. data/projects/detached_entity/src/extensions/type_declarations/handlers/extend_detached_entity_type_declaration.rb +15 -0
  23. data/projects/entity/lib/foobara/entity.rb +3 -7
  24. data/projects/entity/src/concerns/attribute_helpers.rb +1 -58
  25. data/projects/entity/src/concerns/attributes.rb +1 -1
  26. data/projects/entity/src/concerns/callbacks.rb +1 -1
  27. data/projects/entity/src/concerns/initialization.rb +1 -1
  28. data/projects/entity/src/concerns/mutations.rb +68 -0
  29. data/projects/entity/src/concerns/persistence.rb +1 -1
  30. data/projects/entity/src/concerns/queries.rb +56 -1
  31. data/projects/entity/src/concerns/transactions.rb +1 -1
  32. data/projects/entity/src/concerns/types.rb +2 -18
  33. data/projects/entity/src/entity.rb +3 -48
  34. data/projects/entity/src/extensions/builtin_types/entity/casters/hash.rb +8 -20
  35. data/projects/entity/src/extensions/builtin_types/entity/validators/attributes_declaration.rb +3 -18
  36. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/attributes_handler_desugarizer.rb +2 -3
  37. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/hash_desugarizer.rb +2 -32
  38. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/model_class_desugarizer.rb +2 -6
  39. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/primary_key_desugarizer.rb +2 -11
  40. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb +2 -11
  41. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/validate_primary_key_is_symbol.rb +2 -27
  42. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/validate_primary_key_present.rb +2 -19
  43. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/validate_primary_key_references_attribute.rb +3 -28
  44. data/projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration.rb +5 -1
  45. data/projects/entity/src/new_prepend.rb +1 -1
  46. data/projects/entity/src/not_found_error.rb +1 -1
  47. data/projects/foobara/lib/foobara/all.rb +1 -0
  48. data/projects/manifest/src/foobara/manifest/detached_entity.rb +30 -0
  49. data/projects/manifest/src/foobara/manifest/domain.rb +4 -0
  50. data/projects/manifest/src/foobara/manifest/entity.rb +3 -15
  51. data/projects/manifest/src/foobara/manifest/root_manifest.rb +8 -0
  52. data/projects/manifest/src/foobara/manifest/type.rb +17 -1
  53. data/projects/manifest/src/foobara/manifest/type_declaration.rb +17 -0
  54. data/projects/model/src/model.rb +3 -1
  55. metadata +24 -5
@@ -1,14 +1,9 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  module Concerns
4
4
  module AttributeHelpers
5
5
  include Foobara::Concern
6
6
 
7
- def update_aggregate(value, type = self.class.model_type)
8
- # is this a smell?
9
- self.class.update_aggregate(self, value, type)
10
- end
11
-
12
7
  module ClassMethods
13
8
  def attributes_for_update
14
9
  attributes_for_aggregate_update
@@ -111,58 +106,6 @@ module Foobara
111
106
  )
112
107
  end
113
108
 
114
- def update_aggregate(object, value, type = object.class.model_type)
115
- return value if object.nil?
116
-
117
- if type.extends?(BuiltinTypes[:model])
118
- element_types = type.element_types.element_types
119
-
120
- value.each_pair do |attribute_name, new_value|
121
- current_value = object.read_attribute(attribute_name)
122
-
123
- attribute_type = element_types[attribute_name]
124
-
125
- updated_value = update_aggregate(current_value, new_value, attribute_type)
126
-
127
- object.write_attribute(attribute_name, updated_value)
128
- end
129
-
130
- object
131
- elsif type.extends?(BuiltinTypes[:attributes])
132
- element_types = type.element_types
133
-
134
- object = object.dup
135
- object ||= {}
136
-
137
- value.each_pair do |attribute_name, new_value|
138
- current_value = object[attribute_name]
139
- attribute_type = element_types[attribute_name]
140
-
141
- updated_value = update_aggregate(current_value, new_value, attribute_type)
142
-
143
- object[attribute_name] = updated_value
144
- end
145
-
146
- object
147
- elsif type.extends?(BuiltinTypes[:tuple])
148
- # :nocov:
149
- raise "Tuple not yet supported"
150
- # :nocov:
151
- elsif type.extends?(BuiltinTypes[:associative_array])
152
- # :nocov:
153
- raise "Associated array not yet supported"
154
- # :nocov:
155
- elsif type.extends?(BuiltinTypes[:array])
156
- element_type = type.element_type
157
-
158
- value.map.with_index do |element, index|
159
- update_aggregate(object[index], element, element_type)
160
- end
161
- else
162
- value
163
- end
164
- end
165
-
166
109
  private
167
110
 
168
111
  def type_declaration_value_at(declaration, path_parts)
@@ -1,5 +1,5 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  module Concerns
4
4
  module Attributes
5
5
  class UnexpectedPrimaryKeyChangeError < StandardError; end
@@ -1,5 +1,5 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  module Concerns
4
4
  module Callbacks
5
5
  include Concern
@@ -1,5 +1,5 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  class NoCurrentTransactionError < StandardError; end
4
4
 
5
5
  module Concerns
@@ -0,0 +1,68 @@
1
+ module Foobara
2
+ class Entity < DetachedEntity
3
+ module Concerns
4
+ module Mutations
5
+ include Concern
6
+
7
+ def update_aggregate(value, type = self.class.model_type)
8
+ # is this a smell?
9
+ self.class.update_aggregate(self, value, type)
10
+ end
11
+
12
+ module ClassMethods
13
+ def update_aggregate(object, value, type = object.class.model_type)
14
+ return value if object.nil?
15
+
16
+ if type.extends?(BuiltinTypes[:model])
17
+ element_types = type.element_types.element_types
18
+
19
+ value.each_pair do |attribute_name, new_value|
20
+ current_value = object.read_attribute(attribute_name)
21
+
22
+ attribute_type = element_types[attribute_name]
23
+
24
+ updated_value = update_aggregate(current_value, new_value, attribute_type)
25
+
26
+ object.write_attribute(attribute_name, updated_value)
27
+ end
28
+
29
+ object
30
+ elsif type.extends?(BuiltinTypes[:attributes])
31
+ element_types = type.element_types
32
+
33
+ object = object.dup
34
+ object ||= {}
35
+
36
+ value.each_pair do |attribute_name, new_value|
37
+ current_value = object[attribute_name]
38
+ attribute_type = element_types[attribute_name]
39
+
40
+ updated_value = update_aggregate(current_value, new_value, attribute_type)
41
+
42
+ object[attribute_name] = updated_value
43
+ end
44
+
45
+ object
46
+ elsif type.extends?(BuiltinTypes[:tuple])
47
+ # :nocov:
48
+ raise "Tuple not yet supported"
49
+ # :nocov:
50
+ elsif type.extends?(BuiltinTypes[:associative_array])
51
+ # :nocov:
52
+ raise "Associated array not yet supported"
53
+ # :nocov:
54
+ elsif type.extends?(BuiltinTypes[:array])
55
+ element_type = type.element_type
56
+
57
+ value.map.with_index do |element, index|
58
+ update_aggregate(object[index], element, element_type)
59
+ end
60
+ else
61
+ value
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,5 +1,5 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  module Concerns
4
4
  module Persistence
5
5
  class CannotUpdateHardDeletedRecordError < StandardError; end
@@ -1,5 +1,5 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  module Concerns
4
4
  module Queries
5
5
  include Concern
@@ -89,6 +89,61 @@ module Foobara
89
89
  def count
90
90
  current_transaction_table.count
91
91
  end
92
+
93
+ def that_owns(record, filters = [])
94
+ containing_records = that_own(record, filters)
95
+
96
+ unless containing_records.empty?
97
+ if containing_records.size == 1
98
+ containing_records.first
99
+ else
100
+ # :nocov:
101
+ raise "Expected only one record to own #{record} but found #{containing_records.size}"
102
+ # :nocov:
103
+ end
104
+ end
105
+ end
106
+
107
+ def that_own(record, filters = [])
108
+ association_key = association_for([record.class, *filters])
109
+
110
+ data_path = DataPath.new(association_key)
111
+
112
+ done = false
113
+
114
+ containing_records = Util.array(record)
115
+
116
+ until done
117
+ last = data_path.path.last
118
+
119
+ if last == :"#"
120
+ method = :find_all_by_attribute_containing_any_of
121
+ attribute_name = data_path.path[-2]
122
+ data_path = DataPath.new(data_path.path[0..-3])
123
+ else
124
+ method = :find_all_by_attribute_any_of
125
+ attribute_name = last
126
+ data_path = DataPath.new(data_path.path[0..-2])
127
+ end
128
+
129
+ containing_entity_class_path = data_path.to_s
130
+
131
+ entity_class = if containing_entity_class_path.empty?
132
+ done = true
133
+ self
134
+ else
135
+ deep_associations[
136
+ containing_entity_class_path
137
+ ].target_class
138
+ end
139
+
140
+ containing_records = entity_class.send(method, attribute_name, containing_records).to_a
141
+
142
+ done = true unless containing_records
143
+ end
144
+
145
+ containing_records
146
+ end
92
147
  end
93
148
  end
94
149
  end
@@ -1,5 +1,5 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  module Concerns
4
4
  module Transactions
5
5
  include Concern
@@ -1,28 +1,12 @@
1
1
  module Foobara
2
- class Entity < Model
2
+ class Entity < DetachedEntity
3
3
  module Concerns
4
4
  module Types
5
5
  include Concern
6
6
 
7
7
  module ClassMethods
8
- def entity_type
9
- model_type
10
- end
11
-
12
8
  def type_declaration(...)
13
- raise "No primary key set yet" unless primary_key_attribute
14
-
15
- super.merge(type: :entity, primary_key: primary_key_attribute)
16
- end
17
-
18
- def set_model_type
19
- if primary_key_attribute
20
- super
21
- end
22
- end
23
-
24
- def primary_key_type
25
- @primary_key_type ||= attributes_type.element_types[primary_key_attribute]
9
+ super.merge(type: :entity)
26
10
  end
27
11
  end
28
12
  end
@@ -1,62 +1,17 @@
1
1
  module Foobara
2
- class Entity < Model
3
- class CannotConvertRecordWithoutPrimaryKeyToJsonError < StandardError; end
4
-
2
+ class Entity < DetachedEntity
5
3
  include Concerns::Callbacks
6
- include Concerns::Associations
7
4
  include Concerns::Transactions
8
5
  include Concerns::Queries
9
- include Concerns::Types
6
+ include Concerns::Mutations
10
7
  include Concerns::Attributes
11
- include Concerns::PrimaryKey
12
8
  include Concerns::Persistence
13
9
  include Concerns::Initialization
14
- include Concerns::Reflection
15
10
  include Concerns::AttributeHelpers
11
+ include Concerns::Types
16
12
 
17
13
  class << self
18
14
  prepend NewPrepend
19
-
20
- def full_entity_name
21
- full_model_name
22
- end
23
-
24
- def entity_name
25
- model_name
26
- end
27
-
28
- def allowed_subclass_opts
29
- [:primary_key, *super]
30
- end
31
- end
32
-
33
- foobara_delegate :full_entity_name, :entity_name, to: :class
34
-
35
- def dup
36
- # TODO: Maybe raise instead?
37
- self
38
- end
39
-
40
- def ==(other)
41
- # Should both records be required to be persisted to be considered equal when having matching primary keys?
42
- # For now we will consider them equal but it could make sense to consider them not equal.
43
- equal?(other) || (self.class == other.class && primary_key && primary_key == other.primary_key)
44
- end
45
-
46
- def hash
47
- (primary_key || object_id).hash
48
- end
49
-
50
- def inspect
51
- "<#{entity_name}:#{primary_key}>"
52
- end
53
-
54
- def to_json(*_args)
55
- primary_key&.to_json || raise(
56
- CannotConvertRecordWithoutPrimaryKeyToJsonError,
57
- "Cannot call record.to_json on unless record has a primary key. " \
58
- "Consider instead calling record.attributes.to_json instead."
59
- )
60
15
  end
61
16
  end
62
17
  end
@@ -3,35 +3,23 @@ module Foobara
3
3
  module Entity
4
4
  module Casters
5
5
  # TODO: We need a way of disabling/enabling this and it should probably be disabled by default.
6
- class Hash < Attributes::Casters::Hash
7
- class << self
8
- def requires_parent_declaration_data?
9
- true
10
- end
11
- end
12
-
13
- def cast(attributes)
14
- symbolized_attributes = super
6
+ class Hash < DetachedEntity::Casters::Hash
7
+ def build_method(attributes)
8
+ outcome = entity_class.attributes_type.process_value(attributes)
15
9
 
16
- outcome = entity_class.attributes_type.process_value(symbolized_attributes)
10
+ outcome.result
17
11
 
18
12
  if outcome.success?
19
- entity_class.create(symbolized_attributes)
13
+ :create
20
14
  else
21
15
  # we build an instance so that it can fail a validator later. But we already know we don't want to
22
16
  # persist this thing. So use build instead of create.
23
- entity_class.build(outcome.result)
17
+ :build
24
18
  end
25
19
  end
26
20
 
27
- def entity_class
28
- type = parent_declaration_data[:type]
29
-
30
- if type == :entity
31
- Object.const_get(parent_declaration_data[:model_class])
32
- else
33
- Foobara::Namespace.current.foobara_lookup_type!(type).target_class
34
- end
21
+ def expected_type_symbol
22
+ :entity
35
23
  end
36
24
  end
37
25
  end
@@ -2,28 +2,13 @@ module Foobara
2
2
  module BuiltinTypes
3
3
  module Entity
4
4
  module Validators
5
- class AttributesDeclaration < Model::Validators::AttributesDeclaration
5
+ class AttributesDeclaration < DetachedEntity::Validators::AttributesDeclaration
6
6
  def applicable?(record)
7
7
  record.created? || record.built?
8
8
  end
9
9
 
10
- # Why is this here in entity/ instead of in model/?
11
- def possible_errors
12
- return [] if parent_declaration_data == { type: :entity }
13
-
14
- mutable = parent_declaration_data.key?(:mutable) ? parent_declaration_data[:mutable] : false
15
-
16
- if parent_declaration_data.key?(:model_class)
17
- Object.const_get(parent_declaration_data[:model_class]).possible_errors(mutable:)
18
- elsif parent_declaration_data[:type] != :entity
19
- model_type = type_for_declaration(parent_declaration_data[:type])
20
- model_class = model_type.target_class
21
- model_class.possible_errors(mutable:)
22
- else
23
- # :nocov:
24
- raise "Missing :model_class in parent_declaration_data for #{parent_declaration_data}"
25
- # :nocov:
26
- end
10
+ def expected_type_symbol
11
+ :entity
27
12
  end
28
13
  end
29
14
  end
@@ -1,9 +1,8 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- # TODO: need primary key type declaration validator!
6
- class AttributesHandlerDesugarizer < ExtendModelTypeDeclaration::AttributesHandlerDesugarizer
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class AttributesHandlerDesugarizer < ExtendDetachedEntityTypeDeclaration::AttributesHandlerDesugarizer
7
6
  def expected_type_symbol
8
7
  :entity
9
8
  end
@@ -1,41 +1,11 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- class HashDesugarizer < TypeDeclarations::Desugarizer
6
- def applicable?(sugary_type_declaration)
7
- return false unless sugary_type_declaration.is_a?(::Hash)
8
- return false unless Util.all_symbolizable_keys?(sugary_type_declaration)
9
-
10
- sugary_type_declaration = Util.symbolize_keys(sugary_type_declaration)
11
-
12
- type_symbol = sugary_type_declaration[:type] || sugary_type_declaration["type"]
13
- return false unless type_symbol
14
-
15
- type_symbol = type_symbol.to_sym if type_symbol.is_a?(::String)
16
-
17
- return true if type_symbol == expected_type_symbol
18
-
19
- if type_symbol.is_a?(::Symbol) && type_registered?(type_symbol)
20
- type = Foobara.foobara_root_namespace.foobara_lookup_type(
21
- type_symbol, mode: Namespace::LookupMode::ABSOLUTE
22
- )
23
-
24
- type&.extends?(BuiltinTypes[expected_type_symbol])
25
- end
26
- end
27
-
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class HashDesugarizer < ExtendDetachedEntityTypeDeclaration::HashDesugarizer
28
6
  def expected_type_symbol
29
7
  :entity
30
8
  end
31
-
32
- def desugarize(sugary_type_declaration)
33
- Util.symbolize_keys(sugary_type_declaration)
34
- end
35
-
36
- def priority
37
- Priority::FIRST + 1
38
- end
39
9
  end
40
10
  end
41
11
  end
@@ -1,8 +1,8 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- class ModelClassDesugarizer < ExtendModelTypeDeclaration::ModelClassDesugarizer
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class ModelClassDesugarizer < ExtendDetachedEntityTypeDeclaration::ModelClassDesugarizer
6
6
  def expected_type_symbol
7
7
  :entity
8
8
  end
@@ -10,10 +10,6 @@ module Foobara
10
10
  def default_model_base_class
11
11
  Foobara::Entity
12
12
  end
13
-
14
- def create_model_class_args(model_module:, type_declaration:)
15
- super.merge(primary_key: type_declaration[:primary_key])
16
- end
17
13
  end
18
14
  end
19
15
  end
@@ -1,17 +1,8 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- class PrimaryKeyDesugarizer < Desugarizer
6
- def applicable?(sugary_type_declaration)
7
- primary_key = sugary_type_declaration[:primary_key]
8
-
9
- primary_key.is_a?(::String)
10
- end
11
-
12
- def desugarize(sugary_type_declaration)
13
- sugary_type_declaration.merge(primary_key: sugary_type_declaration[:primary_key].to_sym)
14
- end
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class PrimaryKeyDesugarizer < ExtendDetachedEntityTypeDeclaration::PrimaryKeyDesugarizer
15
6
  end
16
7
  end
17
8
  end
@@ -1,8 +1,8 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- class ToTypeTransformer < ExtendModelTypeDeclaration::ToTypeTransformer
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class ToTypeTransformer < ExtendDetachedEntityTypeDeclaration::ToTypeTransformer
6
6
  # TODO: move this to a more appropriate place
7
7
  class EntityPrimaryKeyCaster < Value::Caster
8
8
  class << self
@@ -32,21 +32,12 @@ module Foobara
32
32
  end
33
33
  end
34
34
 
35
- def non_processor_keys
36
- [:primary_key, *super]
37
- end
38
-
39
35
  def process_value(strict_declaration_type)
40
36
  super.tap do |outcome|
41
37
  if outcome.success?
42
38
  type = outcome.result
43
-
44
39
  entity_class = type.target_class
45
40
 
46
- unless entity_class.primary_key_attribute
47
- entity_class.primary_key(strict_declaration_type[:primary_key])
48
- end
49
-
50
41
  unless entity_class.can_be_created_through_casting?
51
42
  type.casters = type.casters.reject do |caster|
52
43
  caster.is_a?(Foobara::BuiltinTypes::Entity::Casters::Hash)
@@ -1,33 +1,8 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- class ValidatePrimaryKeyIsSymbol < TypeDeclarations::TypeDeclarationValidator
6
- class PrimaryKeyNotSymbolError < TypeDeclarationError
7
- class << self
8
- def context_type_declaration
9
- {
10
- primary_key: :duck
11
- }
12
- end
13
- end
14
- end
15
-
16
- def applicable?(strict_type_declaration)
17
- strict_type_declaration.key?(:primary_key)
18
- end
19
-
20
- def validation_errors(strict_type_declaration)
21
- primary_key = strict_type_declaration[:primary_key]
22
- unless primary_key.is_a?(::Symbol)
23
- build_error(
24
- message: "Expected #{primary_key} to be a symbol but it was a #{primary_key.class}",
25
- context: {
26
- primary_key:
27
- }
28
- )
29
- end
30
- end
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class ValidatePrimaryKeyIsSymbol < ExtendDetachedEntityTypeDeclaration::ValidatePrimaryKeyIsSymbol
31
6
  end
32
7
  end
33
8
  end
@@ -1,25 +1,8 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- class ValidatePrimaryKeyPresent < TypeDeclarations::TypeDeclarationValidator
6
- # TODO: seems like maybe we could actually check against types now...
7
- # like make a type for primary_key: :symbol ??
8
- class MissingPrimaryKeyError < TypeDeclarationError; end
9
-
10
- def validation_errors(strict_type_declaration)
11
- unless strict_type_declaration.key?(:primary_key)
12
- build_error
13
- end
14
- end
15
-
16
- def error_message(_value)
17
- "Missing required :primary_key to state which attribute is the primary key"
18
- end
19
-
20
- def error_context(_value)
21
- {}
22
- end
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class ValidatePrimaryKeyPresent < ExtendDetachedEntityTypeDeclaration::ValidatePrimaryKeyPresent
23
6
  end
24
7
  end
25
8
  end
@@ -1,34 +1,9 @@
1
1
  module Foobara
2
2
  module TypeDeclarations
3
3
  module Handlers
4
- class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
5
- class ValidatePrimaryKeyReferencesAttribute < TypeDeclarations::TypeDeclarationValidator
6
- class InvalidPrimaryKeyError < TypeDeclarationError
7
- class << self
8
- def context_type_declaration
9
- {
10
- allowed_primary_keys: [:symbol],
11
- primary_key: :symbol
12
- }
13
- end
14
- end
15
- end
16
-
17
- def applicable?(strict_type_declaration)
18
- strict_type_declaration.key?(:primary_key) && strict_type_declaration[:primary_key].is_a?(::Symbol)
19
- end
20
-
21
- def validation_errors(strict_type_declaration)
22
- allowed_primary_keys = strict_type_declaration[:attributes_declaration][:element_type_declarations].keys
23
-
24
- primary_key = strict_type_declaration[:primary_key]
25
- unless allowed_primary_keys.include?(primary_key)
26
- build_error(
27
- message: "Invalid primary key. Expected #{primary_key} to be one of #{allowed_primary_keys}",
28
- context: { allowed_primary_keys:, primary_key: }
29
- )
30
- end
31
- end
4
+ class ExtendEntityTypeDeclaration < ExtendDetachedEntityTypeDeclaration
5
+ class ValidatePrimaryKeyReferencesAttribute <
6
+ ExtendDetachedEntityTypeDeclaration::ValidatePrimaryKeyReferencesAttribute
32
7
  end
33
8
  end
34
9
  end