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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c59e34205e7543bb5d58358d8e93b92ff911cacac58b4dbd55069b1398d10691
4
- data.tar.gz: 7ba6653bb5b7ecd3e10d3e23be396f9482b0cf336f8807d5cc09a8d9fa1b9750
3
+ metadata.gz: a707b8082dc9c58012a4b8f93c93d2c8543b3ee85d96f64d972b9a074b3bed29
4
+ data.tar.gz: 2d0a8eeea15d0418f91c4fb48e128d1f965db24773a81a9ac4f23d6c2b01500e
5
5
  SHA512:
6
- metadata.gz: 6b6b302946b9eb92cbcedf61a6eae529030154ec251ce4a98928d0cd31cffb069e43ad2fd98f4768ca208ea1f7f65e5cf0be89fa16bce1d3fd7f2c02d8d837c1
7
- data.tar.gz: ee6bdb5edaf5a37230c631036936a3dc8a520aa0ed5a344ad69cdf03ff9d77d817f2c346d0b8a27ca0a421f6c25f16a80190262ead23877af6a7a4f7a706a8ad
6
+ metadata.gz: 941a6c062c86638aa540eba6ac2fc697ba9177b7521c344bed4ae71b88747b5419c1c8b15cf44da9d5dea5098bf6a6de910125173bdfa75cb4838f353f28caec
7
+ data.tar.gz: 7a07ba572c6fe6890368b7b1b0458cf934ee07953e4bd84a60728945c5f852d2a8206fe0e6cf83b70be343dda5683927b9e29d49292cb8181461ffa567611cb9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_params (0.6.0)
4
+ easy_params (0.6.1)
5
5
  activemodel (>= 3.2)
6
6
 
7
7
  GEM
@@ -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
- type = EasyParams::Types::Each.with_type(&block)
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
- type = Class.new(EasyParams::Types::Struct.class).tap { |c| c.class_eval(&block) }.new
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::Types::Struct.class
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::Types::Struct.class).tap { |c| c.class_eval(&block) }.new
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
@@ -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 = Class.new(EasyParams::Base).new
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::Types::Struct.class
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::Types::Struct.class
27
+ when EasyParams::Base
28
28
  handle_nested_errors(value, error_key_prefix, attr_name, array_index)
29
29
  end
30
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyParams
4
- VERSION = '0.6.0'
4
+ VERSION = '0.6.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran