easy_params 0.2.0 → 0.3.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: 8dc0f5627888a21c92ef0ebfe212bf74035feeb5cc3579c8a710c7128445d863
4
- data.tar.gz: 809f1077535f33db3b03b15fd5aabe93f4478cb1c8e577b06ec6c4c92622fbe4
3
+ metadata.gz: d5a19f19d20a42ef6a6ba3e606298c3dbf696a22c9dcdc9e42a8ffe48e1e67db
4
+ data.tar.gz: fce70967d59989d5e650289483d5437f4ebb80793d49b3a446d252796017117c
5
5
  SHA512:
6
- metadata.gz: b9054b406b6b55a066e4556128bf95c4224f4273b65c6086d0d07e5f20f175118b8dcf097e0de3e7e9bad65456ac491b0aa7077547cc18d88b89f0f03c73882d
7
- data.tar.gz: f26d3a0f8fb1d275232f7cba454181fc47e6a5444e65c36be534fd6a39e7734b79fcd9d8dca402749947472cfdcd4b60226d3b7c846dcbffbb36f857f4aed19b
6
+ metadata.gz: 3f690f1a17df11da94e8f707c99f97854aec48ca890320c0345159286c4e10ac19448a5b3a6557936c6076b4bf4e886cd51841ef394b3a205c91626a680b598f
7
+ data.tar.gz: c43521492ce73961d7ff7fb562ba7b8ba944f03c928a8c68298f8f6f168c21ab698d4acdf3fe2dd073296bec99418cf14adffd8446cc751541b32136b452e972
@@ -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/.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/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,35 +1,36 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_params (0.2.0)
5
- activemodel (>= 3.2, < 6.1)
4
+ easy_params (0.3.0)
5
+ activemodel (>= 3.2, < 8)
6
6
  dry-struct (~> 1.4)
7
7
  dry-types (~> 1.5)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- activemodel (6.0.3.5)
13
- activesupport (= 6.0.3.5)
14
- activesupport (6.0.3.5)
12
+ activemodel (7.0.4.3)
13
+ activesupport (= 7.0.4.3)
14
+ activesupport (7.0.4.3)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
- i18n (>= 0.7, < 2)
17
- minitest (~> 5.1)
18
- tzinfo (~> 1.1)
19
- zeitwerk (~> 2.2, >= 2.2.2)
20
- concurrent-ruby (1.1.8)
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ tzinfo (~> 2.0)
19
+ ast (2.4.2)
20
+ coderay (1.1.3)
21
+ concurrent-ruby (1.2.2)
21
22
  diff-lcs (1.4.4)
22
23
  docile (1.3.5)
23
- dry-configurable (0.12.1)
24
+ dry-configurable (0.14.0)
24
25
  concurrent-ruby (~> 1.0)
25
- dry-core (~> 0.5, >= 0.5.0)
26
- dry-container (0.7.2)
26
+ dry-core (~> 0.6)
27
+ dry-container (0.9.0)
27
28
  concurrent-ruby (~> 1.0)
28
- dry-configurable (~> 0.1, >= 0.1.3)
29
- dry-core (0.5.0)
29
+ dry-configurable (~> 0.13, >= 0.13.0)
30
+ dry-core (0.7.1)
30
31
  concurrent-ruby (~> 1.0)
31
- dry-inflector (0.2.0)
32
- dry-logic (1.1.0)
32
+ dry-inflector (0.3.0)
33
+ dry-logic (1.2.0)
33
34
  concurrent-ruby (~> 1.0)
34
35
  dry-core (~> 0.5, >= 0.5)
35
36
  dry-struct (1.4.0)
@@ -42,11 +43,24 @@ GEM
42
43
  dry-core (~> 0.5, >= 0.5)
43
44
  dry-inflector (~> 0.1, >= 0.1.2)
44
45
  dry-logic (~> 1.0, >= 1.0.2)
45
- i18n (1.8.9)
46
+ i18n (1.14.1)
46
47
  concurrent-ruby (~> 1.0)
47
48
  ice_nine (0.11.2)
48
- minitest (5.14.4)
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)
49
61
  rake (12.3.3)
62
+ regexp_parser (2.8.1)
63
+ rexml (3.2.5)
50
64
  rspec (3.10.0)
51
65
  rspec-core (~> 3.10.0)
52
66
  rspec-expectations (~> 3.10.0)
@@ -62,26 +76,40 @@ GEM
62
76
  rspec-support (3.10.0)
