jsonapi-resources-anchor 2.12.0 → 2.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/anchor/concerns/typeable.rb +26 -0
  3. data/lib/anchor/config.rb +5 -1
  4. data/lib/anchor/inference/active_record/infer/base.rb +12 -0
  5. data/lib/anchor/inference/active_record/infer/columns.rb +42 -0
  6. data/lib/anchor/inference/active_record/infer/enums.rb +17 -0
  7. data/lib/anchor/inference/active_record/infer/model.rb +64 -0
  8. data/lib/anchor/inference/active_record/infer/rbs.rb +30 -0
  9. data/lib/anchor/inference/active_record/infer.rb +8 -0
  10. data/lib/anchor/inference/active_record/types/base.rb +9 -0
  11. data/lib/anchor/inference/active_record/types/column_comments.rb +29 -0
  12. data/lib/anchor/inference/active_record/types/defaulted.rb +17 -0
  13. data/lib/anchor/inference/active_record/types/overridden.rb +23 -0
  14. data/lib/anchor/inference/active_record/types/presence_required.rb +33 -0
  15. data/lib/anchor/inference/active_record/types/serialized.rb +13 -0
  16. data/lib/anchor/inference/active_record/types.rb +8 -0
  17. data/lib/anchor/inference/jsonapi/infer/anchor_def.rb +86 -0
  18. data/lib/anchor/inference/jsonapi/infer/base.rb +12 -0
  19. data/lib/anchor/inference/jsonapi/infer/rbs.rb +30 -0
  20. data/lib/anchor/inference/jsonapi/infer/relationship_references.rb +38 -0
  21. data/lib/anchor/inference/jsonapi/infer/resource.rb +72 -0
  22. data/lib/anchor/inference/jsonapi/infer/shell.rb +24 -0
  23. data/lib/anchor/inference/jsonapi/infer.rb +8 -0
  24. data/lib/anchor/inference/jsonapi/read_type.rb +47 -0
  25. data/lib/anchor/inference/jsonapi/types/active_record_relationships_wrapper.rb +44 -0
  26. data/lib/anchor/inference/jsonapi/types/anchor_comments.rb +23 -0
  27. data/lib/anchor/inference/jsonapi/types/base.rb +9 -0
  28. data/lib/anchor/inference/jsonapi/types/empty.rb +9 -0
  29. data/lib/anchor/inference/jsonapi/types/overridden.rb +14 -0
  30. data/lib/anchor/inference/jsonapi/types/readable.rb +22 -0
  31. data/lib/anchor/inference/jsonapi/types/relationships_wrapper.rb +29 -0
  32. data/lib/anchor/inference/jsonapi/types.rb +8 -0
  33. data/lib/anchor/json_schema/resource.rb +8 -8
  34. data/lib/anchor/json_schema/schema_generator.rb +1 -3
  35. data/lib/anchor/json_schema/serializer.rb +1 -0
  36. data/lib/anchor/schema.rb +1 -1
  37. data/lib/anchor/schema_generator.rb +1 -2
  38. data/lib/anchor/type_script/file_structure.rb +4 -3
  39. data/lib/anchor/type_script/multifile_schema_generator.rb +1 -4
  40. data/lib/anchor/type_script/resource.rb +17 -15
  41. data/lib/anchor/type_script/schema_generator.rb +1 -3
  42. data/lib/anchor/type_script/serializer.rb +1 -0
  43. data/lib/anchor/types/inference/active_record.rb +11 -35
  44. data/lib/anchor/types/inference/rbs.rb +35 -23
  45. data/lib/anchor/types.rb +123 -11
  46. data/lib/anchor/version.rb +1 -1
  47. data/lib/jsonapi-resources-anchor.rb +35 -2
  48. metadata +32 -4
  49. data/lib/anchor/resource.rb +0 -213
  50. data/lib/anchor/types/inference/jsonapi.rb +0 -14
data/lib/anchor/types.rb CHANGED
@@ -7,23 +7,131 @@ module Anchor
7
7
  class Boolean; end
8
8
  class Null; end
9
9
  class Unknown; end
10
- Record = Struct.new(:value_type)
11
- Maybe = Struct.new(:type)
12
- Array = Struct.new(:type)
13
- Literal = Struct.new(:value)
14
- Union = Struct.new(:types)
15
- Intersection = Struct.new(:types)
16
- Reference = Struct.new(:name) do
17
- def anchor_schema_name
18
- name
10
+ Identity = Data.define(:type)
11
+ Record = Data.define(:value_type)
12
+ Maybe = Data.define(:type)
13
+ Array = Data.define(:type)
14
+ Literal = Data.define(:value)
15
+ Union = Data.define(:types) do
16
+ def |(other)
17
+ self.class.new(types + [other])
19
18
  end
