easy_params 0.2.1 → 0.3.1

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: 969e00add49155dc01e28f0fec6af1db78c7af085f3b77cf8870f3b372be2a08
4
- data.tar.gz: 7823ed67872e7e04d570c7edb79071ea3e93bc295768f2d8fe66aee5728cbc6f
3
+ metadata.gz: 38358431f3a00f55f6a46aaa72941c1cbe0fe5f81d6a2fd898a019e09c2cf069
4
+ data.tar.gz: 218c09133f6cc7220545fe2fbdf34deefc8a6ab4941d6ef9e729d264f6ecca93
5
5
  SHA512:
6
- metadata.gz: 0eabefe7ff1bdc1091c493aa6749ed16774733e8676bd5c497d7718554be250f32ff377c96b4258960452d5313c0130b796610dcd17b5a073bfb0a6a012f7f99
7
- data.tar.gz: 29412891a3c5ace497b81ede1ec6fe192a278716434b909ef71359286d443c96ce68275fd56c5b94f43fd00a2883a804311b55f54d8d57a5bd4c7ff275065632
6
+ metadata.gz: 8674d76046f76b6ce08e0e1a7c2a1d8c12f55bf549b4ea580fcfcf1b6a5b50cda6caa6621b684fd94a41a8bad3a25044de5ea2b8b2f68e7496e6dec00e86d3ea
7
+ data.tar.gz: 268b513fff84704447102ee24f1cfef76e271c73a7ab7379eef1fc1093307c69d462d0679eb387d85bc90031e588d399b53ba2c233c94829e335efe4abf7eee6
@@ -0,0 +1,50 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ "master" ]
13
+ pull_request:
14
+ branches: [ "master" ]
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ test:
21
+
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby-version: ['2.7', '3.0']
26
+
27
+ steps:
28
+ - uses: actions/checkout@v3
29
+ - name: Set up Ruby
30
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
+ # uses: ruby/setup-ruby@v1
33
+ uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
34
+ with:
35
+ ruby-version: ${{ matrix.ruby-version }}
36
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37
+ - name: Setup Code Climate test-reporter
38
+ run: |
39
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
40
+ chmod +x ./cc-test-reporter
41
+ ./cc-test-reporter before-build
42
+ - name: Run the default task
43
+ env:
44
+ CC_TEST_REPORTER_ID: 3484f4290e34aa523ef280da3743fd5dc86a443fd7d8a1e5ba2339d1f63308d8
45
+ run: bundle exec rake
46
+ - name: Publish code coverage
47
+ run: |
48
+ export GIT_BRANCH="master"
49
+ ./cc-test-reporter after-build -d -t simplecov -r 3484f4290e34aa523ef280da3743fd5dc86a443fd7d8a1e5ba2339d1f63308d8
50
+
data/.gitignore CHANGED
File without changes
data/.rspec CHANGED
File without changes
data/.rubocop.yml ADDED
@@ -0,0 +1,188 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ Exclude:
5
+ - ./spec/spec_helper.rb
6
+ - ./spec/**/*.rb
7
+ - vendor/bundle/**/*
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - ./spec/**/*.rb
15
+ - ./easy_params.gemspec
16
+
17
+ Gemspec/DeprecatedAttributeAssignment: # new in 1.30
18
+ Enabled: true
19
+ Gemspec/DevelopmentDependencies: # new in 1.44
20
+ Enabled: true
21
+ Gemspec/RequireMFA: # new in 1.23
22
+ Enabled: false
23
+ Layout/LineContinuationLeadingSpace: # new in 1.31
24
+ Enabled: true
25
+ Layout/LineContinuationSpacing: # new in 1.31
26
+ Enabled: true
27
+ Layout/LineEndStringConcatenationIndentation: # new in 1.18
28
+ Enabled: true
29
+ Layout/SpaceBeforeBrackets: # new in 1.7
30
+ Enabled: true
31
+ Lint/AmbiguousAssignment: # new in 1.7
32
+ Enabled: true
33
+ Lint/AmbiguousOperatorPrecedence: # new in 1.21
34
+ Enabled: true
35
+ Lint/AmbiguousRange: # new in 1.19
36
+ Enabled: true
37
+ Lint/ConstantOverwrittenInRescue: # new in 1.31
38
+ Enabled: true
39
+ Lint/DeprecatedConstants: # new in 1.8
40
+ Enabled: true
41
+ Lint/DuplicateBranch: # new in 1.3
42
+ Enabled: true
43
+ Lint/DuplicateMagicComment: # new in 1.37
44
+ Enabled: true
45
+ Lint/DuplicateMatchPattern: # new in 1.50
46
+ Enabled: true
47
+ Lint/DuplicateRegexpCharacterClassElement: # new in 1.1
48
+ Enabled: true
49
+ Lint/EmptyBlock: # new in 1.1
50
+ Enabled: true
51
+ Lint/EmptyClass: # new in 1.3
52
+ Enabled: true
53
+ Lint/EmptyInPattern: # new in 1.16
54
+ Enabled: true
55
+ Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
56
+ Enabled: true
57
+ Lint/LambdaWithoutLiteralBlock: # new in 1.8
58
+ Enabled: true
59
+ Lint/NoReturnInBeginEndBlocks: # new in 1.2
60
+ Enabled: true
61
+ Lint/NonAtomicFileOperation: # new in 1.31
62
+ Enabled: true
63
+ Lint/NumberedParameterAssignment: # new in 1.9
64
+ Enabled: true
65
+ Lint/OrAssignmentToConstant: # new in 1.9
66
+ Enabled: true
67
+ Lint/RedundantDirGlobSort: # new in 1.8
68
+ Enabled: true
69
+ Lint/RefinementImportMethods: # new in 1.27
70
+ Enabled: true
71
+ Lint/RequireRangeParentheses: # new in 1.32
72
+ Enabled: true
73
+ Lint/RequireRelativeSelfPath: # new in 1.22
74
+ Enabled: true
75
+ Lint/SymbolConversion: # new in 1.9
76
+ Enabled: true
77
+ Lint/ToEnumArguments: # new in 1.1
78
+ Enabled: true
79
+ Lint/TripleQuotes: # new in 1.9
80
+ Enabled: true
81
+ Lint/UnexpectedBlockArity: # new in 1.5
82
+ Enabled: true
83
+ Lint/UnmodifiedReduceAccumulator: # new in 1.1
84
+ Enabled: true
85
+ Lint/UselessRescue: # new in 1.43
86
+ Enabled: true
87
+ Lint/UselessRuby2Keywords: # new in 1.23
88
+ Enabled: true
89
+ Metrics/CollectionLiteralLength: # new in 1.47
90
+ Enabled: true
91
+ Naming/BlockForwarding: # new in 1.24
92
+ Enabled: true
93
+ Security/CompoundHash: # new in 1.28
94
+ Enabled: true
95
+ Security/IoMethods: # new in 1.22
96
+ Enabled: true
97
+ Style/ArgumentsForwarding: # new in 1.1
98
+ Enabled: true
99
+ Style/ArrayIntersect: # new in 1.40
100
+ Enabled: true
101
+ Style/CollectionCompact: # new in 1.2
102
+ Enabled: true
103
+ Style/ComparableClamp: # new in 1.44
104
+ Enabled: true
105
+ Style/ConcatArrayLiterals: # new in 1.41
106
+ Enabled: true
107
+ Style/DataInheritance: # new in 1.49
108
+ Enabled: true
109
+ Style/DirEmpty: # new in 1.48
110
+ Enabled: true
111
+ Style/DocumentDynamicEvalDefinition: # new in 1.1
112
+ Enabled: true
113
+ Style/EmptyHeredoc: # new in 1.32
114
+ Enabled: true
115
+ Style/EndlessMethod: # new in 1.8
116
+ Enabled: true
117
+ Style/EnvHome: # new in 1.29
118
+ Enabled: true
119
+ Style/FetchEnvVar: # new in 1.28
120
+ Enabled: true
121
+ Style/FileEmpty: # new in 1.48
122
+ Enabled: true
123
+ Style/FileRead: # new in 1.24
124
+ Enabled: true
125
+ Style/FileWrite: # new in 1.24
126
+ Enabled: true
127
+ Style/HashConversion: # new in 1.10
128
+ Enabled: true
129
+ Style/HashExcept: # new in 1.7
130
+ Enabled: true
131
+ Style/IfWithBooleanLiteralBranches: # new in 1.9
132
+ Enabled: true
133
+ Style/InPatternThen: # new in 1.16
134
+ Enabled: true
135
+ Style/MagicCommentFormat: # new in 1.35
136
+ Enabled: true
137
+ Style/MapCompactWithConditionalBlock: # new in 1.30
138
+ Enabled: true
139
+ Style/MapToHash: # new in 1.24
140
+ Enabled: true
141
+ Style/MapToSet: # new in 1.42
142
+ Enabled: true
143
+ Style/MinMaxComparison: # new in 1.42
144
+ Enabled: true
145
+ Style/MultilineInPatternThen: # new in 1.16
146
+ Enabled: true
147
+ Style/NegatedIfElseCondition: # new in 1.2
148
+ Enabled: true
149
+ Style/NestedFileDirname: # new in 1.26
150
+ Enabled: true
151
+ Style/NilLambda: # new in 1.3
152
+ Enabled: true
153
+ Style/NumberedParameters: # new in 1.22
154
+ Enabled: true
155
+ Style/NumberedParametersLimit: # new in 1.22
156
+ Enabled: true
157
+ Style/ObjectThen: # new in 1.28
158
+ Enabled: true
159
+ Style/OpenStructUse: # new in 1.23
160
+ Enabled: true
161
+ Style/OperatorMethodCall: # new in 1.37
162
+ Enabled: true
163
+ Style/QuotedSymbols: # new in 1.16
164
+ Enabled: true
165
+ Style/RedundantArgument: # new in 1.4
166
+ Enabled: true
167
+ Style/RedundantConstantBase: # new in 1.40
168
+ Enabled: true
169
+ Style/RedundantDoubleSplatHashBraces: # new in 1.41
170
+ Enabled: true
171
+ Style/RedundantEach: # new in 1.38
172
+ Enabled: true
173
+ Style/RedundantHeredocDelimiterQuotes: # new in 1.45
174
+ Enabled: true
175
+ Style/RedundantInitialize: # new in 1.27
176
+ Enabled: true
177
+ Style/RedundantLineContinuation: # new in 1.49
178
+ Enabled: true
179
+ Style/RedundantSelfAssignmentBranch: # new in 1.19
180
+ Enabled: true
181
+ Style/RedundantStringEscape: # new in 1.37
182
+ Enabled: true
183
+ Style/SelectByRegexp: # new in 1.22
184
+ Enabled: true
185
+ Style/StringChars: # new in 1.12
186
+ Enabled: true
187
+ Style/SwapValues: # new in 1.1
188
+ Enabled: true
data/.travis.yml CHANGED
File without changes
data/CODE_OF_CONDUCT.md CHANGED
File without changes
data/Gemfile CHANGED
@@ -1,7 +1,13 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in easy_params.gemspec
4
6
  gemspec