63
77
  rspec_vars_helper (0.1.0)
64
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)
65
92
  simplecov (0.21.2)
66
93
  docile (~> 1.1)
67
94
  simplecov-html (~> 0.11)
68
95
  simplecov_json_formatter (~> 0.1)
69
96
  simplecov-html (0.12.3)
70
97
  simplecov_json_formatter (0.1.2)
71
- thread_safe (0.3.6)
72
- tzinfo (1.2.9)
73
- thread_safe (~> 0.1)
74
- zeitwerk (2.4.2)
98
+ tzinfo (2.0.6)
99
+ concurrent-ruby (~> 1.0)
100
+ unicode-display_width (2.4.2)
75
101
 
76
102
  PLATFORMS
77
103
  ruby
78
104
 
79
105
  DEPENDENCIES
80
106
  easy_params!
107
+ pry
81
108
  rake (~> 12.0)
82
109
  rspec (~> 3.0)
83
110
  rspec_vars_helper (~> 0.1)
111
+ rubocop
84
112
  simplecov (~> 0.17)
85
113
 
86
114
  BUNDLED WITH
87
- 2.1.4
115
+ 2.2.3
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:
@@ -47,7 +57,7 @@ Validation messages for nested attributes will look like this.
47
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,24 +22,18 @@ 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'
27
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
30
  spec.require_paths = ['lib']
29
31
 
30
- spec.required_ruby_version = '>= 2.2.2'
31
-
32
- version_string = ['>= 3.2', '< 6.1']
32
+ version_string = ['>= 3.2', '< 8']
33
33
 
34
34
  spec.add_runtime_dependency 'activemodel', version_string
35
35
 
36
36
  # spec.add_dependency 'dry-logic', '~> 0.4.2'
37
37
  spec.add_dependency 'dry-struct', '~> 1.4'
38
38
  spec.add_dependency 'dry-types', '~> 1.5'
39
-
40
- spec.add_development_dependency 'rspec', '~> 3.0'
41
- spec.add_development_dependency 'rspec_vars_helper', '~> 0.1'
42
- spec.add_development_dependency 'simplecov', '~> 0.17'
43
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,48 @@ 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(&run_nested_validations)
26
+ end
27
+
28
+ def run_nested_validations
29
+ proc do |attr_name, value, array_index, error_key_prefix|
21
30
  case value
22
31
  when Array
23
32
  value.each.with_index do |element, i|
24
33
  run_nested_validations[attr_name, element, i, error_key_prefix]
25
34
  end
26
35
  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
33
- 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
36
+ handle_struct_validation(value, error_key_prefix, attr_name, array_index)
39
37
  end
40
38
  end
41
- attributes.each(&run_nested_validations)
39
+ end
40
+
41
+ def handle_struct_validation(value, error_key_prefix, attr_name, array_index)
42
+ if value.invalid?
43
+ error_key_components = [error_key_prefix, attr_name, array_index]
44
+ attr_error_key_prefix = error_key_components.compact.join('/')
45
+ add_errors_on_top_level(value, attr_error_key_prefix)
46
+ end
47
+ value.attributes.each do |nested_attr_name, nested_value|
48
+ run_nested_validations[nested_attr_name, nested_value, nil, attr_error_key_prefix]
49
+ end
50
+ end
51
+
52
+ if defined? ActiveModel::Error
53
+ def add_errors_on_top_level(value, attr_error_key_prefix)
54
+ value.errors.each do |error|
55
+ next unless error.options[:message]
56
+
57
+ errors.add("#{attr_error_key_prefix}/#{error.attribute}", error.options[:message])
58
+ end
59
+ end
60
+ else
61
+ def add_errors_on_top_level(value, attr_error_key_prefix)
62
+ value.errors.each { |key, message| errors.add("#{attr_error_key_prefix}/#{key}", message) }
63
+ end
42
64
  end
43
65
  end
44
66
  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)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyParams
2
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.3.0'
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.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-20 00:00:00.000000000 Z
11
+ date: 2023-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '3.2'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.1'
22
+ version: '8'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '3.2'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.1'
32
+ version: '8'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: dry-struct
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -140,14 +100,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
100
  requirements:
141
101
  - - ">="
142
102
  - !ruby/object:Gem::Version
143
- version: 2.2.2
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.1.4
110
+ rubygems_version: 3.3.26
151
111
  signing_key:
152
112
  specification_version: 4
153
113
  summary: A tool that handles common tasks needed when working with params in Rails