20
19
  end
21
- Property = Struct.new(:name, :type, :optional, :description)
20
+ Intersection = Data.define(:types)
21
+ Reference = Data.define(:name) do
22
+ def anchor_schema_name = name
23
+
24
+ def |(other)
25
+ Anchor::Types::Union.new([self, other])
26
+ end
27
+ end
28
+
29
+ Property = Data.define(:name, :type, :optional, :description) do
30
+ def initialize(name:, type:, optional: false, description: nil)
31
+ super
32
+ end
33
+
34
+ def dup(name: nil, type: nil, optional: nil, description: nil)
35
+ self.class.new(
36
+ name: name || self.name,
37
+ type: type || self.type,
38
+ optional: optional.nil? ? self.optional : optional,
39
+ description: description || self.description,
40
+ )
41
+ end
42
+ end
43
+
22
44
  class Object
23
- attr_reader :properties
45
+ attr_reader :properties, :properties_hash
46
+
47
+ delegate :[], :keys, :key?, to: :properties_hash
24
48
 
25
49
  def initialize(properties)
26
50
  @properties = properties || []
51
+ @properties_hash = properties.index_by(&:name) || []
52
+ end
53
+
54
+ def pick(keys)
55
+ picked = properties_hash.slice(*keys).values
56
+ self.class.new(picked)
57
+ end
58
+
59
+ def omit(keys)
60
+ omitted = properties_hash.except(*keys).values
61
+ self.class.new(omitted)
62
+ end
63
+
64
+ def pick_by_value(t)
65
+ props = properties.filter { |prop| prop.type.is_a?(t) }
66
+ self.class.new(props)
67
+ end
68
+
69
+ def untype(names = nil)
70
+ names ||= keys
71
+ pick(names).overwrite_values(Anchor::Types::Unknown) + omit(names)
72
+ end
73
+
74
+ def overwrite_values(type)
75
+ props = properties.map { |prop| prop.dup(type:) }
76
+ self.class.new(props)
77
+ end
78
+
79
+ def apply_higher(other, keep_description: :right)
80
+ props = properties.filter_map do |prop|
81
+ if (other_prop = other[prop.name])
82
+ desc = keep_description == :right ? other_prop.description : property.description
83
+ Property.new(prop.name, other_prop.type.new(prop.type), prop.optional, desc)
84
+ else
85
+ prop
86
+ end
87
+ end
88
+ self.class.new(props)
89
+ end
90
+
91
+ def overwrite(other, keep_description: :right)
92
+ props = properties.map do |prop|
93
+ if (other_prop = other[prop.name])
94
+ description = keep_description == :left ? prop.description : other_prop.description
95
+ other_prop.dup(description:)
96
+ else
97
+ prop.dup
98
+ end
99
+ end
100
+ self.class.new(props)
101
+ end
102
+
103
+ # left-based union
104
+ def +(other)
105
+ self.class.new(properties + other.omit(keys).properties)
106
+ end
107
+
108
+ def transform_keys
109
+ props = properties.map { |prop| prop.dup(name: yield(prop.name)) }
110
+ self.class.new(props)
111
+ end
112
+
113
+ def camelize
114
+ transform_keys { |name| Anchor::Types.camelize_without_inflection(name) }
115
+ end
116
+
117
+ def convert_case
118
+ transform_keys { |name| Anchor::Types.convert_case(name) }
119
+ end
120
+
121
+ def nonnullable
122
+ props = properties.map do |prop|
123
+ next prop unless prop.type.is_a?(Anchor::Types::Maybe)
124
+ prop.dup(type: prop.type.type)
125
+ end
126
+ self.class.new(props)
127
+ end
128
+
129
+ def nullable_to_optional
130
+ props = properties.map do |prop|
131
+ next prop unless prop.type.is_a?(Anchor::Types::Maybe)
132
+ prop.dup(type: prop.type.type, optional: true)
133
+ end
134
+ self.class.new(props)
27
135
  end
28
136
 
29
137
  class << self
@@ -35,6 +143,10 @@ module Anchor
35
143
  @properties ||= []
36
144
  @properties.push(Property.new(name, type, optional, description))
37
145
  end