5
7
 
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
8
+ gem 'pry'
9
+ gem 'rake', '~> 12.0'
10
+ gem 'rspec', '~> 3.0'
11
+ gem 'rspec_vars_helper', '~> 0.1'
12
+ gem 'rubocop'
13
+ gem 'simplecov', '~> 0.17'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_params (0.2.1)
4
+ easy_params (0.3.1)
5
5
  activemodel (>= 3.2, < 8)
6
6
  dry-struct (~> 1.4)
7
7
  dry-types (~> 1.5)
@@ -9,14 +9,16 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activemodel (7.0.1)
13
- activesupport (= 7.0.1)
14
- activesupport (7.0.1)
12
+ activemodel (7.0.6)
13
+ activesupport (= 7.0.6)
14
+ activesupport (7.0.6)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
16
  i18n (>= 1.6, < 2)
17
17
  minitest (>= 5.1)
18
18
  tzinfo (~> 2.0)
19
- concurrent-ruby (1.1.9)
19
+ ast (2.4.2)
20
+ coderay (1.1.3)
21
+ concurrent-ruby (1.2.2)
20
22
  diff-lcs (1.4.4)
21
23
  docile (1.3.5)
22
24
  dry-configurable (0.14.0)
@@ -27,7 +29,7 @@ GEM
27
29
  dry-configurable (~> 0.13, >= 0.13.0)
