foobara 0.0.47 → 0.0.49

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b307418792bdb9ffc486a5f7280d67838100daac32a7826277612121612304a9
4
- data.tar.gz: e6e6c541731cadc39c9c64c9f759c3d347e291fa9731351d33248d9b27ec8de5
3
+ metadata.gz: c906fa3ff55b19d33976a861a7d249aaf8b869507ba845cd8743459653fc117f
4
+ data.tar.gz: 86da929e2ed3d0b478067f27e7605060941a593eb8c5c95298304d3cf048fc45
5
5
  SHA512:
6
- metadata.gz: d76ba08fc4bc43e51a3c99ec9a780da84e57e9b15d6b29b1782e916ace9adbc198f6e1723852232e4c04766a5979c367bdc1dae6ec020ec3d5d742c61bb4a667
7
- data.tar.gz: 95a5ae616494aa957e57e465700a7330b1224bf2d4bb43482f5c13e35a5cabfa63b74e239492136a0e1d75f44c578b0b9a02abc7de64a0cadfd8053769de9341
6
+ metadata.gz: 04424105e5a0d9788839c40c3689ae34d8f1c39192128cf6e30a8c85e326fb3f5aa0b02a11cec7ac374ae62862cd941bb774db423d01e6e912b4a1a21977b3c2
7
+ data.tar.gz: 4030da0e998f344b19a453a624481189dbf432654c8af7a63a5197eeeb281d7a64df683c4babfcb59abad342a5b436036d61eda8bb760a0d011aa30cac1b780b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [0.0.49] - 2025-01-21
2
+
3
+ - Fill out more foobara_ model attribute helper methods to further support the ActiveRecordType gem
4
+
5
+ ## [0.0.48] - 2025-01-17
6
+
7
+ - Add support for foobara_attributes_type to allow ActiveRecordType gem to work
8
+
1
9
  ## [0.0.47] - 2025-01-03
2
10
 
3
11
  - Bumped Ruby from 3.2.2 to 3.4.1
@@ -91,7 +91,7 @@ module Foobara
91
91
 
92
92
  error_class = self.class.lookup_input_error_class(symbol, path)
93
93
  error_class.new(**error_args, path:)
94
- elsif args.size == 2 || args.size == 3
94
+ elsif [2, 3].include?(args.size)
95
95
  input, symbol, message = args
96
96
  context = opts
97
97
 
@@ -143,7 +143,7 @@ module Foobara
143
143
 
144
144
  error_class = self.class.lookup_runtime_error_class(symbol)
145
145
  error_class.new(**error_args)
146
- elsif args.is_a?(::Array) && (args.size == 1 || args.size == 2)
146
+ elsif args.is_a?(::Array) && [1, 2].include?(args.size)
147
147
  symbol, message = args
148
148
  context = opts
149
149
 
@@ -2,6 +2,8 @@ module Foobara
2
2
  class Command
3
3
  module Concerns
4
4
  module Inputs
5
+ class UnexpectedInputValidationError < StandardError; end
6
+
5
7
  include Concern
6
8
 
7
9
  attr_reader :inputs
@@ -41,9 +43,8 @@ module Foobara
41
43
  if error.is_a?(Value::DataError)
42
44
  add_input_error(error)
43
45
  else
44
- # TODO: raise a real error
45
46
  # :nocov:
46
- raise "wtf"
47
+ raise UnexpectedInputValidationError, "Unexpected input validation error: #{error}"
47
48
  # :nocov:
48
49
  end
49
50
  end
@@ -5,10 +5,12 @@ module Foobara
5
5
  include Concern
6
6
 
7
7
  module ClassMethods
8
- def associations
9
- @associations ||= construct_associations
8
+ def foobara_associations
9
+ @foobara_associations ||= construct_associations
10
10
  end
11
11
 
12
+ alias associations foobara_associations
13
+
12
14
  def deep_associations
13
15
  @deep_associations ||= begin
14
16
  deep = {}
@@ -109,6 +111,8 @@ module Foobara
109
111
  end
110
112
  end
111
113
 
114
+ # TODO: this big switch is a problem. Hard to create new types in other projects without being able
115
+ # to modify this switch. Figure out what to do.
112
116
  def construct_associations(
113
117
  type = attributes_type,
114
118
  path = DataPath.new,
@@ -135,7 +139,12 @@ module Foobara
135
139
  construct_associations(element_type, path.append(attribute_name), result)
136
140
  end
137
141
  elsif type.extends?(BuiltinTypes[:model])
138
- construct_associations(type.target_class.attributes_type, path, result)
142
+ target_class = type.target_class
143
+
144
+ method = target_class.respond_to?(:foobara_attributes_type) ? :foobara_attributes_type : :attributes_type
145
+ attributes_type = target_class.send(method)
146
+
147
+ construct_associations(attributes_type, path, result)
139
148
  elsif type.extends?(BuiltinTypes[:associative_array])
140
149
  # not going to bother testing this for now
141
150
  # :nocov:
@@ -20,18 +20,20 @@ module Foobara
20
20
  # :nocov:
21
21
  end
22
22
 
23
- @primary_key_attribute = attribute_name.to_sym
23
+ @foobara_primary_key_attribute = attribute_name.to_sym
24
24
 
25
25
  set_model_type
26
26
  end
27
27
 
28
- def primary_key_attribute
29
- return @primary_key_attribute if @primary_key_attribute
28
+ def foobara_primary_key_attribute
29
+ return @foobara_primary_key_attribute if @foobara_primary_key_attribute
30
30
 
31
31
  unless superclass == DetachedEntity
32
- @primary_key_attribute = superclass.primary_key_attribute
32
+ @foobara_primary_key_attribute = superclass.primary_key_attribute
33
33
  end
34
34
  end
35
+
36
+ alias primary_key_attribute foobara_primary_key_attribute
35
37
  end
36
38
 
37
39
  def primary_key
@@ -23,10 +23,12 @@ module Foobara
23
23
  end
24
24
  end
25
25
 
26
- def primary_key_type
27
- @primary_key_type ||= attributes_type.element_types[primary_key_attribute]
26
+ def foobara_primary_key_type
27
+ @foobara_primary_key_type ||= attributes_type.element_types[primary_key_attribute]
28
28
  end
29
29
 
30
+ alias primary_key_type foobara_primary_key_type
31
+
30
32
  def full_entity_name
31
33
  full_model_name
32
34
  end
@@ -7,7 +7,6 @@ module Foobara
7
7
  include Concerns::Attributes
8
8
  include Concerns::Persistence
9
9
  include Concerns::Initialization
10
- include Concerns::AttributeHelpers
11
10
  include Concerns::Types
12
11
 
13
12
  class << self
@@ -30,6 +30,7 @@ module Foobara
30
30
  "model",
31
31
  "detached_entity",
32
32
  "entity",
33
+ "model_attribute_helpers",
33
34
  "command",
34
35
  "domain_mapper",
35
36
  "persistence",
@@ -53,7 +53,7 @@ module Foobara
53
53
  end
54
54
 
55
55
  def model?
56
- return false if reference == "entity" || reference == "detached_entity"
56
+ return false if %w[entity detached_entity].include?(reference)
57
57
 
58
58
  type = base_type
59
59
 
@@ -102,7 +102,7 @@ module Foobara
102
102
  )
103
103
  end
104
104
 
105
- def attributes_type
105
+ def foobara_attributes_type
106
106
  return @attributes_type if @attributes_type
107
107
 
108
108
  @attributes_type = if model_type
@@ -112,6 +112,8 @@ module Foobara
112
112
  end
113
113
  end
114
114
 
115
+ alias attributes_type foobara_attributes_type
116
+
115
117
  def model_type=(model_type)
116
118
  @model_type = model_type
117
119
 
@@ -0,0 +1,16 @@
1
+ require "date"
2
+ require "time"
3
+ require "bigdecimal"
4
+
5
+ module Foobara
6
+ module ModelAttributeHelpers
7
+ class << self
8
+ def install!
9
+ Model.include Concerns::AttributeHelpers
10
+ Model.include Concerns::AttributeHelperAliases
11
+ Entity.include Concerns::AttributeHelpers
12
+ Entity.include Concerns::AttributeHelperAliases
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ # This is currently the working file, fix this up/move it
2
+
3
+ module Foobara
4
+ module ModelAttributeHelpers
5
+ module Concerns
6
+ module AttributeHelperAliases
7
+ include Foobara::Concern
8
+
9
+ module ClassMethods
10
+ %i[
11
+ attributes_for_update
12
+ type_from_foobara_model_class
13
+ attributes_type_from_foobara_model_class
14
+ primary_key_attribute_from_foobara_model_class
15
+ foobara_model_class_has_primary_key
16
+ attributes_for_create
17
+ attributes_for_aggregate_update
18
+ attributes_for_atom_update
19
+ attributes_for_find_by
20
+ type_declaration_value_at
21
+ ].each do |method_name|
22
+ define_method method_name do |*args, **opts, &block|
23
+ send("foobara_#{method_name}", *args, **opts, &block)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,37 +1,56 @@
1
+ # delete this file
2
+
1
3
  module Foobara
