sober_swag 0.9.0 → 0.14.0

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: 9eb0df290ab4fa245edd30b31ac7ba60ab590ad0586ad343183fa9ba1d8ef7a7
4
- data.tar.gz: 836911a79386af53ccccaf7bbe09d108d7804ad6eb482ad8cca3dfb57b8b69fb
3
+ metadata.gz: 234da6bca56fd440edae30e4fc9c34c50a37a6ab26b41739276e57ab43c73408
4
+ data.tar.gz: 103e61429365ceb04d6d0225822ddaa4012f95a043a5bb97c6b8a2fd26671ce5
5
5
  SHA512:
6
- metadata.gz: afd6d4975c6328e6496ed551d8e6421cc7d93d39e49445e5253fa9f82124a3b2eeb38ef3c4e8fec8522242a2eb98f2fe3d2a37b7950778e4648b64f3ebd59afb
7
- data.tar.gz: 8ca0acee27f790c58c8d5ac236f2346dc1286ff338673678c9602f2f109b4273e568e1c7143187c1b10e3fa740c7cfa3a9bf3d1ff22d29a35f2107bcb5fe235a
6
+ metadata.gz: 58f27a7deebfcb50ec6279084e0a0e872fd0c2a447fca922549d7d3d9822a7e13e010372376c1a079472ba807845d267327275fe453efb99662f685321041e28
7
+ data.tar.gz: 97ecdb6352db7545004fba2103bb505b3730973a22689f05dedd5b6a305115a63f302f0f248edd3f70e6377ebe470c9ef4f734b2e38af031bf762d229c71bcc0
@@ -126,7 +126,9 @@ module SoberSwag
126
126
 
127
127
  case type
128
128
  when Class
129
- { :$ref => self.class.get_ref(type) }
129
+ # refs have to be standalone
130
+ # so to not interefere with our other stuff, do this horrible garbage
131
+ { oneOf: [{ '$ref'.to_sym => self.class.get_ref(type) }] }
130
132
  when Dry::Types::Constrained
131
133
  self.class.new(type.type).schema_stub
132
134
  when Dry::Types::Array::Member
@@ -220,19 +222,20 @@ module SoberSwag
220
222
  else
221
223
  [name, value]
222
224
  end
223
- # can't match on value directly as ruby uses `===` to match,
224
- # and classes use `===` to mean `is an instance of`, as
225
- # opposed to direct equality lmao
226
225
  when Nodes::Primitive
227
226
  value = object.value
228
227
  metadata = object.metadata
229
- if self.class.primitive?(value)
230
- md = self.class.primitive_def(value)
231
- METADATA_KEYS.select(&metadata.method(:key?)).reduce(md) do |definition, key|
232
- definition.merge(key => metadata[key])
228
+ type_def =
229
+ if self.class.primitive?(value)
230
+ self.class.primitive_def(value)
231
+ else
232
+ metadata.merge!(value.meta)
233
+ # refs have to be on their own, this is the stupid workaround
234
+ # so you can add descriptions and stuff
235
+ { oneOf: [{ '$ref'.to_sym => self.class.get_ref(value) }] }
233
236
  end
234
- else
235
- { '$ref': self.class.get_ref(value) }
237
+ METADATA_KEYS.select(&metadata.method(:key?)).reduce(type_def) do |definition, key|
238
+ definition.merge(key => metadata[key])
236
239
  end
237
240
  else
238
241
  raise ArgumentError, "Got confusing node #{object} (#{object.class})"
@@ -13,9 +13,16 @@ module SoberSwag
13
13
  # The name to use for this type in external documentation.
14
14
  def identifier(arg = nil)
15
15
  @identifier = arg if arg
16
+
16
17
  @identifier || name.to_s.gsub('::', '.')
17
18
  end
18
19
 
20
+ def meta(*args)
21
+ super(*args).tap do |result|
22
+ result.identifier(identifier) if result.is_a?(Class) # pass on identifier
23
+ end
24
+ end
25
+
19
26
  def primitive(sym)
20
27
  SoberSwag::Types.const_get(sym)
21
28
  end
@@ -92,6 +92,10 @@ module SoberSwag
92
92
  end
93
93
  end
94
94
 
95
+ def to_s
96
+ "<SoberSwag::OutputObject(#{identifier})>"
97
+ end
98
+
95
99
  def base_serializer