146
+
147
+ def camelize
148
+ new(properties.map { |prop| prop.dup(name: Anchor::Types.camelize_without_inflection(prop.name)) })
149
+ end
38
150
  end
39
151
  end
40
152
 
@@ -1,3 +1,3 @@
1
1
  module Anchor
2
- VERSION = "2.12.0"
2
+ VERSION = "2.13.1"
3
3
  end
@@ -1,16 +1,15 @@
1
1
  require "anchor/types"
2
2
  require "anchor/anchor"
3
3
  require "anchor/config"
4
- require "anchor/resource"
5
4
  require "anchor/schema_generator"
6
5
  require "anchor/concerns/annotatable"
6
+ require "anchor/concerns/typeable"
7
7
  require "anchor/concerns/custom_linkable"
8
8
  require "anchor/concerns/custom_meta"
9
9
  require "anchor/concerns/static_context"
10
10
  require "anchor/concerns/type_inferable"
11
11
  require "anchor/concerns/schema_serializable"
12
12
  require "anchor/schema"
13
- require "anchor/types/inference/jsonapi"
14
13
  require "anchor/types/inference/active_record"
15
14
  require "anchor/types/inference/rbs"
16
15
  require "anchor/type_script/file_structure"
@@ -23,3 +22,37 @@ require "anchor/type_script/resource"
23
22
  require "anchor/json_schema/serializer"
24
23
  require "anchor/json_schema/schema_generator"
25
24
  require "anchor/json_schema/resource"
25
+
26
+ require "anchor/inference/active_record/types"
27
+ require "anchor/inference/active_record/types/base"
28
+ require "anchor/inference/active_record/types/column_comments"
29
+ require "anchor/inference/active_record/types/presence_required"
30
+ require "anchor/inference/active_record/types/serialized"
31
+ require "anchor/inference/active_record/types/defaulted"
32
+ require "anchor/inference/active_record/types/overridden"
33
+
34
+ require "anchor/inference/active_record/infer"
35
+ require "anchor/inference/active_record/infer/base"
36
+ require "anchor/inference/active_record/infer/columns"
37
+ require "anchor/inference/active_record/infer/model"
38
+ require "anchor/inference/active_record/infer/enums"
39
+ require "anchor/inference/active_record/infer/rbs"
40
+
41
+ require "anchor/inference/jsonapi/types"
42
+ require "anchor/inference/jsonapi/types/base"
43
+ require "anchor/inference/jsonapi/types/active_record_relationships_wrapper"
44
+ require "anchor/inference/jsonapi/types/relationships_wrapper"
45
+ require "anchor/inference/jsonapi/types/empty"
46
+ require "anchor/inference/jsonapi/types/overridden"
47
+ require "anchor/inference/jsonapi/types/anchor_comments"
48
+ require "anchor/inference/jsonapi/types/readable"
49
+
50
+ require "anchor/inference/jsonapi/infer"
51
+ require "anchor/inference/jsonapi/infer/base"
52
+ require "anchor/inference/jsonapi/infer/anchor_def"
53
+ require "anchor/inference/jsonapi/infer/shell"
54
+ require "anchor/inference/jsonapi/infer/rbs"
55
+ require "anchor/inference/jsonapi/infer/relationship_references"
56
+ require "anchor/inference/jsonapi/infer/resource"
57
+
58
+ require "anchor/inference/jsonapi/read_type"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources-anchor
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.12.0
4
+ version: 2.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-10-14 00:00:00.000000000 Z
11
+ date: 2025-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-resources
@@ -93,11 +93,40 @@ files:
93
93
  - lib/anchor/concerns/schema_serializable.rb
94
94
  - lib/anchor/concerns/static_context.rb
95
95
  - lib/anchor/concerns/type_inferable.rb
96
+ - lib/anchor/concerns/typeable.rb
96
97
  - lib/anchor/config.rb