2
- class Entity < DetachedEntity
4
+ module ModelAttributeHelpers
3
5
  module Concerns
6
+ # TODO: This concern is retroactively designed to be mixed into any entity-like class that can hold an
7
+ # entity-like foobara type.
8
+ # Because we want a subclass of ActiveRecord::Base to be such a target in the case of the
9
+ # foobara-active-record-type class, but because we can't make ActiveRecord::Base a subclass of Entity or
10
+ # DetachedEntity, and also because it makes sense to extend such behavior to foobara model classes,
11
+ # we will implement this as a Concern/mixin with a published expected interface and prefixed methods.
12
+ # This also means it should live in its own project, not here in the entity project.
13
+ # required methods:
14
+ #
15
+ # foobara_type
16
+ # foobara_attributes_type
17
+ # foobara_primary_key_attribute (nil if not an entity type)
18
+ # foobara_primary_key_type (nil if not an entity type)
19
+ # foobara_associations
4
20
  module AttributeHelpers
5
21
  include Foobara::Concern
6
22
 
7
23
  module ClassMethods
8
- def attributes_for_update
9
- attributes_for_aggregate_update
24
+ def foobara_has_primary_key?
25
+ respond_to?(:foobara_primary_key_attribute)
26
+ end
27
+
28
+ def foobara_attributes_for_update
29
+ foobara_attributes_for_aggregate_update
10
30
  end
11
31
 
12
32
  # TODO: we should have metadata on the entity about whether it required a primary key
13
33
  # upon creation or not instead of an option here.
14
- def attributes_for_create(includes_primary_key: false)
15
- return attributes_type if includes_primary_key
34
+ def foobara_attributes_for_create(includes_primary_key: false)
35
+ return foobara_attributes_type if includes_primary_key
36
+ return foobara_attributes_type unless foobara_has_primary_key?
37
+
38
+ primary_key_attribute = foobara_primary_key_attribute
16
39
 
17
40
  declaration = attributes_type.declaration_data
18
41
  # TODO: just slice out the element type declarations
19
42
  declaration = Util.deep_dup(declaration)
20
43
 
21
- if declaration.key?(:required) && declaration[:required].include?(primary_key_attribute)
44
+ declaration[:element_type_declarations].delete(primary_key_attribute)
45
+
46
+ if declaration.key?(:required)
22
47
  declaration[:required].delete(primary_key_attribute)
23
48
  end
24
49
 
25
- if declaration.key?(:defaults) && declaration[:defaults].include?(primary_key_attribute)
50
+ if declaration.key?(:defaults)
26
51
  declaration[:defaults].delete(primary_key_attribute)
27
52
  end
28
53
 
29
- if declaration.key?(:element_type_declarations)
30
- if declaration[:element_type_declarations].key?(primary_key_attribute)
31
- declaration[:element_type_declarations].delete(primary_key_attribute)
32
- end
33
- end
34
-
35
54
  handler = Domain.global.foobara_type_builder.handler_for_class(
36
55
  TypeDeclarations::Handlers::ExtendAttributesTypeDeclaration
37
56
  )
@@ -39,48 +58,50 @@ module Foobara
39
58
  handler.desugarize(declaration)
40
59
  end
41
60
 
42
- def attributes_for_aggregate_update(initial = true)
43
- declaration = attributes_type.declaration_data
61
+ def foobara_attributes_for_aggregate_update(initial = true)
62
+ declaration = foobara_attributes_type.declaration_data
44
63
  # TODO: just slice out the element type declarations
45
64
  declaration = Util.deep_dup(declaration)
46
65
 
47
66
  declaration.delete(:defaults)
48
67
  declaration.delete(:required)
49
68
 
50
- if initial
51
- declaration[:required] = [primary_key_attribute]
69
+ if initial && foobara_has_primary_key?
70
+ declaration[:required] = [foobara_primary_key_attribute]
52
71
  end
53
72
 
54
- associations.each_pair do |data_path, type|
73
+ foobara_associations.each_pair do |data_path, type|
55
74
  if type.extends?(BuiltinTypes[:entity])
56
75
  target_class = type.target_class
57
76
 
58
- entry = type_declaration_value_at(declaration, DataPath.new(data_path).path)
77
+ entry = foobara_type_declaration_value_at(declaration, DataPath.new(data_path).path)
59
78
  entry.clear
60
- entry.merge!(target_class.attributes_for_aggregate_update(false))
79
+ entry.merge!(target_class.foobara_attributes_for_aggregate_update(false))
61
80
  end
62
81
  end