28
30
  dry-core (0.7.1)
29
31
  concurrent-ruby (~> 1.0)
30
- dry-inflector (0.2.1)
32
+ dry-inflector (0.3.0)
31
33
  dry-logic (1.2.0)
32
34
  concurrent-ruby (~> 1.0)
33
35
  dry-core (~> 0.5, >= 0.5)
@@ -41,11 +43,24 @@ GEM
41
43
  dry-core (~> 0.5, >= 0.5)
42
44
  dry-inflector (~> 0.1, >= 0.1.2)
43
45
  dry-logic (~> 1.0, >= 1.0.2)
44
- i18n (1.9.1)
46
+ i18n (1.14.1)
45
47
  concurrent-ruby (~> 1.0)
46
48
  ice_nine (0.11.2)
47
- minitest (5.15.0)
49
+ json (2.6.3)
50
+ method_source (1.0.0)
51
+ minitest (5.18.1)
52
+ parallel (1.23.0)
53
+ parser (3.2.2.3)
54
+ ast (~> 2.4.1)
55
+ racc
56
+ pry (0.14.2)
57
+ coderay (~> 1.1)
58
+ method_source (~> 1.0)
59
+ racc (1.7.1)
60
+ rainbow (3.1.1)
48
61
  rake (12.3.3)