98
+ - lib/anchor/inference/active_record/infer.rb
99
+ - lib/anchor/inference/active_record/infer/base.rb
100
+ - lib/anchor/inference/active_record/infer/columns.rb
101
+ - lib/anchor/inference/active_record/infer/enums.rb
102
+ - lib/anchor/inference/active_record/infer/model.rb
103
+ - lib/anchor/inference/active_record/infer/rbs.rb
104
+ - lib/anchor/inference/active_record/types.rb
105
+ - lib/anchor/inference/active_record/types/base.rb
106
+ - lib/anchor/inference/active_record/types/column_comments.rb
107
+ - lib/anchor/inference/active_record/types/defaulted.rb
108
+ - lib/anchor/inference/active_record/types/overridden.rb
109
+ - lib/anchor/inference/active_record/types/presence_required.rb
110
+ - lib/anchor/inference/active_record/types/serialized.rb
111
+ - lib/anchor/inference/jsonapi/infer.rb
112
+ - lib/anchor/inference/jsonapi/infer/anchor_def.rb
113
+ - lib/anchor/inference/jsonapi/infer/base.rb
114
+ - lib/anchor/inference/jsonapi/infer/rbs.rb
115
+ - lib/anchor/inference/jsonapi/infer/relationship_references.rb
116
+ - lib/anchor/inference/jsonapi/infer/resource.rb
117
+ - lib/anchor/inference/jsonapi/infer/shell.rb
118
+ - lib/anchor/inference/jsonapi/read_type.rb
119
+ - lib/anchor/inference/jsonapi/types.rb
120
+ - lib/anchor/inference/jsonapi/types/active_record_relationships_wrapper.rb
121
+ - lib/anchor/inference/jsonapi/types/anchor_comments.rb
122
+ - lib/anchor/inference/jsonapi/types/base.rb
123
+ - lib/anchor/inference/jsonapi/types/empty.rb
124
+ - lib/anchor/inference/jsonapi/types/overridden.rb
125
+ - lib/anchor/inference/jsonapi/types/readable.rb
126
+ - lib/anchor/inference/jsonapi/types/relationships_wrapper.rb
97
127
  - lib/anchor/json_schema/resource.rb
98
128
  - lib/anchor/json_schema/schema_generator.rb
99
129
  - lib/anchor/json_schema/serializer.rb
100
- - lib/anchor/resource.rb
101
130
  - lib/anchor/schema.rb
102
131
  - lib/anchor/schema_generator.rb
103
132
  - lib/anchor/type_script/file_structure.rb
@@ -109,7 +138,6 @@ files:
109
138
  - lib/anchor/type_script/types.rb
110
139
  - lib/anchor/types.rb
111
140
  - lib/anchor/types/inference/active_record.rb
112
- - lib/anchor/types/inference/jsonapi.rb
113
141
  - lib/anchor/types/inference/rbs.rb
114
142
  - lib/anchor/version.rb
115
143
  - lib/jsonapi-resources-anchor.rb
