typed-parameter 0.1.0 → 0.2.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 +152 -6
- data/lib/typed_parameter.rb +1 -0
- data/lib/typed_parameter/constraints.rb +7 -49
- 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/datetime_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/parameter_types.rb +18 -3
- data/lib/typed_parameter/permit_field_generator.rb +1 -1
- data/lib/typed_parameter/swagger/type_generator.rb +0 -3
- data/lib/typed_parameter/swagger/types.rb +7 -45
- 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_type.rb +13 -0
- data/lib/typed_parameter/swagger/types/datetime_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/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5a39f98f145cc8ca6739aaa4bf0a729264be875ed09ba6308f7f3064870245c
|
4
|
+
data.tar.gz: 9ada066d064730b375493ead65ba4c594eecd690f2dca4be89bbb12232bcb3fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b3165b35201ee612aca228d6ea55ac271bae117333cec8a6b5028b4c5bbc7021964618e6f94b4724b63893afc594906e9d8f135ae0feb5e99edadd61a2e4947
|
7
|
+
data.tar.gz: 815563901ab5685a3122caf11a280070d63da903cac667784e826721df94c58dabae3f3e115723819082f67ecb67923fd67a57501911060c4a710658aea81e31
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
# TypedParameter
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
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,156 @@ 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
|
+
ParentParams.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
|
+
...
|
167
|
+
|
168
|
+
Example 5. Swaggerize
|
169
|
+
------
|
170
|
+
...
|
171
|
+
|
26
172
|
|
27
173
|
## Development
|
28
174
|
|
data/lib/typed_parameter.rb
CHANGED
@@ -12,6 +12,7 @@ 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
|
+
|
15
16
|
module TypedParameter
|
16
17
|
class Error < StandardError; end
|
17
18
|
end
|
@@ -1,49 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
class IntegerConstraint < self
|
10
|
-
def self.value(value)
|
11
|
-
value.to_i
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class FloatConstraint < self
|
16
|
-
def self.value(value)
|
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
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
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/datetime_constraint'
|
7
|
+
require 'typed_parameter/constraints/boolean_constraint'
|
@@ -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,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
|
@@ -2,9 +2,6 @@ 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
|
@@ -1,45 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
class IntegerType
|
9
|
-
def self.value
|
10
|
-
{ type: :integer }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class FloatType
|
15
|
-
def self.value
|
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 }
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
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/datetime_type'
|
7
|
+
require 'typed_parameter/swagger/types/boolean_type'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed-parameter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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: 2021-03-
|
11
|
+
date: 2021-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -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,6 +58,13 @@ 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/datetime_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
|
61
69
|
- lib/typed_parameter/parameter_types.rb
|
62
70
|
- lib/typed_parameter/permit_field_generator.rb
|
@@ -64,6 +72,13 @@ files:
|
|
64
72
|
- lib/typed_parameter/swagger/property_generator.rb
|
65
73
|
- lib/typed_parameter/swagger/type_generator.rb
|
66
74
|
- lib/typed_parameter/swagger/types.rb
|
75
|
+
- lib/typed_parameter/swagger/types/base.rb
|
76
|
+
- lib/typed_parameter/swagger/types/boolean_type.rb
|
77
|
+
- lib/typed_parameter/swagger/types/date_type.rb
|
78
|
+
- lib/typed_parameter/swagger/types/datetime_type.rb
|
79
|
+
- lib/typed_parameter/swagger/types/float_type.rb
|
80
|
+
- lib/typed_parameter/swagger/types/integer_type.rb
|
81
|
+
- lib/typed_parameter/swagger/types/string_type.rb
|
67
82
|
- lib/typed_parameter/type_constraint.rb
|
68
83
|
- lib/typed_parameter/version.rb
|
69
84
|
- typed_parameter.gemspec
|