broi-input 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/input.gemspec +1 -1
- data/lib/broi/errors.rb +49 -0
- data/lib/broi/input/success.rb +1 -1
- data/lib/broi/input.rb +2 -11
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e13c93b82d7cb1ba3fa069fcb9c5bda023c61c6988281f7147b767043a97d7dc
|
4
|
+
data.tar.gz: fa65e198e065ae420b64b9e9134d23cb43359858f59dbdcef13e49ee7b43f7f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 150a21be121b39c47a2e85a00fcc406521ae6b4185ca034ede43a0250e410e936f493e86f051f132dac15cab6386c6902fb60af7283eac2f92f66ce3ced929b4
|
7
|
+
data.tar.gz: b7e507c92ec3e84d1938e86d67be7f4add22596f5091546dec9ab5c93c4f9b9517b3c57e05886bf54186f560cb961a020357ce6ca7d1b4fa25a711fb85dd66f7
|
data/Gemfile.lock
CHANGED
data/input.gemspec
CHANGED
data/lib/broi/errors.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Broi
|
2
|
+
# TODO: Separate gem?
|
3
|
+
class Errors
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
def initialize(error_hash = {})
|
7
|
+
@errors = errorize_hash(error_hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_nested_hash
|
11
|
+
Broi::Input::Utils.deep_transform_values(@errors) do |value|
|
12
|
+
value.respond_to?(:to_nested_hash) ? value.to_nested_hash : value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_flat_hash
|
17
|
+
result = {}
|
18
|
+
@errors.each do |key, value|
|
19
|
+
if value.respond_to?(:to_flat_hash)
|
20
|
+
value.to_flat_hash.each do |inner_key, inner_value|
|
21
|
+
result[:"#{key}.#{inner_key}"] = inner_value.dup
|
22
|
+
end
|
23
|
+
else
|
24
|
+
result[key] = value.dup
|
25
|
+
end
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def each(&block)
|
31
|
+
to_flat_hash.each(&block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def [](key)
|
35
|
+
key, nested = key.to_s.split('.', 2)
|
36
|
+
return unless (result = @errors[key.to_sym])
|
37
|
+
return result unless nested
|
38
|
+
result[nested]
|
39
|
+
end
|
40
|
+
|
41
|
+
alias empty? none?
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def errorize_hash(hash)
|
46
|
+
hash.map {|k, v| [k.to_sym, v.is_a?(Hash) ? Errors.new(errorize_hash(v)) : v] }.to_h
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/broi/input/success.rb
CHANGED
data/lib/broi/input.rb
CHANGED
@@ -2,6 +2,7 @@ require 'dry-struct'
|
|
2
2
|
require 'dry/validation'
|
3
3
|
require 'dry/monads/result'
|
4
4
|
|
5
|
+
require 'broi/errors'
|
5
6
|
require 'broi/input/success'
|
6
7
|
require 'broi/input/failure'
|
7
8
|
require 'broi/input/utils'
|
@@ -9,8 +10,6 @@ require 'broi/input/value'
|
|
9
10
|
require 'broi/input/invalid_value'
|
10
11
|
require 'broi/input/soft'
|
11
12
|
|
12
|
-
require 'byebug'
|
13
|
-
|
14
13
|
module Broi
|
15
14
|
class Input < Dry::Struct::Value
|
16
15
|
|
@@ -43,18 +42,10 @@ module Broi
|
|
43
42
|
def call(params = {})
|
44
43
|
result = validation.(params)
|
45
44
|
input = Soft.(self, result.output, result.errors)
|
46
|
-
# output = input[result.output]
|
47
|
-
# input = Utils.deep_merge(output, result.errors) do |target, _error|
|
48
|
-
# InvalidValue.new(target)
|
49
|
-
# end
|
50
|
-
# input = Utils.deep_transform_values(input) do |value|
|
51
|
-
# value.is_a?(InvalidValue) ? value : Value.new(value)
|
52
|
-
# end
|
53
|
-
# input = new(input)
|
54
45
|
if result.success?
|
55
46
|
Success.new(input)
|
56
47
|
else
|
57
|
-
Failure.new(input, result.errors)
|
48
|
+
Failure.new(input, Errors.new(result.errors))
|
58
49
|
end
|
59
50
|
end
|
60
51
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: broi-input
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- broisatse
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-validation
|
@@ -141,6 +141,7 @@ files:
|
|
141
141
|
- bin/console
|
142
142
|
- bin/setup
|
143
143
|
- input.gemspec
|
144
|
+
- lib/broi/errors.rb
|
144
145
|
- lib/broi/input.rb
|
145
146
|
- lib/broi/input/failure.rb
|
146
147
|
- lib/broi/input/invalid_error.rb
|