acts_as_doc 1.0.1 → 1.1.1

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: 69e3e4d92a82219b4e70f108f1d7ba407c5faa1dc90b7e123c7d69021cd7f4e7
4
+ data.tar.gz: a106cb419fba57a5dd448f0625c80ffdf90b9cc329b98bfd0a2e908a1cf1c2c8
5
5
  SHA512:
6
- metadata.gz: cbc1030672261ae66675f1ebe0d6c7dfa6780f39adc32088a4375e3b51b07539ffbc09473b11a65f66749a8b38b692734637db6f5e3c8dfc5a403bddcb41a2f8
7
- data.tar.gz: 3c615324dcbe8ef60ded241251633f40a92a86f4ce523a0eb08967f51e34b1725a86ff6074c0a5c9fecef5285a2a301ee6147f79b0dcfeb01f81d434faeab53d
6
+ metadata.gz: 98b40a4d13e824f471374dce1514f2edd6170dc792922fb4682b6d262dd9657293249e37e6ecc4dd6b24618d97b8cd16880ce869ade38999613130c0cb242ac5
7
+ data.tar.gz: 2ffc7d404eb9c0c738d09e5d6b69769bf4255cf750db09bdda7e463d51b5dc0c2f3a7b25a64b674ecdb7cc1dc9ac9212ceff34608a56313d75749642c7e80f10
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.1"
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,42 @@ 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-z,\s_A-Z]+)\)$/))
54
+ columns = Object.const_get(klass).columns.map do |column|
55
+ column_type = if column.respond_to?(:array?) && column.array?
56
+ 'array'
57
+ else
58
+ TYPE_MAPPER[column.type]
59
+ end
60
+ [ column.name.to_s, [column_type, column.comment] ]
61
+ end.to_h
62
+
63
+ matches[1].split(',').each do |attr_name|
64
+ attr_name.strip!
65
+ c_type, c_desc = columns[attr_name]
66
+ self.props_recursion!(hash, "$#{name}.#{attr_name}", c_type, c_desc)
67
+ end
68
+ else
69
+ hash[name].merge!(type: type, description: desc)
70
+ end
42
71
  else
43
72
  nest_hash = hash[name]
44
73
  nest_hash[:properties] = {} unless nest_hash.key?(:properties)
45
- self.props_recursion!(nest_hash[:properties], arr.join('.'), type, desc)
74
+ self.props_recursion!(nest_hash[:properties], arr.join('.'), type, desc, klass)
46
75
  end
47
76
 
48
77
  hash
@@ -62,16 +91,24 @@ module ActsAsDoc
62
91
 
63
92
  if tag == '@prop'
64
93
  desc = arr[3..] ? arr[3..].join(' ') : ''
65
- matches = type.match(/\[(?<type>#{SUPPORT_TYPES.join('|')})\]/)
94
+
95
+ matches = type.match(/\[(?<type>[a-zA-Z<>:]+)\]/)
66
96
  type = matches ? matches[:type] : 'string'
67
97
 
98
+ klass_matches = type.match(/(?<type>[a-zA-Z]+)<(?<klass>[a-zA-Z:]+)>/)
99
+ klass = nil
100
+ if klass_matches
101
+ type = SUPPORT_TYPES.include?(klass_matches[:type]) ? klass_matches[:type] : 'string'
102
+ klass = klass_matches[:klass]
103
+ end
104
+
68
105
  if name.start_with?(':')
69
106
  name = name.sub(/^:/, '')
70
107
  schema[name] = { type: type, description: desc }
71
108
  end
72
109
 
73
110
  if name.start_with?(REF_FLAG)
74
- self.class.props_recursion!(schema, name, type, desc)
111
+ self.class.props_recursion!(schema, name, type, desc, klass)
75
112
  end
76
113
  end
77
114
  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.1
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