63
82
 
64
83
  declaration
65
84
  end
66
85
 
67
- def attributes_for_atom_update
68
- declaration = attributes_type.declaration_data
86
+ def foobara_attributes_for_atom_update
87
+ declaration = foobara_attributes_type.declaration_data
69
88
  # TODO: just slice out the element type declarations
70
89
  declaration = Util.deep_dup(declaration)
71
90
 
72
91
  declaration.delete(:defaults)
73
- declaration[:required] = [primary_key_attribute]
92
+ if foobara_has_primary_key?
93
+ declaration[:required] = [foobara_primary_key_attribute]
94
+ end
74
95
 
75
96
  # expect all associations to be expressed as primary key values
76
97
  # TODO: should we have a special type for encapsulating primary keys types??
77
- associations.each_pair do |data_path, type|
98
+ foobara_associations.each_pair do |data_path, type|
78
99
  if type.extends?(BuiltinTypes[:entity])
79
100
  target_class = type.target_class
80
101
  # TODO: do we really need declaration_data? Why cant we use the type directly?
81
102
  # TODO: make this work with the type directly for performance reasons.
82
- primary_key_type_declaration = target_class.primary_key_type.declaration_data
83
- entry = type_declaration_value_at(declaration, DataPath.new(data_path).path)
103
+ primary_key_type_declaration = target_class.foobara_primary_key_type.declaration_data
104
+ entry = foobara_type_declaration_value_at(declaration, DataPath.new(data_path).path)
84
105
  entry.clear
85
106
  entry.merge!(primary_key_type_declaration)
86
107
  end
@@ -89,10 +110,10 @@ module Foobara
89
110
  declaration
90
111
  end
91
112
 
92
- def attributes_for_find_by
113
+ def foobara_attributes_for_find_by
93
114
  element_type_declarations = {}
94
115
 
95
- attributes_type.element_types.each_pair do |attribute_name, attribute_type|
116
+ foobara_attributes_type.element_types.each_pair do |attribute_name, attribute_type|
96
117
  element_type_declarations[attribute_name] = attribute_type.reference_or_declaration_data
97
118
  end
98
119
 
@@ -108,7 +129,7 @@ module Foobara
108
129
 
109
130
  private
110
131
 
111
- def type_declaration_value_at(declaration, path_parts)
132
+ def foobara_type_declaration_value_at(declaration, path_parts)
112
133
  return declaration if path_parts.empty?
113
134
 
114
135
  path_part, *path_parts = path_parts
@@ -124,7 +145,7 @@ module Foobara
124
145
  # :nocov:
125
146
  end
126
147
 
127
- type_declaration_value_at(declaration, path_parts)
148
+ foobara_type_declaration_value_at(declaration, path_parts)
128
149
  end
129
150
  end
130
151
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.47
4
+ version: 0.0.49
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-04 00:00:00.000000000 Z
10
+ date: 2025-01-21 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bigdecimal
@@ -219,7 +219,6 @@ files:
219
219
  - projects/domain_mapper/src/domain_mapper.rb
220
220
  - projects/domain_mapper/src/domain_mapper_lookups.rb
221
221
  - projects/entity/lib/foobara/entity.rb
222
- - projects/entity/src/concerns/attribute_helpers.rb
223
222
  - projects/entity/src/concerns/attributes.rb
224
223
  - projects/entity/src/concerns/callbacks.rb
225
224
  - projects/entity/src/concerns/initialization.rb
@@ -289,6 +288,9 @@ files:
289
288
  - projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/to_type_transformer.rb
290
289
  - projects/model/src/extensions/type_declarations/handlers/registered_type_declaration/model_class_desugarizer.rb
291
290
  - projects/model/src/model.rb
291
+ - projects/model_attribute_helpers/lib/foobara/model_attribute_helpers.rb
292
+ - projects/model_attribute_helpers/src/attribute_helper_aliases.rb
293
+ - projects/model_attribute_helpers/src/attribute_helpers.rb
292
294
  - projects/monorepo/lib/foobara/monorepo.rb
293
295
  - projects/monorepo/lib/foobara/monorepo/project.rb
294
296
  - projects/namespace/lib/foobara/namespace.rb
@@ -415,6 +417,7 @@ require_paths:
415
417
  - "./projects/in_memory_crud_driver_minimal/lib"
416
418
  - "./projects/manifest/lib"
417
419
  - "./projects/model/lib"
420
+ - "./projects/model_attribute_helpers/lib"
418
421
  - "./projects/monorepo/lib"
419
422
  - "./projects/namespace/lib"
420
423
  - "./projects/persistence/lib"