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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e0f46ef3693b82ba2c85964aba75468b69971855dbe6cff28e8b6c7c66d2f74
4
- data.tar.gz: 2a3d91c2d8d229bb89336bd891db8169494619d996f6ec2553a14ffc213733b6
3
+ metadata.gz: d5a39f98f145cc8ca6739aaa4bf0a729264be875ed09ba6308f7f3064870245c
4
+ data.tar.gz: 9ada066d064730b375493ead65ba4c594eecd690f2dca4be89bbb12232bcb3fd
5
5
  SHA512:
6
- metadata.gz: 798e72cc8eefdd41e684ab5e9df6c0513a4bdaed0559136e8c0839a51113e2c696f229d751dbae83f664d7a642e5cb6d1df7ab3fa5a9bae00ac4dbc976d11a1b
7
- data.tar.gz: a1d9565b84129234acf6cb683c10762751b62b2de77e4dc15842c71e413881e0d4c5e6d3ddaef9d4f3cd8a877de8a68cb3deb677079f54af838796d2c62aa17d
6
+ metadata.gz: 4b3165b35201ee612aca228d6ea55ac271bae117333cec8a6b5028b4c5bbc7021964618e6f94b4724b63893afc594906e9d8f135ae0feb5e99edadd61a2e4947
7
+ data.tar.gz: 815563901ab5685a3122caf11a280070d63da903cac667784e826721df94c58dabae3f3e115723819082f67ecb67923fd67a57501911060c4a710658aea81e31
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ - 2021-03-06
2
+ -
3
+ - Release TypedParameter
data/README.md CHANGED
@@ -1,9 +1,6 @@
1
1
  # TypedParameter
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/typed-parameter`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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
 
@@ -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
- # frozen_string_literal: true
2
-
3
- module TypedParameter
4
- class Constraints
5
- def self.value(_v)
6
- nil
7
- end
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,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Constraints
5
+ class Base
6
+ def self.value(value)
7
+ raise NotImplementedError, "need to implement"
8
+ end
9
+ end
10
+ end
11
+ 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
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Constraints
5
+ class DateConstraint < Base
6
+ def self.value(value)
7
+ value.to_date
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Constraints
5
+ class DateTimeConstraint < Base
6
+ def self.value(value)
7
+ value.to_datetime
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Constraints
5
+ class FloatConstraint < Base
6
+ def self.value(value)
7
+ value.to_f
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Constraints
5
+ class IntegerConstraint < Base
6
+ def self.value(value)
7
+ value.to_i
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Constraints
5
+ class StringConstraint < Base
6
+ def self.value(value)
7
+ value.to_s
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,6 +1,21 @@
1
1
  module TypedParameter
2
2
  module ParameterTypes
3
- BASE_TYPES = [Integer, Float, String, Date, DateTime, Boolean].freeze
4
- CUSTOM_TYPES = [].freeze
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,7 +1,7 @@
1
1
  module TypedParameter
2
2
  module PermitFieldGenerator
3
3
  class << self
4
- BASE_TYPES = TypedParameter::ParameterTypes::BASE_TYPES
4
+ BASE_TYPES = TypedParameter::ParameterTypes.types
5
5
 
6
6
  def generate(name, type)
7
7
  props = generate_properties(type)
@@ -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
- module TypedParameter
2
- module Swagger
3
- class Types
4
- def self.value
5
- raise NotImplementedError, 'You need to implement this method'
6
- end
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'
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Swagger
5
+ module Types
6
+ class Base
7
+ def self.value
8
+ raise NotImplementedError, 'You need to implement this method'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Swagger
5
+ module Types
6
+ class BooleanType < Base
7
+ def self.value
8
+ { type: :boolean }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Swagger
5
+ module Types
6
+ class DateType < Base
7
+ def self.value
8
+ { type: :string, format: :date }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Swagger
5
+ module Types
6
+ class DateTimeType < Base
7
+ def self.value
8
+ { type: :string, format: :datetime }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Swagger
5
+ module Types
6
+ class FloatType < Base
7
+ def self.value
8
+ { type: :number, format: :float }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Swagger
5
+ module Types
6
+ class IntegerType < Base
7
+ def self.value
8
+ { type: :integer }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TypedParameter
4
+ module Swagger
5
+ module Types
6
+ class StringType < Base
7
+ def self.value
8
+ { type: :string }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module TypedParameter
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'.freeze
3
3
  end
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.1.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-06 00:00:00.000000000 Z
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