96
100
  @base_serializer ||= SoberSwag::Serializer::FieldList.new(fields).tap do |s|
97
101
  s.identifier(identifier)
@@ -41,6 +41,10 @@ module SoberSwag
41
41
  @fields << field
42
42
  end
43
43
 
44
+ def to_s
45
+ "<SoberSwag::OutputObject::View(#{identifier})>"
46
+ end
47
+
44
48
  ##
45
49
  # Get the serializer defined by this view.
46
50
  # WARNING: Don't add more fields after you call this.
@@ -46,8 +46,9 @@ module SoberSwag
46
46
  when Dry::Types::Constrained
47
47
  bind(Parser.new(@node.type))
48
48
  when Dry::Types::Nominal
49
+ old_meta = @node.primitive.respond_to?(:meta) ? @node.primitive.meta : {}
49
50
  # start off with the moral equivalent of NodeTree[String]
50
- Nodes::Primitive.new(@node.primitive, @node.meta)
51
+ Nodes::Primitive.new(@node.primitive, old_meta.merge(@node.meta))
51
52
  else
52
53
  # Inside of this case we have a class that is some user-defined type
53
54
  # We put it in our array of found types, and consider it a primitive
@@ -6,25 +6,36 @@ module SoberSwag
6
6
  class Meta < Base
7
7
  def initialize(base, meta)
8
8
  @base = base
9
- @meta = meta
9
+ @metadata = meta
10
+ @identifier = @base.identifier
10
11
  end
11
12
 
12
- attr_reader :base, :meta
13
+ attr_reader :base, :metadata
13
14
 
14
15
  def serialize(args, opts = {})
15
16
  base.serialize(args, opts)
16
17
  end
17
18
 
19
+ def meta(hash)
20
+ self.class.new(base, metadata.merge(hash))
21
+ end
22
+
18
23
  def lazy_type
19
- @base.lazy_type.meta(**meta)
24
+ @lazy_type ||= @base.lazy_type.meta(**metadata)
20
25
  end
21
26
 
22
27
  def type
23
- @base.type.meta(**meta)
28
+ @type ||= @base.type.meta(**metadata)
24
29
  end
25
30
 
26
31
  def finalize_lazy_type!
27
32
  @base.finalize_lazy_type!
33
+ # Using .meta on dry-struct returns a *new type* that wraps the old one.
34
+ # As such, we need to be a bit clever about when we tack on the identifier
35
+ # for this type.
36
+ %i[lazy_type type].each do |sym|
37
+ public_send(sym).identifier(@base.public_send(sym).identifier) if @base.public_send(sym).respond_to?(:identifier)
38
+ end
28
39
  end
29
40
 
30
41
  def lazy_type?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SoberSwag
4
- VERSION = '0.9.0'
4
+ VERSION = '0.14.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sober_swag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Super
@@ -183,7 +183,6 @@ files:
183
183
  - LICENSE.txt
184
184
  - README.md
185
185
  - Rakefile
186
- - bin/console
187
186
  - bin/setup
188
187
  - docs/serializers.md
189
188
  - example/.gitignore
@@ -1,48 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'sober_swag'
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
- module Types
10
- include Dry.Types()
11
- end
12
-
13
- Bio = SoberSwag.input_object do
14
- attribute :name, primitive(:String).meta(description: 'A very basic bio name')
15
- end
16
-
17
- Person = SoberSwag.input_object do
18
- attribute :name, primitive(:String).meta(description: 'The full name description')
19
- attribute :age, param(:Integer).constrained(gt: 0).optional.meta(description: 'My cool age')
20
- attribute? :mood, Types::String
21
- end
22
-
23
- ##
24
- # Demonstration of subclass-style.
25
- class PersonSearch < SoberSwag::InputObject
26
- attribute? :name, Types::String
27
- attribute? :age, Types::Integer
28
- end
29
-
30
- ##
31
- # Demonstration of subclass-style *and* recursion in structs.
32
- class LinkedList < SoberSwag::InputObject
33
- attribute :value, Types::String
34
- attribute? :next, LinkedList
35
- end
36
-
37
- Foo = SoberSwag::OutputObject.define do
38
- field :name, primitive(:String)
39
- field :age, primitive(:String)
40
-
41
- view :foo do
42
- field :bar, primitive(:String).optional
43
- end
44
- end
45
-
46
- # (If you use this, don't forget to add pry to your Gemfile!)
47
- require 'pry'
48
- Pry.start