62
+ regexp_parser (2.8.1)
63
+ rexml (3.2.5)
49
64
  rspec (3.10.0)
50
65
  rspec-core (~> 3.10.0)
51
66
  rspec-expectations (~> 3.10.0)
@@ -61,23 +76,39 @@ GEM
61
76
  rspec-support (3.10.0)
62
77
  rspec_vars_helper (0.1.0)
63
78
  rspec (>= 2.4)
79
+ rubocop (1.52.1)
80
+ json (~> 2.3)
81
+ parallel (~> 1.10)
82
+ parser (>= 3.2.2.3)
83
+ rainbow (>= 2.2.2, < 4.0)
84
+ regexp_parser (>= 1.8, < 3.0)
85
+ rexml (>= 3.2.5, < 4.0)
86
+ rubocop-ast (>= 1.28.0, < 2.0)
87
+ ruby-progressbar (~> 1.7)
88
+ unicode-display_width (>= 2.4.0, < 3.0)
89
+ rubocop-ast (1.29.0)
90
+ parser (>= 3.2.1.0)
91
+ ruby-progressbar (1.13.0)
64
92
  simplecov (0.21.2)
65
93
  docile (~> 1.1)
66
94
  simplecov-html (~> 0.11)
67
95
  simplecov_json_formatter (~> 0.1)
68
96
  simplecov-html (0.12.3)
69
97
  simplecov_json_formatter (0.1.2)
70
- tzinfo (2.0.4)
98
+ tzinfo (2.0.6)
71
99
  concurrent-ruby (~> 1.0)
100
+ unicode-display_width (2.4.2)
72
101
 