@@ -1,213 +0,0 @@
1
- module Anchor
2
- class Resource
3
- delegate_missing_to :@resource_klass
4
- attr_reader :resource_klass
5
-
6
- # resource_klass#anchor_attributes, #anchor_relationships, #anchor_attributes_descriptions,
7
- # #anchor_relationships_descriptions are optional methods from Anchor::Annotatable.
8
- # @param [JSONAPI::Resource] Must include Anchor::TypeInferable
9
- def initialize(resource_klass)
10
- @resource_klass = resource_klass
11
- @anchor_attributes = resource_klass.try(:anchor_attributes) || {}
12
- @anchor_relationships = resource_klass.try(:anchor_relationships) || {}
13
- @anchor_attributes_descriptions = resource_klass.try(:anchor_attributes_descriptions) || {}
14
- @anchor_relationships_descriptions = resource_klass.try(:anchor_relationships_descriptions) || {}
15
- @anchor_method_added_count = resource_klass.anchor_method_added_count || Hash.new(0)
16
- @anchor_links_schema = resource_klass.try(:anchor_links_schema) || nil
17
- @anchor_meta_schema = resource_klass.try(:anchor_meta_schema) || nil
18
- end
19
-
20
- def express(...)
21
- raise NotImplementedError
22
- end
23
-
24
- private
25
-
26
- delegate :convert_case, to: Anchor::Types
27
-
28
- def schema_fetchable_fields(context:, include_all_fields:)
29
- return fields unless statically_determinable_fetchable_fields? && !include_all_fields
30
- @resource_klass.anchor_fetchable_fields(context)
31
- end
32
-
33
- def statically_determinable_fetchable_fields?
34
- @resource_klass.singleton_class.method_defined?(:anchor_fetchable_fields)
35
- end
36
-
37
- # @return [Anchor::Types::Property]
38
- def id_property
39
- # TODO: resource_key_type can also return a proc
40
- res_key_type = case resource_key_type
41
- when :integer then Anchor::Types::Integer
42
- else Anchor::Types::String
43
- end
44
-
45
- Anchor::Types::Property.new(:id, res_key_type)
46
- end
47
-
48
- # @return [Anchor::Types::Property]
49
- def type_property
50
- Anchor::Types::Property.new(:type, Anchor::Types::Literal.new(_type))
51
- end
52
-
53
- # @param included_fields [Array<Symbol>]
54
- # @return [Array<Anchor::Types::Property>]
55
- def anchor_attributes_properties(included_fields:)
56
- _attributes.except(:id).filter_map do |attr, options|
57
- next if included_fields.exclude?(attr.to_sym)
58
- description = @anchor_attributes_descriptions[attr]
59
- next Anchor::Types::Property.new(
60
- convert_case(attr),
61
- @anchor_attributes[attr],
62
- false,
63
- description,
64
- ) if @anchor_attributes.key?(attr)
65
-
66
- type = begin
67
- model_method = options[:delegate] || attr
68
- resource_method = attr
69
-
70
- model_method_defined = _model_class.try(
71
- :method_defined?,
72
- model_method.to_sym,
73
- ) && !_model_class.instance_method(model_method.to_sym)
74
- .owner.is_a?(ActiveRecord::AttributeMethods::GeneratedAttributeMethods)
75
- resource_method_defined = @anchor_method_added_count[resource_method.to_sym] > 1
76
- serializer_defined = (_model_class.try(:attribute_types) || {})[model_method.to_s].respond_to?(:coder)
77
- method_defined = model_method_defined || resource_method_defined || serializer_defined
78
-
79
- enum = Anchor.config.infer_ar_enums && !method_defined && _model_class.try(:defined_enums).try(:[], model_method.to_s)
80
- column = !method_defined && _model_class.try(:columns_hash).try(:[], model_method.to_s)
81
-
82
- rbs_defined = defined?(::RBS) && ::RBS::VERSION.first == "3"
83
-
84
- if resource_method_defined && rbs_defined
85
- Anchor::Types::Inference::RBS.from(name.to_sym, resource_method)
86
- elsif model_method_defined && rbs_defined
87
- Anchor::Types::Inference::RBS.from(_model_class.name.to_sym, model_method.to_sym)
88
- elsif column
89
- type = Anchor::Types::Inference::ActiveRecord::SQL.from(column)
90
-
91
- if enum
92
- enum_type = Anchor::Types::Union.new(enum.map { |_key, val| Anchor::Types::Literal.new(val) })
93
- type = type.is_a?(Anchor::Types::Maybe) ? Anchor::Types::Maybe.new(enum_type) : enum_type
94
- end
95
-
96
- unless description
97
- description = column.comment if Anchor.config.use_active_record_comment
98
- if description && !Anchor.config.ar_comment_to_string.nil?
99
- description = Anchor.config.ar_comment_to_string.call(description)
100
- end
101
- end
102
- check_presence = type.is_a?(Anchor::Types::Maybe) && Anchor.config.use_active_record_validations
103
- if check_presence && _model_class.validators_on(model_method).any? do |v|
104
- if v.is_a?(ActiveRecord::Validations::NumericalityValidator)
105
- opts = v.options.with_indifferent_access
106
- !(opts[:allow_nil] || opts[:if] || opts[:unless] || opts[:on])
107
- elsif v.is_a?(ActiveRecord::Validations::PresenceValidator)
108
- opts = v.options.with_indifferent_access
109
- !(opts[:if] || opts[:unless] || opts[:on])
110
- end
111
- end
112
- type.type
113
- elsif type.is_a?(Anchor::Types::Maybe) && Anchor.config.infer_default_as_non_null
114
- column.default.present? || column.default_function.present? && column.instance_variable_get(:@generated).blank? ? type.type : type
115
- else
116
- type
117
- end
118
- elsif rbs_defined
119
- # TODO: Methods may not be defined on the class at generation time?
120
- [
121
- Anchor::Types::Inference::RBS.from(name.to_sym, resource_method),
122
- Anchor::Types::Inference::RBS.from(_model_class.name.to_sym, model_method.to_sym),
123
- ].find { |t| t != Anchor::Types::Unknown } || Anchor::Types::Unknown
124
- else
125
- Anchor::Types::Unknown
126
- end
127
- end
128
-
129
- Anchor::Types::Property.new(convert_case(attr), type, false, description)
130
- end
131
- end
132
-
133
- # @param included_fields [Array<Symbol>]
134
- # @return [Anchor::Types::Property, NilClass]
135
- def anchor_relationships_property(included_fields:)
136
- anchor_relationships_properties(included_fields:).then do |properties|
137
- break if properties.blank?
138
- Anchor::Types::Property.new(:relationships, Anchor::Types::Object.new(properties))
139
- end
140
- end
141
-
142
- def anchor_links_property
143
- if @anchor_links_schema
144
- Anchor::Types::Property.new("links", @anchor_links_schema, false)
145
- end
146
- end
147
-
148
- def anchor_meta_property
149
- if @anchor_meta_schema
150
- Anchor::Types::Property.new("meta", @anchor_meta_schema, false)
151
- end
152
- end
153
-
154
- # @param included_fields [Array<Symbol>]
155
- # @return [Array<Anchor::Types::Property>]
156
- def anchor_relationships_properties(included_fields:)
157
- _relationships.filter_map do |name, rel|
158
- next if included_fields.exclude?(name.to_sym)
159
- description = @anchor_relationships_descriptions[name]
160
- relationship_type = relationship_type_for(rel, rel.resource_klass, name) if @anchor_relationships.exclude?(name)
161
-
162
- relationship_type ||= begin
163
- anchor_relationship = @anchor_relationships[name]
164
-
165
- type = if (resources = anchor_relationship.resources)
166
- references = resources.map do |resource_klass|
167
- Anchor::Types::Reference.new(resource_klass.anchor_schema_name)
168
- end
169
- null_type = Array.wrap(anchor_relationship.null_elements.presence && Anchor::Types::Null)
170
- Anchor::Types::Union.new(references + null_type)
171
- else
172
- Anchor::Types::Reference.new(anchor_relationship.resource.anchor_schema_name)
173
- end
174
-
175
- type = Anchor::Types::Inference::JSONAPI.wrapper_from_relationship(rel).call(type)
176
- anchor_relationship.null.present? ? Anchor::Types::Maybe.new(type) : type
177
- end
178
-
179
- use_optional = Anchor.config.infer_nullable_relationships_as_optional
180
- if use_optional && relationship_type.is_a?(Anchor::Types::Maybe)
181
- Anchor::Types::Property.new(convert_case(name), relationship_type.type, true, description)
182
- else
183
- Anchor::Types::Property.new(convert_case(name), relationship_type, false, description)
184
- end
185
- end
186
- end
187
-
188
- # @param rel [Relationship]
189
- # @param resource_klass [Anchor::Resource]
190
- # @param name [String, Symbol]
191
- # @return [Anchor::Types::Reference, Anchor::Types::Array<Anchor::Types::Reference>, Anchor::Types::Maybe<Anchor::Types::Reference>, Anchor::Types::Union<Anchor::Types::Reference>]
192
- def relationship_type_for(rel, resource_klass, name)
193
- rel_type = if rel.polymorphic? && rel.respond_to?(:polymorphic_types) # 0.11.0.beta2
194
- resource_klasses = rel.polymorphic_types.map { |t| resource_klass_for(t) }
195
- Anchor::Types::Union.new(resource_klasses.map { |rk| Anchor::Types::Reference.new(rk.anchor_schema_name) })
196
- elsif rel.polymorphic? && rel.class.respond_to?(:polymorphic_types) # TODO: < 0.11.0.beta2
197
- resource_klasses = rel.class.polymorphic_types.map { |t| resource_klass_for(t) }
198
- Anchor::Types::Union.new(resource_klasses.map { |rk| Anchor::Types::Reference.new(rk.anchor_schema_name) })
199
- end
200
-
201
- rel_type ||= Anchor::Types::Reference.new(resource_klass.anchor_schema_name)
202
- model_relationship_name = (rel.options[:relation_name] || name).to_s
203
- reflection = _model_class.try(:reflections).try(:[], model_relationship_name)
204
- wrapper = if reflection
205
- Anchor::Types::Inference::ActiveRecord.wrapper_from_reflection(reflection)
206
- else
207
- Anchor::Types::Inference::JSONAPI.wrapper_from_relationship(rel)
208
- end
209
-
210
- wrapper.call(rel_type)
211
- end
212
- end
213
- end
@@ -1,14 +0,0 @@
1
- module Anchor::Types::Inference
2
- module JSONAPI
3
- class << self
4
- # @return [Proc{Type => Type, Anchor::Types::Array<Type>}]
5
- def wrapper_from_relationship(relationship)
6
- case relationship
7
- when ::JSONAPI::Relationship::ToOne then ->(type) { type }
8
- when ::JSONAPI::Relationship::ToMany then ->(type) { Anchor::Types::Array.new(type) }
9
- else raise "#{relationship.class.name} not supported"
10
- end
11
- end
12
- end
13
- end
14
- end