acts_as_doc 1.0.1 → 1.1.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: 12a796bab9820e42c34cc94ad2029dff43521113e20a8686aed667c72a7f1c86
4
- data.tar.gz: 7924b82d0461dd99417ed2757e653de560e2a884430586436d931e34f8c85c7d
3
+ metadata.gz: 84a468753835af1c1f73d8edf468234bd164283c67b18a0f1a4281bea905fc86
4
+ data.tar.gz: 498222082962d5a7376994cdde490ea64d03440a1a1487b568a9fc9cad6c0d38
5
5
  SHA512:
6
- metadata.gz: cbc1030672261ae66675f1ebe0d6c7dfa6780f39adc32088a4375e3b51b07539ffbc09473b11a65f66749a8b38b692734637db6f5e3c8dfc5a403bddcb41a2f8
7
- data.tar.gz: 3c615324dcbe8ef60ded241251633f40a92a86f4ce523a0eb08967f51e34b1725a86ff6074c0a5c9fecef5285a2a301ee6147f79b0dcfeb01f81d434faeab53d
6
+ metadata.gz: 7965992ef4dc0b7464b1742a261a668d1793343bee96d876d3b9771328d54dc97740db4df2fafe9f2b8848c2eed7e45d931503e6c19a896a846cbfc5cb2e4e16
7
+ data.tar.gz: 951e8fc0ac3c327979e8bf2d53c32dcaef6fda75aafb3b7e4e2815695ff72261feda7234ec64f40ee99234156194f5a1973cb8cd141dcdc4879c69cec0d06821
data/acts_as_doc.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # rubocop:disable all
2
2
  Gem::Specification.new do |s|
3
3
  s.name = "acts_as_doc"
4
- s.version = "1.0.1"
4
+ s.version = "1.1.0"
5
5
  s.summary = "Generate swagger response doc"
6
6
  s.description = "Add swagger comment to any ruby file for generating swagger response doc struct"
7
7
  s.authors = ["alex.zang"]
@@ -16,6 +16,17 @@ module ActsAsDoc
16
16
 
17
17
  REF_FLAG = '$'
18
18
 
19
+ TYPE_MAPPER = {
20
+ integer: 'integer',
21
+ string: 'string',
22
+ decimal: 'number',
23
+ text: 'string',
24
+ geography: 'string',
25
+ json: 'object',
26
+ jsonb: 'object',
27
+ array: 'array'
28
+ }.freeze
29
+
19
30
  # 处理注释
20
31
  #
21
32
  # @param token [String] one line of comments
@@ -25,24 +36,36 @@ module ActsAsDoc
25
36
  end
26
37
 
27
38
  # Make props recursion
39
+ #
28
40
  # @example
29
41
  # >> ActsAsDoc::ResponseParser.props_recursion!({}, '$a.$b.c', 'c', 'd')
30
42
  # # => {'a' => }
31
43
  #
32
44
  # @return [Hash] 处理过的hash
33
45
  # rubocop:disable all
34
- def self.props_recursion!(hash, name, type, desc)
46
+ def self.props_recursion!(hash, name, type, desc = '', klass = nil)
35
47
  arr = name.split('.')
36
48
  name = arr.shift.sub(/^\$/, '')
37
49
 
38
50
  hash[name] = {} unless hash.key?(name)
39
51
 
40
52
  if arr.empty?
41
- hash[name].merge!(type: type, description: desc)
53
+ if klass && (matches = desc.match(/^\(([a-zA-Z]+)\)$/))
54
+ columns = Object.const_get(klass).columns.map do |column|
55
+ [ column.name.to_s, [TYPE_MAPPER[column.type], column.comment] ]
56
+ end.to_h
57
+
58
+ matches[1].split(',').each do |attr_name|
59
+ c_type, c_desc = columns[attr_name]
60
+ self.props_recursion!(hash, "$#{name}.#{attr_name}", c_type, c_desc)
61
+ end
62
+ else
63
+ hash[name].merge!(type: type, description: desc)
64
+ end
42
65
  else
43
66
  nest_hash = hash[name]
44
67
  nest_hash[:properties] = {} unless nest_hash.key?(:properties)
45
- self.props_recursion!(nest_hash[:properties], arr.join('.'), type, desc)
68
+ self.props_recursion!(nest_hash[:properties], arr.join('.'), type, desc, klass)
46
69
  end
47
70
 
48
71
  hash
@@ -62,16 +85,24 @@ module ActsAsDoc
62
85
 
63
86
  if tag == '@prop'
64
87
  desc = arr[3..] ? arr[3..].join(' ') : ''
65
- matches = type.match(/\[(?<type>#{SUPPORT_TYPES.join('|')})\]/)
88
+
89
+ matches = type.match(/\[(?<type>[a-zA-Z<>]+)\]/)
66
90
  type = matches ? matches[:type] : 'string'
67
91
 
92
+ klass_matches = type.match(/(?<type>[a-zA-Z]+)<(?<klass>[a-zA-Z]+)>/)
93
+ klass = nil
94
+ if klass_matches
95
+ type = SUPPORT_TYPES.include?(klass_matches[:type]) ? klass_matches[:type] : 'string'
96
+ klass = klass_matches[:klass]
97
+ end
98
+
68
99
  if name.start_with?(':')
69
100
  name = name.sub(/^:/, '')
70
101
  schema[name] = { type: type, description: desc }
71
102
  end
72
103
 
73
104
  if name.start_with?(REF_FLAG)
74
- self.class.props_recursion!(schema, name, type, desc)
105
+ self.class.props_recursion!(schema, name, type, desc, klass)
75
106
  end
76
107
  end
77
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alex.zang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-09 00:00:00.000000000 Z
11
+ date: 2022-12-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Add swagger comment to any ruby file for generating swagger response
14
14
  doc struct