73
102
  PLATFORMS
74
103
  ruby
75
104
 
76
105
  DEPENDENCIES
77
106
  easy_params!
107
+ pry
78
108
  rake (~> 12.0)
79
109
  rspec (~> 3.0)
80
110
  rspec_vars_helper (~> 0.1)
111
+ rubocop
81
112
  simplecov (~> 0.17)
82
113
 
83
114
  BUNDLED WITH
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # EasyParams
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/nina.svg)](https://badge.fury.io/rb/nina)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/17872804ce576b8b0df2/maintainability)](https://codeclimate.com/github/andriy-baran/easy_params/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/17872804ce576b8b0df2/test_coverage)](https://codeclimate.com/github/andriy-baran/easy_params/test_coverage)
6
+
7
+ Provides an easy way define structure, validation rules, type coercion and set default values for any hash-like structure. It's built on top of `dry-types`, `dry-structure` and `active_model/validations`.
8
+
9
+ ## Types
10
+
11
+ Dry types are wrapped by class methods. Avaliable types: `integer`, `decimal`, `float`, `bool`, `string`, `array`, `date`, `datetime`, `time`, `struct`
12
+
3
13
  ## Installation
4
14
 
5
15
  Add this line to your application's Gemfile:
@@ -41,13 +51,13 @@ end
41
51
  Validation messages for nested attributes will look like this.
42
52
  ```ruby
43
53
  {
44
- :"sections/0/id"=>an_instance_of(Array),
45
- :"sections/0/post/id"=>an_instance_of(Array),
46
- :"post/id"=>an_instance_of(Array),
47
- :"post/sections/0/id"=>an_instance_of(Array)
54
+ :"sections[0].id"=>an_instance_of(Array),
55
+ :"sections[0].post.id"=>an_instance_of(Array),
56
+ :"post.id"=>an_instance_of(Array),
57
+ :"post.sections[0].id"=>an_instance_of(Array)
48
58
  }
49
59
  ```
50
- Optionally you can use more compact form
60
+ Optionally you can use more compact form
51
61
  ```ruby
52
62
  class MyParams < EasyParams::Base
53
63
  extend EasyParams::DSL
@@ -61,6 +71,8 @@ class MyParams < EasyParams::Base
61
71
  end
62
72
  end
63
73
  ```
74
+ This hovewer has some limitations: for attributes have name like `integer`, `decimal`, `float`, `bool`, `string`, `array`, `date`, `datetime`, `time`, `struct` it won't work as expected.
75
+
64
76
  More examples here [params_spec.rb](https://github.com/andriy-baran/easy_params/blob/master/spec/easy_params_spec.rb)
65
77
 
66
78
  ## Development
data/Rakefile CHANGED
@@ -1,6 +1,12 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "easy_params"
4
+ require 'bundler/setup'
5
+ require 'easy_params'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "easy_params"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
data/easy_params.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/easy_params/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
@@ -6,11 +8,11 @@ Gem::Specification.new do |spec|
6
8
  spec.authors = ['Andrii Baran']
7
9
  spec.email = ['andriy.baran.v@gmail.com']
8
10
 
9
- spec.summary = %q{A tool that handles common tasks needed when working with params in Rails}
10
- spec.description = %q{Dessribe structure, validate and coerce values. Powered by dry-types and dry-struct}
11
+ spec.summary = 'A tool that handles common tasks needed when working with params in Rails'
12
+ spec.description = 'Dessribe structure, validate and coerce values. Powered by dry-types and dry-struct'
11
13
  spec.homepage = 'https://github.com/andriy-baran/easy_params'
12
14
  spec.license = 'MIT'
13
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
14
16
 
15
17
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
16
18
 
@@ -20,7 +22,7 @@ Gem::Specification.new do |spec|
20
22
 
21
23
  # Specify which files should be added to the gem when it is released.
22
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
27
  end
26
28
  spec.bindir = 'exe'
@@ -34,8 +36,4 @@ Gem::Specification.new do |spec|
34
36
  # spec.add_dependency 'dry-logic', '~> 0.4.2'
35
37
  spec.add_dependency 'dry-struct', '~> 1.4'
36
38
  spec.add_dependency 'dry-types', '~> 1.5'
37
-
38
- spec.add_development_dependency 'rspec', '~> 3.0'
39
- spec.add_development_dependency 'rspec_vars_helper', '~> 0.1'
40
- spec.add_development_dependency 'simplecov', '~> 0.17'
41
39
  end
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyParams
4
+ # Implements validations logic and nesting structures
2
5
  class Base < Dry::Struct
3
6
  include ActiveModel::Validations
4
7
 
@@ -16,29 +19,49 @@ module EasyParams
16
19
  validate_nested
17
20
  end
18
21
 
22
+ private
23
+
19
24
  def validate_nested
20
- run_nested_validations = proc do |attr_name, value, array_index, error_key_prefix|
25
+ attributes.each do |_, value|
26
+ case value
27
+ when *EasyParams::Types::ARRAY_OF_STRUCTS_TYPES_LIST
28
+ value.each(&:valid?)
29
+ when *EasyParams::Types::STRUCT_TYPES_LIST
30
+ value.valid?
31
+ end
32
+ end
33
+ attributes.each(&aggregate_nested_errors)
34
+ end
35
+
36
+ def aggregate_nested_errors
37
+ proc do |attr_name, value, array_index, error_key_prefix|
21
38
  case value
22
- when Array
39
+ when *EasyParams::Types::ARRAY_OF_STRUCTS_TYPES_LIST
23
40
  value.each.with_index do |element, i|
24
- run_nested_validations[attr_name, element, i, error_key_prefix]
25
- end
26
- when self.class.struct
27
- if value.invalid?
28
- error_key_components = [error_key_prefix, attr_name, array_index]
29
- attr_error_key_prefix = error_key_components.compact.join('/')
30
- value.errors.each do |error_key, error_message|
31
- errors.add("#{attr_error_key_prefix}/#{error_key}", error_message)
32
- end
41
+ aggregate_nested_errors[attr_name, element, "[#{i}]", error_key_prefix]
33
42
  end
34
- value.attributes.each do |nested_attr_name, nested_value|
35
- run_nested_validations[nested_attr_name, nested_value, nil, attr_error_key_prefix]
36
- end
37
- else
38
- # NOOP
43
+ when *EasyParams::Types::STRUCT_TYPES_LIST
44
+ handle_nested_errors(value, error_key_prefix, attr_name, array_index)
39
45
  end
40
46
  end
41
- attributes.each(&run_nested_validations)
47
+ end
48
+
49
+ def handle_nested_errors(value, error_key_prefix, attr_name, array_index)
50
+ return if value.errors.blank?
51
+
52
+ error_key_components = [error_key_prefix, attr_name, array_index]
53
+ attr_error_key_prefix = error_key_components.compact.join('.').gsub(/\.\[(\d+)\]/, '[\1]')
54
+ add_errors_on_top_level(value, attr_error_key_prefix)
55
+ end
56
+
57
+ if defined? ActiveModel::Error
58
+ def add_errors_on_top_level(value, attr_error_key_prefix)
59
+ value.errors.each { |error| errors.add("#{attr_error_key_prefix}.#{error.attribute}", error.message) }
60
+ end
61
+ else
62
+ def add_errors_on_top_level(value, attr_error_key_prefix)
63
+ value.errors.each { |key, message| errors.add("#{attr_error_key_prefix}.#{key}", message) }
64
+ end
42
65
  end
43
66
  end
44
67
  end
@@ -1,4 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyParams
4
+ # Makes definition more compact
5
+ # Do not use if your attributes have name like
6
+ # integer, decimal, float, bool, string, array, date, datetime, time, struct
2
7
  module DSL
3
8
  def each(&block)
4
9
  array.of(struct_dsl, &block)
@@ -11,5 +16,9 @@ module EasyParams
11
16
  def method_missing(method_name, *args, &block)
12
17
  public_send(:attribute, method_name, *args, &block)
13
18
  end
19
+
20
+ def respond_to_missing?(*)
21
+ true
22
+ end
14
23
  end
15
24
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyParams
2
4
  module Types
3
5
  Struct = EasyParams::Base.meta(omittable: true)
@@ -7,9 +9,12 @@ module EasyParams
7
9
  Float = Dry::Types['params.float'].optional.meta(omittable: true).default(nil)
8
10
  Bool = Dry::Types['strict.bool'].optional.meta(omittable: true).default(nil)
9
11
  String = Dry::Types['string'].optional.meta(omittable: true).default(nil)
10
- Array = Dry::Types['array'].of(Struct).meta(omittable: true).default([])
12
+ Array = Dry::Types['array'].meta(omittable: true).default([])
11
13
  Date = Dry::Types['params.date'].optional.meta(omittable: true).default(nil)
12
14
  DateTime = Dry::Types['params.date_time'].optional.meta(omittable: true).default(nil)
13
15
  Time = Dry::Types['params.time'].optional.meta(omittable: true).default(nil)
16
+
17
+ STRUCT_TYPES_LIST = [Struct, StructDSL].freeze
18
+ ARRAY_OF_STRUCTS_TYPES_LIST = [Array.of(Struct), Array.of(StructDSL)].freeze
14
19
  end
15
20
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyParams
2
- VERSION = '0.2.1'.freeze
4
+ VERSION = '0.3.1'
3
5
  end
data/lib/easy_params.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'dry-struct'
2
4
  require 'dry-types'
3
5
  require 'active_model'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-28 00:00:00.000000000 Z
11
+ date: 2023-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -58,48 +58,6 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '1.5'
61
- - !ruby/object:Gem::Dependency
62
- name: rspec
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '3.0'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '3.0'
75
- - !ruby/object:Gem::Dependency
76
- name: rspec_vars_helper
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '0.1'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '0.1'
89
- - !ruby/object:Gem::Dependency
90
- name: simplecov
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '0.17'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: '0.17'
103
61
  description: Dessribe structure, validate and coerce values. Powered by dry-types
104
62
  and dry-struct
105
63
  email:
@@ -108,8 +66,10 @@ executables: []
108
66
  extensions: []
109
67
  extra_rdoc_files: []
110
68
  files:
69
+ - ".github/workflows/ruby.yml"
111
70
  - ".gitignore"
112
71
  - ".rspec"
72
+ - ".rubocop.yml"
113
73
  - ".travis.yml"
114
74
  - CODE_OF_CONDUCT.md
115
75
  - Gemfile
@@ -132,7 +92,7 @@ metadata:
132
92
  allowed_push_host: https://rubygems.org
133
93
  homepage_uri: https://github.com/andriy-baran/easy_params
134
94
  source_code_uri: https://github.com/andriy-baran/easy_params
135
- post_install_message:
95
+ post_install_message:
136
96
  rdoc_options: []
137
97
  require_paths:
138
98
  - lib
@@ -140,15 +100,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
100
  requirements:
141
101
  - - ">="
142
102
  - !ruby/object:Gem::Version
143
- version: 2.3.0
103
+ version: 2.6.0
144
104
  required_rubygems_version: !ruby/object:Gem::Requirement
145
105
  requirements:
146
106
  - - ">="
147
107
  - !ruby/object:Gem::Version
148
108
  version: '0'
149
109
  requirements: []
150
- rubygems_version: 3.2.3
151
- signing_key:
110
+ rubygems_version: 3.3.26
111
+ signing_key:
152
112
  specification_version: 4
153
113
  summary: A tool that handles common tasks needed when working with params in Rails
154
114
  test_files: []