typed-parameter 0.1.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +228 -7
- data/lib/typed_parameter/base.rb +46 -21
- data/lib/typed_parameter/boolean.rb +2 -6
- data/lib/typed_parameter/constraints/base.rb +11 -0
- data/lib/typed_parameter/constraints/boolean_constraint.rb +15 -0
- data/lib/typed_parameter/constraints/date_constraint.rb +11 -0
- data/lib/typed_parameter/constraints/date_time_constraint.rb +11 -0
- data/lib/typed_parameter/constraints/float_constraint.rb +11 -0
- data/lib/typed_parameter/constraints/integer_constraint.rb +11 -0
- data/lib/typed_parameter/constraints/string_constraint.rb +11 -0
- data/lib/typed_parameter/constraints.rb +21 -41
- data/lib/typed_parameter/enum_constraint.rb +13 -3
- data/lib/typed_parameter/errors.rb +9 -0
- data/lib/typed_parameter/parameter_types.rb +18 -3
- data/lib/typed_parameter/permit_field_generator.rb +8 -8
- data/lib/typed_parameter/swagger/component_generator.rb +9 -1
- data/lib/typed_parameter/swagger/type_generator.rb +3 -6
- data/lib/typed_parameter/swagger/types/base.rb +13 -0
- data/lib/typed_parameter/swagger/types/boolean_type.rb +13 -0
- data/lib/typed_parameter/swagger/types/date_time_type.rb +13 -0
- data/lib/typed_parameter/swagger/types/date_type.rb +13 -0
- data/lib/typed_parameter/swagger/types/float_type.rb +13 -0
- data/lib/typed_parameter/swagger/types/integer_type.rb +13 -0
- data/lib/typed_parameter/swagger/types/string_type.rb +13 -0
- data/lib/typed_parameter/swagger/types.rb +22 -36
- data/lib/typed_parameter/type_constraint.rb +6 -6
- data/lib/typed_parameter/version.rb +1 -1
- data/lib/typed_parameter.rb +4 -4
- data/typed_parameter.gemspec +1 -1
- metadata +22 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff83851eec45e830cd251fa2a886722d6968373ce94f3a84eef45b47b3c67351
|
4
|
+
data.tar.gz: 47f08d63b04dad171ff357ca3ce31433d586bd5da1adc95560395783e687a80b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3ceb0d67668c8f4b02a3269c450c9800b9c63821b1904d14992b50d06475bc911b0bb6b5b3a5fe7d26db97aa03e164fe877ec533058c7426eea520027e90d6f
|
7
|
+
data.tar.gz: c505148ce9101eebf71c145f000abd8e45aa6501c4832cc39e03d43b0a734890d70cdf53a13226c8653ba4bee847d566672d4f139075d003b046a944b6fa54a4
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
1
|
+
# typed_parameter
|
2
|
+
Create typed parameter class using strong paramter in rails.
|
3
|
+
You can cleanup parameter code for permit in your controller and convert it to the type you want.
|
7
4
|
## Installation
|
8
5
|
|
9
6
|
Add this line to your application's Gemfile:
|
@@ -22,7 +19,231 @@ Or install it yourself as:
|
|
22
19
|
|
23
20
|
## Usage
|
24
21
|
|
25
|
-
|
22
|
+
Example 1. Type convert
|
23
|
+
-----
|
24
|
+
~~~ruby
|
25
|
+
class UserCreateParams < TypedParamter::Base
|
26
|
+
field :name, String
|
27
|
+
field :age, Integer
|
28
|
+
field :email, String
|
29
|
+
end
|
30
|
+
-----------------------------------------------
|
31
|
+
############
|
32
|
+
## before ##
|
33
|
+
############
|
34
|
+
# method for params
|
35
|
+
def user_create_params
|
36
|
+
params.permit(:name, :age, :email)
|
37
|
+
end
|
38
|
+
|
39
|
+
# in action
|
40
|
+
def create
|
41
|
+
raise UserAgeTooYoung if user_create_params[:age].to_i < 20
|
42
|
+
# You need to convert your parameter type
|
43
|
+
end
|
44
|
+
|
45
|
+
############
|
46
|
+
## after ###
|
47
|
+
############
|
48
|
+
# method for params
|
49
|
+
def user_create_params
|
50
|
+
UserCreateParams.permit(params)
|
51
|
+
end
|
52
|
+
|
53
|
+
# in action
|
54
|
+
def create
|
55
|
+
raise UserAgeTooYoung if user_create_params[:age] < 20
|
56
|
+
# paramters are converted as your type
|
57
|
+
end
|
58
|
+
~~~
|
59
|
+
|
60
|
+
|
61
|
+
Example 2. More Types
|
62
|
+
------
|
63
|
+
~~~ruby
|
64
|
+
class CustomParameter < TypedParameter::Base
|
65
|
+
field :string_field, String
|
66
|
+
field :integer_field, Integer
|
67
|
+
field :float_field, Float
|
68
|
+
field :date_field, Date
|
69
|
+
field :datetime_field, DateTime
|
70
|
+
field :boolean_field, Boolean
|
71
|
+
end
|
72
|
+
|
73
|
+
############
|
74
|
+
## before ##
|
75
|
+
############
|
76
|
+
def custom_params
|
77
|
+
params.permit(
|
78
|
+
:string_field, # => is_a? String
|
79
|
+
:integer_field, # => is_a? String. You need to &:to_i
|
80
|
+
:fload_field, # => is_a? String. You need to &:to_f
|
81
|
+
:date_field, # => is_a? String. You need to &:to_date
|
82
|
+
:datetime_field # => is_a? String. You need to &:to_datetime
|
83
|
+
:boolean_field # => is_a? Boolean. If paramter is string (like "false") then String
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
############
|
88
|
+
## after ##
|
89
|
+
############
|
90
|
+
def custom_params
|
91
|
+
CustomParamter.permit(params)
|
92
|
+
end
|
93
|
+
|
94
|
+
custom_params[:string_field] # => is_a? String
|
95
|
+
custom_params[:integer_field] # => is_a? Integer
|
96
|
+
custom_params[:float_field] # => is_a? Float
|
97
|
+
custom_params[:date_field] # => is_a? Date
|
98
|
+
custom_params[:datetime_field] # => is_a? DateTime
|
99
|
+
custom_params[:boolean] # => is_a? Boolean, If parameter is string (like "false", "true" "False", "True") then convert true or false
|
100
|
+
~~~
|
101
|
+
|
102
|
+
Example 3. Enum & Required
|
103
|
+
---
|
104
|
+
~~~ruby
|
105
|
+
class EnumAndRequiredParameter < TypedParameter::base
|
106
|
+
field :enum_field, Integer, enum: [10, 20, 30, 40]
|
107
|
+
field :required_field, String, required: true
|
108
|
+
end
|
109
|
+
|
110
|
+
def enum_and_required_params
|
111
|
+
EnumAndRequiredParameter.permit(params)
|
112
|
+
end
|
113
|
+
|
114
|
+
# If params[:enum_field] has 25 ( not in 10, 20, 30, 40)
|
115
|
+
# => ArgumentError, "enum_field must be in 10, 20, 30, 40"
|
116
|
+
|
117
|
+
# if params[:required_field] is Nil
|
118
|
+
# => ArgumentError, "required_field is required"
|
119
|
+
~~~
|
120
|
+
|
121
|
+
Example 4. array & nested parameters
|
122
|
+
---
|
123
|
+
~~~ruby
|
124
|
+
class ParentParameter < TypedParameter::Base
|
125
|
+
field :name, String
|
126
|
+
field :integers, [Integer]
|
127
|
+
field :child, ChildParameter
|
128
|
+
field :childs, [ChildParamter]
|
129
|
+
end
|
130
|
+
|
131
|
+
class ChildParameter < TypedParameter::Base
|
132
|
+
field :name, String
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
example_params = {
|
137
|
+
name: "Name",
|
138
|
+
integers: ['1','2','3','4'],
|
139
|
+
child: { name: "child" },
|
140
|
+
childs: [
|
141
|
+
{ name: "child1", age: "10" },
|
142
|
+
{ name: "child2", age: "12" },
|
143
|
+
{ name: "child3", age: "15" }
|
144
|
+
]
|
145
|
+
}
|
146
|
+
|
147
|
+
def parent_and_childs_params
|
148
|
+
ParentParameter.permit(example_params)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Result
|
152
|
+
{
|
153
|
+
name: "Name",
|
154
|
+
integers: [1,2,3,4],
|
155
|
+
child: { name: "child" },
|
156
|
+
childs: [
|
157
|
+
{ name: "child1" },
|
158
|
+
{ name: "child2" },
|
159
|
+
{ name: "child3" }
|
160
|
+
]
|
161
|
+
}
|
162
|
+
|
163
|
+
~~~
|
164
|
+
Example 4. CustomType
|
165
|
+
------
|
166
|
+
If You want to use your custom type like Email, just create two file.
|
167
|
+
- your type file. like "Email"
|
168
|
+
- yout type constant. like "EmailConstrant"
|
169
|
+
|
170
|
+
~~~ruby
|
171
|
+
class Email
|
172
|
+
def initialize(value)
|
173
|
+
@email = value
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
class EmailConstrant
|
178
|
+
class << self
|
179
|
+
EMAIL_REGREX = //
|
180
|
+
|
181
|
+
# using convert value to your type
|
182
|
+
def value(value)
|
183
|
+
raise ArgumentError unless EMAIL_REGREX.match? value
|
184
|
+
|
185
|
+
Email.new(value)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
class EmailSwaggerType
|
191
|
+
class << self
|
192
|
+
def value
|
193
|
+
{ type: :string }
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Register your type to typed-parameter when application initialize.
|
199
|
+
TypedParameter::ParameterTypes.register Email
|
200
|
+
TypedParameter::Constants.register :Email, EmailConstrant
|
201
|
+
TypedParameter::Swagger::Types.register :Email, EmailSwaggerType
|
202
|
+
~~~
|
203
|
+
|
204
|
+
Example 5. Swaggerize
|
205
|
+
------
|
206
|
+
With Rswag(https://github.com/rswag/rswag), you can swaggerize your parameter types.
|
207
|
+
|
208
|
+
~~~ruby
|
209
|
+
class SwaggerParameter < TypedParameter::Base
|
210
|
+
field :string, String, enum: ["one", "two"], description: "String Field"
|
211
|
+
field :integer, Integer, description: "Integer Field"
|
212
|
+
field :ref_field, SwaggerRefParameter
|
213
|
+
end
|
214
|
+
|
215
|
+
class SwaggerRefParameter < TypedParameter::Base
|
216
|
+
field :string, String, description: "Ref String Field"
|
217
|
+
end
|
218
|
+
|
219
|
+
SwaggerParameter.swagger_properties
|
220
|
+
# =>
|
221
|
+
# {
|
222
|
+
# string: { type: :string, enum: ["one", "two" ], description: "String Field" },
|
223
|
+
# integer: { type: :integer, description: "Integer Field" },
|
224
|
+
# ref_field: { "$ref": "#/components/schemas/SwaggerRefParamter" }
|
225
|
+
# }
|
226
|
+
#
|
227
|
+
|
228
|
+
# Add Components in your swagger
|
229
|
+
# in swagger_helper
|
230
|
+
components = TypedParameter::Swagger::ComponentGenerator.generate_all!
|
231
|
+
|
232
|
+
config.swagger_docs = {
|
233
|
+
'v1/swagger.yaml' => {
|
234
|
+
openapi: '3.0.1',
|
235
|
+
# ...,
|
236
|
+
components: {
|
237
|
+
schemas: {
|
238
|
+
**components
|
239
|
+
}
|
240
|
+
}
|
241
|
+
#...
|
242
|
+
}
|
243
|
+
}
|
244
|
+
~~~
|
245
|
+
...
|
246
|
+
|
26
247
|
|
27
248
|
## Development
|
28
249
|
|
data/lib/typed_parameter/base.rb
CHANGED
@@ -2,25 +2,46 @@ module TypedParameter
|
|
2
2
|
class Base
|
3
3
|
class << self
|
4
4
|
def field(name, type, **kargs)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
if block_given?
|
6
|
+
raise Error::InvalidBlockParameter unless [type].flatten[0].in?([Hash, Object])
|
7
|
+
|
8
|
+
block_param_name = "#{self.to_s}_Partial_#{name.to_s.camelize}"
|
9
|
+
class_eval("class ::#{block_param_name} < ::TypedParameter::Base; end")
|
10
|
+
block_param = block_param_name.constantize
|
11
|
+
yield(block_param)
|
12
|
+
|
13
|
+
if type.is_a?(Array)
|
14
|
+
target = [block_param]
|
15
|
+
else
|
16
|
+
target = block_param
|
17
|
+
end
|
18
|
+
else
|
19
|
+
target = type
|
20
|
+
end
|
9
21
|
|
10
|
-
def permit(params)
|
11
|
-
raise ArgumentError unless params.class <= ActionController::Parameters
|
12
22
|
|
13
|
-
|
23
|
+
initialize_constraints(name, target, kargs)
|
24
|
+
initialize_swagger_properties(name, target, kargs)
|
14
25
|
end
|
15
26
|
|
16
|
-
def
|
17
|
-
|
27
|
+
def permit(params)
|
28
|
+
raise Error::ParameterError unless params.class <= ActionController::Parameters
|
29
|
+
|
30
|
+
use_constraints(params).permit!
|
18
31
|
end
|
19
32
|
|
20
33
|
def swagger_properties
|
21
34
|
__swagger_properties.freeze
|
22
35
|
end
|
23
36
|
|
37
|
+
def swagger_requirements
|
38
|
+
__swagger_requirements.freeze
|
39
|
+
end
|
40
|
+
|
41
|
+
def constraints
|
42
|
+
__constraints.freeze
|
43
|
+
end
|
44
|
+
|
24
45
|
alias key name
|
25
46
|
|
26
47
|
def ref
|
@@ -31,41 +52,45 @@ module TypedParameter
|
|
31
52
|
|
32
53
|
def initialize_swagger_properties(name, type, kargs)
|
33
54
|
swagger_type = TypedParameter::Swagger::TypeGenerator.generate(type)
|
34
|
-
swagger_options = swagger_type.merge(kargs)
|
55
|
+
swagger_options = swagger_type.merge(kargs.slice(:enum, :description))
|
35
56
|
__swagger_properties[name] = swagger_options
|
36
|
-
|
37
|
-
|
38
|
-
def initialize_permitted_fields(name, type)
|
39
|
-
__fields << PermitFieldGenerator.generate(name, type)
|
57
|
+
__swagger_requirements << name if kargs[:required]
|
40
58
|
end
|
41
59
|
|
42
60
|
def initialize_constraints(name, type, kargs)
|
43
61
|
options = kargs.slice(:enum, :required)
|
44
|
-
|
62
|
+
options[:enum] = options[:enum].map(&:to_s) if options[:enum]
|
45
63
|
__constraints << [name, type, options]
|
46
64
|
end
|
47
65
|
|
48
66
|
def use_constraints(params)
|
67
|
+
typed_params = ActionController::Parameters.new
|
68
|
+
|
49
69
|
__constraints.each do |name, type, options|
|
50
70
|
value = params[name]
|
51
|
-
raise
|
71
|
+
raise Error::RequiredFieldError, "(#{self.name}) #{name} is required" if options[:required] && empty_value?(value)
|
72
|
+
|
52
73
|
next unless value.present?
|
53
74
|
|
54
|
-
|
55
|
-
|
75
|
+
typed_params[name] = TypeConstraint.value(type, value)
|
76
|
+
typed_params[name] = EnumConstraint.value(value, options[:enum]) if options[:enum]
|
56
77
|
end
|
57
78
|
|
58
|
-
|
79
|
+
typed_params
|
59
80
|
end
|
60
81
|
|
61
|
-
def
|
62
|
-
|
82
|
+
def empty_value?(value)
|
83
|
+
value.nil? || value == '' || (value.is_a?(Array) && value.empty?)
|
63
84
|
end
|
64
85
|
|
65
86
|
def __constraints
|
66
87
|
@__constraints ||= []
|
67
88
|
end
|
68
89
|
|
90
|
+
def __swagger_requirements
|
91
|
+
@__swagger_requirements ||= []
|
92
|
+
end
|
93
|
+
|
69
94
|
def __swagger_properties
|
70
95
|
@__swagger_properties ||= {}
|
71
96
|
end
|
@@ -4,16 +4,12 @@ class Boolean; end
|
|
4
4
|
|
5
5
|
class TrueClass
|
6
6
|
def is_a?(type)
|
7
|
-
|
8
|
-
|
9
|
-
super(type)
|
7
|
+
type == Boolean || super(type)
|
10
8
|
end
|
11
9
|
end
|
12
10
|
|
13
11
|
class FalseClass
|
14
12
|
def is_a?(type)
|
15
|
-
|
16
|
-
|
17
|
-
super(type)
|
13
|
+
type == Boolean || super(type)
|
18
14
|
end
|
19
15
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TypedParameter
|
4
|
+
module Constraints
|
5
|
+
class BooleanConstraint < Base
|
6
|
+
TRUE_VALUES = [true, 'true', '1', 1].freeze
|
7
|
+
FALSE_VALUES = [false, 'false', '0', 0].freeze
|
8
|
+
|
9
|
+
def self.value(value)
|
10
|
+
return true if TRUE_VALUES.include?(value)
|
11
|
+
return false if FALSE_VALUES.include?(value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,49 +1,29 @@
|
|
1
|
-
|
1
|
+
require 'typed_parameter/constraints/base'
|
2
|
+
require 'typed_parameter/constraints/integer_constraint'
|
3
|
+
require 'typed_parameter/constraints/float_constraint'
|
4
|
+
require 'typed_parameter/constraints/string_constraint'
|
5
|
+
require 'typed_parameter/constraints/date_constraint'
|
6
|
+
require 'typed_parameter/constraints/date_time_constraint'
|
7
|
+
require 'typed_parameter/constraints/boolean_constraint'
|
2
8
|
|
3
9
|
module TypedParameter
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
class IntegerConstraint < self
|
10
|
-
def self.value(value)
|
11
|
-
value.to_i
|
10
|
+
module Constraints
|
11
|
+
class << self
|
12
|
+
def register(key, constraint)
|
13
|
+
@constraints ||= {}
|
14
|
+
@constraints[key] = constraint
|
12
15
|
end
|
13
|
-
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
value.to_f
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class StringConstraint < self
|
22
|
-
def self.value(value)
|
23
|
-
value.to_s
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class BooleanConstraint < self
|
28
|
-
TRUE_VALUES = [true, 'true', '1', 1].freeze
|
29
|
-
FALSE_VALUES = [false, 'false', '0', 0].freeze
|
30
|
-
|
31
|
-
def self.value(value)
|
32
|
-
return true if TRUE_VALUES.include?(value)
|
33
|
-
return false if FALSE_VALUES.include?(value)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
class DateConstraint < self
|
38
|
-
def self.value(value)
|
39
|
-
value.to_date
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
class DateTimeConstraint < self
|
44
|
-
def self.value(value)
|
45
|
-
value.to_datetime
|
17
|
+
def [](name)
|
18
|
+
@constraints[name].freeze
|
46
19
|
end
|
47
20
|
end
|
48
21
|
end
|
49
22
|
end
|
23
|
+
|
24
|
+
TypedParameter::Constraints.register :Integer, TypedParameter::Constraints::IntegerConstraint
|
25
|
+
TypedParameter::Constraints.register :String, TypedParameter::Constraints::StringConstraint
|
26
|
+
TypedParameter::Constraints.register :Float, TypedParameter::Constraints::FloatConstraint
|
27
|
+
TypedParameter::Constraints.register :Date, TypedParameter::Constraints::DateConstraint
|
28
|
+
TypedParameter::Constraints.register :DateTime, TypedParameter::Constraints::DateTimeConstraint
|
29
|
+
TypedParameter::Constraints.register :Boolean, TypedParameter::Constraints::BooleanConstraint
|
@@ -1,10 +1,20 @@
|
|
1
1
|
module TypedParameter
|
2
2
|
module EnumConstraint
|
3
3
|
def self.value(value, enums)
|
4
|
-
|
5
|
-
|
4
|
+
return array_value(value, enums) if value.is_a? Array
|
5
|
+
return value if value.to_s.in? enums
|
6
6
|
|
7
|
-
value
|
7
|
+
raise EnumFieldError, "#{value} must be in #{enums.join(', ')}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.array_value(values, enums)
|
11
|
+
values.each do |value|
|
12
|
+
next if value.to_s.in? enums
|
13
|
+
|
14
|
+
raise EnumFieldError, "#{value} must be in #{enums.join(', ')}"
|
15
|
+
end
|
16
|
+
|
17
|
+
values
|
8
18
|
end
|
9
19
|
end
|
10
20
|
end
|
@@ -1,6 +1,21 @@
|
|
1
1
|
module TypedParameter
|
2
2
|
module ParameterTypes
|
3
|
-
|
4
|
-
|
3
|
+
class << self
|
4
|
+
def register(type)
|
5
|
+
@types ||= []
|
6
|
+
@types << type
|
7
|
+
end
|
8
|
+
|
9
|
+
def types
|
10
|
+
@types.freeze
|
11
|
+
end
|
12
|
+
end
|
5
13
|
end
|
6
|
-
end
|
14
|
+
end
|
15
|
+
|
16
|
+
TypedParameter::ParameterTypes.register Integer
|
17
|
+
TypedParameter::ParameterTypes.register String
|
18
|
+
TypedParameter::ParameterTypes.register Float
|
19
|
+
TypedParameter::ParameterTypes.register Date
|
20
|
+
TypedParameter::ParameterTypes.register DateTime
|
21
|
+
TypedParameter::ParameterTypes.register Boolean
|
@@ -1,20 +1,20 @@
|
|
1
1
|
module TypedParameter
|
2
2
|
module PermitFieldGenerator
|
3
3
|
class << self
|
4
|
-
BASE_TYPES = TypedParameter::ParameterTypes::BASE_TYPES
|
5
|
-
|
6
4
|
def generate(name, type)
|
7
|
-
|
5
|
+
type_class = [type].flatten.first
|
8
6
|
|
9
|
-
props =
|
10
|
-
|
7
|
+
props = if type.is_a? Array
|
8
|
+
type_class.in?(TypedParameter::ParameterTypes.types) ? [] : generate_properties(type_class)
|
9
|
+
else
|
10
|
+
generate_properties(type_class)
|
11
|
+
end
|
11
12
|
|
12
|
-
[[name, props]].to_h
|
13
|
+
props ? [[name, props]].to_h : name
|
13
14
|
end
|
14
15
|
|
15
16
|
def generate_properties(type)
|
16
|
-
return if type.in?
|
17
|
-
return generate_properties(type[0]) if type.is_a? Array
|
17
|
+
return if type.in? TypedParameter::ParameterTypes.types
|
18
18
|
return type.keys if type.is_a? Hash
|
19
19
|
|
20
20
|
type.fields
|
@@ -6,7 +6,15 @@ module TypedParameter
|
|
6
6
|
key = parameter_class.key
|
7
7
|
properties = Swagger::PropertyGenerator.generate(parameter_class.swagger_properties)
|
8
8
|
|
9
|
-
|
9
|
+
swagger = { type: :object, properties: properties }
|
10
|
+
swagger[:required] = parameter_class.swagger_requirements if parameter_class.swagger_requirements.present?
|
11
|
+
{ "#{key}": swagger }
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate_all!
|
15
|
+
parameter_classes = TypedParameter::Base.descendants.reject { |c| c.swagger_properties.empty? }
|
16
|
+
|
17
|
+
parameter_classes.reduce({}) { |prev, curr| prev.merge(generate(curr)) }
|
10
18
|
end
|
11
19
|
end
|
12
20
|
end
|
@@ -2,15 +2,12 @@ module TypedParameter
|
|
2
2
|
module Swagger
|
3
3
|
module TypeGenerator
|
4
4
|
class << self
|
5
|
-
BASE_TYPES = TypedParameter::ParameterTypes::BASE_TYPES
|
6
|
-
CUSTOM_TYPES = TypedParameter::ParameterTypes::CUSTOM_TYPES
|
7
|
-
|
8
5
|
def generate(type)
|
9
6
|
return array_generate(type) if type.is_a? Array
|
10
7
|
return hash_generate(type) if type.is_a? Hash
|
8
|
+
return { ref: type } if type < TypedParameter::Base
|
11
9
|
|
12
|
-
|
13
|
-
swagger_type&.value || { ref: type }
|
10
|
+
Swagger::Types[type.name.to_sym]&.value
|
14
11
|
end
|
15
12
|
|
16
13
|
def hash_generate(hash)
|
@@ -20,7 +17,7 @@ module TypedParameter
|
|
20
17
|
end
|
21
18
|
|
22
19
|
def array_generate(type)
|
23
|
-
{ type: :array, items: generate(type[0]) }
|
20
|
+
{ type: :array, items: generate(type[0]) }
|
24
21
|
end
|
25
22
|
end
|
26
23
|
end
|
@@ -1,45 +1,31 @@
|
|
1
|
+
require 'typed_parameter/swagger/types/base'
|
2
|
+
require 'typed_parameter/swagger/types/integer_type'
|
3
|
+
require 'typed_parameter/swagger/types/float_type'
|
4
|
+
require 'typed_parameter/swagger/types/string_type'
|
5
|
+
require 'typed_parameter/swagger/types/date_type'
|
6
|
+
require 'typed_parameter/swagger/types/date_time_type'
|
7
|
+
require 'typed_parameter/swagger/types/boolean_type'
|
8
|
+
|
1
9
|
module TypedParameter
|
2
10
|
module Swagger
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
class IntegerType
|
9
|
-
def self.value
|
10
|
-
{ type: :integer }
|
11
|
+
module Types
|
12
|
+
class << self
|
13
|
+
def register(key, type)
|
14
|
+
@swagger_types ||= {}
|
15
|
+
@swagger_types[key] = type
|
11
16
|
end
|
12
|
-
end
|
13
17
|
|
14
|
-
|
15
|
-
|
16
|
-
{ type: :number, format: :float }
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class StringType
|
21
|
-
def self.value
|
22
|
-
{ type: :string }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class BooleanType
|
27
|
-
def self.value
|
28
|
-
{ type: :boolean }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class DateTimeType
|
33
|
-
def self.value
|
34
|
-
{ type: :string, format: :datetime }
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class DateType
|
39
|
-
def self.value
|
40
|
-
{ type: :string, format: :date }
|
18
|
+
def [](key)
|
19
|
+
@swagger_types[key].freeze
|
41
20
|
end
|
42
21
|
end
|
43
22
|
end
|
44
23
|
end
|
45
24
|
end
|
25
|
+
|
26
|
+
TypedParameter::Swagger::Types.register :Integer, TypedParameter::Swagger::Types::IntegerType
|
27
|
+
TypedParameter::Swagger::Types.register :String, TypedParameter::Swagger::Types::StringType
|
28
|
+
TypedParameter::Swagger::Types.register :Float, TypedParameter::Swagger::Types::FloatType
|
29
|
+
TypedParameter::Swagger::Types.register :Date, TypedParameter::Swagger::Types::DateType
|
30
|
+
TypedParameter::Swagger::Types.register :DateTime, TypedParameter::Swagger::Types::DateTimeType
|
31
|
+
TypedParameter::Swagger::Types.register :Boolean, TypedParameter::Swagger::Types::BooleanType
|
@@ -3,19 +3,19 @@ module TypedParameter
|
|
3
3
|
class << self
|
4
4
|
def value(type, value)
|
5
5
|
type_class = [type].flatten.first
|
6
|
-
array_type = type.is_a?(Array)
|
7
|
-
parameter_type = type_class < TypedParameter::Base
|
8
6
|
|
9
|
-
if
|
10
|
-
|
7
|
+
if type_class < TypedParameter::Base
|
8
|
+
type.is_a?(Array) ? parameter_array(type_class, value) : type.permit(value)
|
11
9
|
else
|
12
|
-
constraint =
|
10
|
+
constraint = TypedParameter::Constraints[type_class.name.to_sym]
|
13
11
|
constraint ||= TypedParameter::Constraints::StringConstraint # Default
|
14
12
|
|
15
|
-
|
13
|
+
type.is_a?(Array) ? constraint_array(constraint, value) : constraint.value(value)
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
17
|
+
private
|
18
|
+
|
19
19
|
def parameter_array(parameter_class, values)
|
20
20
|
values.map { |value| parameter_class.permit(value) }
|
21
21
|
end
|
data/lib/typed_parameter.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'typed_parameter/boolean' # For Booelan Class Define
|
1
|
+
require 'typed_parameter/boolean.rb' # For Booelan Class Define
|
2
2
|
|
3
3
|
require 'typed_parameter/version'
|
4
4
|
require 'typed_parameter/parameter_types'
|
@@ -12,6 +12,6 @@ require 'typed_parameter/swagger/types'
|
|
12
12
|
require 'typed_parameter/swagger/type_generator'
|
13
13
|
require 'typed_parameter/swagger/property_generator'
|
14
14
|
require 'typed_parameter/swagger/component_generator'
|
15
|
-
|
16
|
-
|
17
|
-
end
|
15
|
+
|
16
|
+
require 'typed_parameter/errors'
|
17
|
+
module TypedParameter; end
|
data/typed_parameter.gemspec
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed-parameter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Minwoo Jo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- ".gitignore"
|
49
49
|
- ".rubocop.yml"
|
50
50
|
- ".ruby-version"
|
51
|
+
- CHANGELOG.md
|
51
52
|
- Gemfile
|
52
53
|
- README.md
|
53
54
|
- Rakefile
|
@@ -57,13 +58,28 @@ files:
|
|
57
58
|
- lib/typed_parameter/base.rb
|
58
59
|
- lib/typed_parameter/boolean.rb
|
59
60
|
- lib/typed_parameter/constraints.rb
|
61
|
+
- lib/typed_parameter/constraints/base.rb
|
62
|
+
- lib/typed_parameter/constraints/boolean_constraint.rb
|
63
|
+
- lib/typed_parameter/constraints/date_constraint.rb
|
64
|
+
- lib/typed_parameter/constraints/date_time_constraint.rb
|
65
|
+
- lib/typed_parameter/constraints/float_constraint.rb
|
66
|
+
- lib/typed_parameter/constraints/integer_constraint.rb
|
67
|
+
- lib/typed_parameter/constraints/string_constraint.rb
|
60
68
|
- lib/typed_parameter/enum_constraint.rb
|
69
|
+
- lib/typed_parameter/errors.rb
|
61
70
|
- lib/typed_parameter/parameter_types.rb
|
62
71
|
- lib/typed_parameter/permit_field_generator.rb
|
63
72
|
- lib/typed_parameter/swagger/component_generator.rb
|
64
73
|
- lib/typed_parameter/swagger/property_generator.rb
|
65
74
|
- lib/typed_parameter/swagger/type_generator.rb
|
66
75
|
- lib/typed_parameter/swagger/types.rb
|
76
|
+
- lib/typed_parameter/swagger/types/base.rb
|
77
|
+
- lib/typed_parameter/swagger/types/boolean_type.rb
|
78
|
+
- lib/typed_parameter/swagger/types/date_time_type.rb
|
79
|
+
- lib/typed_parameter/swagger/types/date_type.rb
|
80
|
+
- lib/typed_parameter/swagger/types/float_type.rb
|
81
|
+
- lib/typed_parameter/swagger/types/integer_type.rb
|
82
|
+
- lib/typed_parameter/swagger/types/string_type.rb
|
67
83
|
- lib/typed_parameter/type_constraint.rb
|
68
84
|
- lib/typed_parameter/version.rb
|
69
85
|
- typed_parameter.gemspec
|