hanami-controller 1.1.0.beta1 → 1.1.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/hanami-controller.gemspec +3 -3
- data/lib/hanami/action/params.rb +97 -4
- data/lib/hanami/controller/version.rb +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 158fe86057c438e5d32d4deafa0913972f98db7a
|
4
|
+
data.tar.gz: e2b1ab0f2a7edbc8ac0ccac2cc53a8bd13971c6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89839ef85004ad52c5ca5a4318b8bae4e95bee858d6f6c15b72a7031440600ebc8f00669c1bf3aac0073834d08e24983ba18e47404ef8dc565933e10da308d6b
|
7
|
+
data.tar.gz: d2851a8b925d3c22a59b0469bcd61edead0d977af5a8713931d90b4060f8d44090741a10caeda8a749db22b4f6c63ac9ed390197c90f645c726587a6d1372ee0
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Hanami::Controller
|
2
2
|
Complete, fast and testable actions for Rack
|
3
3
|
|
4
|
+
## v1.1.0.beta2 - 2017-10-03
|
5
|
+
### Added
|
6
|
+
- [Luca Guidi] Introduce `Hanami::Action::Params::Errors#add` to add errors not generated by params validations
|
7
|
+
|
4
8
|
## v1.1.0.beta1 - 2017-08-11
|
5
9
|
|
6
10
|
## v1.0.1 - 2017-07-10
|
data/hanami-controller.gemspec
CHANGED
@@ -20,10 +20,10 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.required_ruby_version = '>= 2.3.0'
|
21
21
|
|
22
22
|
spec.add_dependency 'rack', '~> 2.0'
|
23
|
-
spec.add_dependency 'hanami-utils', '1.1.0.
|
23
|
+
spec.add_dependency 'hanami-utils', '1.1.0.beta2'
|
24
24
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
26
26
|
spec.add_development_dependency 'rack-test', '~> 0.6'
|
27
|
-
spec.add_development_dependency 'rake', '~>
|
28
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
27
|
+
spec.add_development_dependency 'rake', '~> 12'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.6'
|
29
29
|
end
|
data/lib/hanami/action/params.rb
CHANGED
@@ -16,6 +16,100 @@ module Hanami
|
|
16
16
|
class Params < BaseParams
|
17
17
|
include Hanami::Validations::Form
|
18
18
|
|
19
|
+
# Params errors
|
20
|
+
#
|
21
|
+
# @since 1.1.0
|
22
|
+
class Errors < SimpleDelegator
|
23
|
+
# @since 1.1.0
|
24
|
+
# @api private
|
25
|
+
def initialize(errors = {})
|
26
|
+
super(errors)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Add an error to the param validations
|
30
|
+
#
|
31
|
+
# This has a semantic similar to `Hash#dig` where you use a set of keys
|
32
|
+
# to get a nested value, here you use a set of keys to set a nested
|
33
|
+
# value.
|
34
|
+
#
|
35
|
+
# @param args [Array<Symbol, String>] an array of arguments: the last
|
36
|
+
# one is the message to add (String), while the beginning of the array
|
37
|
+
# is made of keys to reach the attribute.
|
38
|
+
#
|
39
|
+
# @raise [ArgumentError] when try to add a message for a key that is
|
40
|
+
# already filled with incompatible message type.
|
41
|
+
# This usually happens with nested attributes: if you have a `:book`
|
42
|
+
# schema and the input doesn't include data for `:book`, the messages
|
43
|
+
# will be `["is missing"]`. In that case you can't add an error for a
|
44
|
+
# key nested under `:book`.
|
45
|
+
#
|
46
|
+
# @since 1.1.0
|
47
|
+
#
|
48
|
+
# @example Basic usage
|
49
|
+
# require "hanami/controller"
|
50
|
+
#
|
51
|
+
# class MyAction
|
52
|
+
# include Hanami::Action
|
53
|
+
#
|
54
|
+
# params do
|
55
|
+
# required(:book).schema do
|
56
|
+
# required(:isbn).filled(:str?)
|
57
|
+
# end
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# def call(params)
|
61
|
+
# # 1. Don't try to save the record if the params aren't valid
|
62
|
+
# return unless params.valid?
|
63
|
+
#
|
64
|
+
# BookRepository.new.create(params[:book])
|
65
|
+
# rescue Hanami::Model::UniqueConstraintViolationError
|
66
|
+
# # 2. Add an error in case the record wasn't unique
|
67
|
+
# params.errors.add(:book, :isbn, "is not unique")
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# @example Invalid argument
|
72
|
+
# require "hanami/controller"
|
73
|
+
#
|
74
|
+
# class MyAction
|
75
|
+
# include Hanami::Action
|
76
|
+
#
|
77
|
+
# params do
|
78
|
+
# required(:book).schema do
|
79
|
+
# required(:title).filled(:str?)
|
80
|
+
# end
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# def call(params)
|
84
|
+
# puts params.to_h # => {}
|
85
|
+
# puts params.valid? # => false
|
86
|
+
# puts params.error_messages # => ["Book is missing"]
|
87
|
+
# puts params.errors # => {:book=>["is missing"]}
|
88
|
+
#
|
89
|
+
# params.errors.add(:book, :isbn, "is not unique") # => ArgumentError
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
def add(*args)
|
93
|
+
*keys, key, error = args
|
94
|
+
_nested_attribute(keys, key) << error
|
95
|
+
rescue TypeError
|
96
|
+
raise ArgumentError.new("Can't add #{args.map(&:inspect).join(', ')} to #{inspect}")
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
# @since 1.1.0
|
102
|
+
# @api private
|
103
|
+
def _nested_attribute(keys, key)
|
104
|
+
if keys.empty?
|
105
|
+
self
|
106
|
+
else
|
107
|
+
keys.inject(self) { |result, k| result[k] ||= {} }
|
108
|
+
dig(*keys)
|
109
|
+
end[key] ||= []
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
19
113
|
# This is a Hanami::Validations extension point
|
20
114
|
#
|
21
115
|
# @since 0.7.0
|
@@ -71,6 +165,7 @@ module Hanami
|
|
71
165
|
super(_extract_params)
|
72
166
|
@result = validate
|
73
167
|
@params = _params
|
168
|
+
@errors = Errors.new(@result.messages)
|
74
169
|
freeze
|
75
170
|
end
|
76
171
|
|
@@ -92,9 +187,7 @@ module Hanami
|
|
92
187
|
# @example
|
93
188
|
# params.errors
|
94
189
|
# # => {:email=>["is missing", "is in invalid format"], :name=>["is missing"], :tos=>["is missing"], :age=>["is missing"], :address=>["is missing"]}
|
95
|
-
|
96
|
-
@result.messages
|
97
|
-
end
|
190
|
+
attr_reader :errors
|
98
191
|
|
99
192
|
# Returns flat collection of full error messages
|
100
193
|
#
|
@@ -129,7 +222,7 @@ module Hanami
|
|
129
222
|
# @example
|
130
223
|
# params.valid? # => true
|
131
224
|
def valid?
|
132
|
-
|
225
|
+
errors.empty?
|
133
226
|
end
|
134
227
|
|
135
228
|
# Serialize params to Hash
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanami-controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.0.
|
4
|
+
version: 1.1.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.1.0.
|
33
|
+
version: 1.1.0.beta2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 1.1.0.
|
40
|
+
version: 1.1.0.beta2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,28 +72,28 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '12'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '12'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rspec
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '3.
|
89
|
+
version: '3.6'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '3.
|
96
|
+
version: '3.6'
|
97
97
|
description: Complete, fast and testable actions for Rack
|
98
98
|
email:
|
99
99
|
- me@lucaguidi.com
|
@@ -158,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
version: 1.3.1
|
159
159
|
requirements: []
|
160
160
|
rubyforge_project:
|
161
|
-
rubygems_version: 2.6.
|
161
|
+
rubygems_version: 2.6.13
|
162
162
|
signing_key:
|
163
163
|
specification_version: 4
|
164
164
|
summary: Complete, fast and testable actions for Rack and Hanami
|