easy_params 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/easy_params/base.rb +13 -5
- data/lib/easy_params/types/collection.rb +2 -6
- data/lib/easy_params/types.rb +1 -1
- data/lib/easy_params/validation.rb +2 -2
- data/lib/easy_params/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a707b8082dc9c58012a4b8f93c93d2c8543b3ee85d96f64d972b9a074b3bed29
|
4
|
+
data.tar.gz: 2d0a8eeea15d0418f91c4fb48e128d1f965db24773a81a9ac4f23d6c2b01500e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 941a6c062c86638aa540eba6ac2fc697ba9177b7521c344bed4ae71b88747b5419c1c8b15cf44da9d5dea5098bf6a6de910125173bdfa75cb4838f353f28caec
|
7
|
+
data.tar.gz: 7a07ba572c6fe6890368b7b1b0458cf934ee07953e4bd84a60728945c5f852d2a8206fe0e6cf83b70be343dda5683927b9e29d49292cb8181461ffa567611cb9
|
data/Gemfile.lock
CHANGED
data/lib/easy_params/base.rb
CHANGED
@@ -30,16 +30,24 @@ module EasyParams
|
|
30
30
|
@schema ||= {}
|
31
31
|
end
|
32
32
|
|
33
|
-
def each(param_name, default: nil, normalize: nil, **validations, &block)
|
33
|
+
def each(param_name, definition = nil, default: nil, normalize: nil, **validations, &block)
|
34
34
|
validates param_name, **validations if validations.any?
|
35
|
-
|
35
|
+
if definition && !(definition < EasyParams::Base)
|
36
|
+
raise ArgumentError, "definition for attribute #{param_name.inspect} must be a subclass of EasyParams::Base"
|
37
|
+
end
|
38
|
+
|
39
|
+
type = EasyParams::Types::Each.with_type(definition, &block)
|
36
40
|
type = customize_type(type, default, &normalize)
|
37
41
|
attribute(param_name, type)
|
38
42
|
end
|
39
43
|
|
40
|
-
def has(param_name, default: nil, normalize: nil, **validations, &block)
|
44
|
+
def has(param_name, definition = nil, default: nil, normalize: nil, **validations, &block)
|
41
45
|
validates param_name, **validations if validations.any?
|
42
|
-
|
46
|
+
if definition && !(definition < EasyParams::Base)
|
47
|
+
raise ArgumentError, "definition for attribute #{param_name.inspect} must be a subclass of EasyParams::Base"
|
48
|
+
end
|
49
|
+
|
50
|
+
type = (definition || Class.new(EasyParams::Base).tap { |c| c.class_eval(&block) }).new
|
43
51
|
type = customize_type(type, default, &normalize)
|
44
52
|
attribute(param_name, type)
|
45
53
|
end
|
@@ -83,7 +91,7 @@ module EasyParams
|
|
83
91
|
result[key] = case value
|
84
92
|
when EasyParams::Types::StructsCollection
|
85
93
|
value.map(&:to_h)
|
86
|
-
when EasyParams::
|
94
|
+
when EasyParams::Base
|
87
95
|
value.to_h
|
88
96
|
else
|
89
97
|
value
|
@@ -26,10 +26,6 @@ module EasyParams
|
|
26
26
|
self.class.new(@title, @default, block, of: @of_type, &@coerce_proc)
|
27
27
|
end
|
28
28
|
|
29
|
-
def self.optional
|
30
|
-
self.class.new(@title, @default, @normalize_proc, of: @of_type, &@coerce_proc)
|
31
|
-
end
|
32
|
-
|
33
29
|
def default(value)
|
34
30
|
self.class.new(@title, value, @normalize_proc, of: @of_type, &@coerce_proc)
|
35
31
|
end
|
@@ -41,8 +37,8 @@ module EasyParams
|
|
41
37
|
|
42
38
|
# base interface for array of structs type
|
43
39
|
class StructsCollection < Collection
|
44
|
-
def with_type(&block)
|
45
|
-
of_type = Class.new(EasyParams::
|
40
|
+
def with_type(definition = nil, &block)
|
41
|
+
of_type = (definition || Class.new(EasyParams::Base).tap { |c| c.class_eval(&block) }).new
|
46
42
|
self.class.new(@title, @default, @normalize_proc, of: of_type)
|
47
43
|
end
|
48
44
|
end
|
data/lib/easy_params/types.rb
CHANGED
@@ -9,7 +9,7 @@ module EasyParams
|
|
9
9
|
{ '0' => false, 'f' => false, 'false' => false, 'False' => false, 'FALSE' => false, 'F' => false }
|
10
10
|
).freeze
|
11
11
|
|
12
|
-
Struct =
|
12
|
+
Struct = EasyParams::Base.new
|
13
13
|
Array = Collection.new(:array)
|
14
14
|
Each = StructsCollection.new(:array_of_structs)
|
15
15
|
Integer = Generic.new(:integer, &:to_i)
|
@@ -10,7 +10,7 @@ module EasyParams
|
|
10
10
|
case value
|
11
11
|
when EasyParams::Types::StructsCollection
|
12
12
|
value.each(&:valid?)
|
13
|
-
when EasyParams::
|
13
|
+
when EasyParams::Base
|
14
14
|
value.valid?
|
15
15
|
end
|
16
16
|
end
|
@@ -24,7 +24,7 @@ module EasyParams
|
|
24
24
|
value.each.with_index do |element, i|
|
25
25
|
aggregate_nested_errors[attr_name, element, "[#{i}]", error_key_prefix]
|
26
26
|
end
|
27
|
-
when EasyParams::
|
27
|
+
when EasyParams::Base
|
28
28
|
handle_nested_errors(value, error_key_prefix, attr_name, array_index)
|
29
29
|
end
|
30
30
|
end
|
data/lib/easy_params/version.rb
CHANGED