typed-parameter 0.4.0 → 0.5.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: 85aaf512215b5dd4abc568b69190dde93d0894befac1a1f1b6c9097cd468a9f1
4
- data.tar.gz: 51d58c512d2f057b6ce7071876a8b01ad6360fd9d9d70aaa323ce56e96ecdaca
3
+ metadata.gz: ff83851eec45e830cd251fa2a886722d6968373ce94f3a84eef45b47b3c67351
4
+ data.tar.gz: 47f08d63b04dad171ff357ca3ce31433d586bd5da1adc95560395783e687a80b
5
5
  SHA512:
6
- metadata.gz: 26b48a740e5f53583003ef50f9c2a3c016cb19a8146b74e90660865fa224f8de9e01bbe150e83c379091f069021281f394d36e7ec204e40f4b586c15126c2034
7
- data.tar.gz: 144a0dab8ec0676cdb4fb3d0447769df7d8c0cc007d38830f2499fd16cba1cf78a194b64644b5e223a3e3a82188ccc30f0e7b5cdb5fa3f2d60b0f2d47bc1a426
6
+ metadata.gz: c3ceb0d67668c8f4b02a3269c450c9800b9c63821b1904d14992b50d06475bc911b0bb6b5b3a5fe7d26db97aa03e164fe877ec533058c7426eea520027e90d6f
7
+ data.tar.gz: c505148ce9101eebf71c145f000abd8e45aa6501c4832cc39e03d43b0a734890d70cdf53a13226c8653ba4bee847d566672d4f139075d003b046a944b6fa54a4
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # TypedParameter
1
+ # typed_parameter
2
2
  Create typed parameter class using strong paramter in rails.
3
3
  You can cleanup parameter code for permit in your controller and convert it to the type you want.
4
4
  ## Installation
@@ -145,7 +145,7 @@ example_params = {
145
145
  }
146
146
 
147
147
  def parent_and_childs_params
148
- ParentParams.permit(example_params)
148
+ ParentParameter.permit(example_params)
149
149
  end
150
150
 
151
151
  # Result
@@ -2,25 +2,46 @@ module TypedParameter
2
2
  class Base
3
3
  class << self
4
4
  def field(name, type, **kargs)
5
- initialize_permitted_fields(name, type) # Need to remove! we don't need strong_parameter's filter
6
- initialize_swagger_properties(name, type, kargs)
7
- initialize_constraints(name, type, kargs)
8
- end
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
- use_constraints(params).permit! # will change to use_constraints(params).permit!
23
+ initialize_constraints(name, target, kargs)
24
+ initialize_swagger_properties(name, target, kargs)
14
25
  end
15
26
 
16
- def fields
17
- __fields.freeze
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,43 +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
- end
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)
49
- paramters = ActionController::Parameters.new
67
+ typed_params = ActionController::Parameters.new
50
68
 
51
69
  __constraints.each do |name, type, options|
52
70
  value = params[name]
53
- raise RequiredFieldError, "(#{self.name}) #{name} is required" if options[:required] && !value.present?
71
+ raise Error::RequiredFieldError, "(#{self.name}) #{name} is required" if options[:required] && empty_value?(value)
72
+
54
73
  next unless value.present?
55
74
 
56
- paramters[name] = TypeConstraint.value(type, value)
57
- paramters[name] = EnumConstraint.value(value, options[:enum]) if options[:enum]
75
+ typed_params[name] = TypeConstraint.value(type, value)
76
+ typed_params[name] = EnumConstraint.value(value, options[:enum]) if options[:enum]
58
77
  end
59
78
 
60
- paramters
79
+ typed_params
61
80
  end
62
81
 
63
- def __fields
64
- @__fields ||= []
82
+ def empty_value?(value)
83
+ value.nil? || value == '' || (value.is_a?(Array) && value.empty?)
65
84
  end
66
85
 
67
86
  def __constraints
68
87
  @__constraints ||= []
69
88
  end
70
89
 
90
+ def __swagger_requirements
91
+ @__swagger_requirements ||= []
92
+ end
93
+
71
94
  def __swagger_properties
72
95
  @__swagger_properties ||= {}
73
96
  end
@@ -4,16 +4,12 @@ class Boolean; end
4
4
 
5
5
  class TrueClass
6
6
  def is_a?(type)
7
- return true if type == Boolean
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
- return true if type == Boolean
16
-
17
- super(type)
13
+ type == Boolean || super(type)
18
14
  end
19
15
  end
@@ -4,7 +4,7 @@ module TypedParameter
4
4
  module Constraints
5
5
  class DateConstraint < Base
6
6
  def self.value(value)
7
- value.to_date
7
+ value.to_time.to_date
8
8
  end
9
9
  end
10
10
  end
@@ -4,7 +4,7 @@ module TypedParameter
4
4
  module Constraints
5
5
  class DateTimeConstraint < Base
6
6
  def self.value(value)
7
- value.to_datetime
7
+ value.to_time.to_datetime
8
8
  end
9
9
  end
10
10
  end
@@ -1,10 +1,20 @@
1
1
  module TypedParameter
2
2
  module EnumConstraint
3
3
  def self.value(value, enums)
4
- enums = enums.map(&:to_s) if value.is_a? String
5
- raise EnumFieldError, "#{value} must be in #{enums.join(', ')}" unless value.in? enums
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
@@ -4,4 +4,6 @@ module TypedParameter
4
4
  class RequiredFieldError < Error; end
5
5
 
6
6
  class EnumFieldError < Error; end
7
+
8
+ class InvalidBlockParameter < Error; end
7
9
  end
@@ -6,11 +6,13 @@ module TypedParameter
6
6
  key = parameter_class.key
7
7
  properties = Swagger::PropertyGenerator.generate(parameter_class.swagger_properties)
8
8
 
9
- { "#{key}": { type: :object, properties: properties } }
9
+ swagger = { type: :object, properties: properties }
10
+ swagger[:required] = parameter_class.swagger_requirements if parameter_class.swagger_requirements.present?
11
+ { "#{key}": swagger }
10
12
  end
11
13
 
12
14
  def generate_all!
13
- parameter_classes = TypedParameter::Base.descendants.reject { |c| c.fields.empty? }
15
+ parameter_classes = TypedParameter::Base.descendants.reject { |c| c.swagger_properties.empty? }
14
16
 
15
17
  parameter_classes.reduce({}) { |prev, curr| prev.merge(generate(curr)) }
16
18
  end
@@ -5,9 +5,9 @@ module TypedParameter
5
5
  def generate(type)
6
6
  return array_generate(type) if type.is_a? Array
7
7
  return hash_generate(type) if type.is_a? Hash
8
+ return { ref: type } if type < TypedParameter::Base
8
9
 
9
- swagger_type = Swagger::Types[type.name.to_sym]
10
- swagger_type&.value || { ref: type }
10
+ Swagger::Types[type.name.to_sym]&.value
11
11
  end
12
12
 
13
13
  def hash_generate(hash)
@@ -17,7 +17,7 @@ module TypedParameter
17
17
  end
18
18
 
19
19
  def array_generate(type)
20
- { type: :array, items: generate(type[0]) }
20
+ { type: :array, items: generate(type[0]) }
21
21
  end
22
22
  end
23
23
  end
@@ -1,3 +1,3 @@
1
1
  module TypedParameter
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.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.4.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: 2021-03-08 00:00:00.000000000 Z
11
+ date: 2022-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails