tram-policy 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +10 -0
- data/lib/tram/policy.rb +1 -1
- data/lib/tram/policy/error.rb +10 -6
- data/lib/tram/policy/errors.rb +13 -11
- data/spec/tram/policy/error_spec.rb +3 -2
- data/spec/tram/policy/errors_spec.rb +17 -17
- data/spec/tram/policy_spec.rb +2 -2
- data/tram-policy.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2d94489718b8070f78e8a4792e67902336e87d03da02d216fe49069767684fb
|
4
|
+
data.tar.gz: 4c9e417c8a39f18e0faf75c2b7bae22f7d3aa0e53c2c9486e068910f59837f2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71862e68041ee87d414f60bcf79c5f84f4996db5bc1ddc143628598dc29f0ca4a9816c7669b63a1c10e9e737e371800ff9ad1b214ed814753ce4c393c3877165
|
7
|
+
data.tar.gz: 6278ffc6803fdb5ab6f96bc4c4d2a38ad7758e2eb723fca763234aa647e2ba6b1854220059d2ce0cc182215042b2926080ad39f39eb815614610850d12e2d72c
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
6
|
|
7
|
+
## [2.0.0] - [2019-07-04]
|
8
|
+
|
9
|
+
### Changed
|
10
|
+
|
11
|
+
- [BREAKING] separate `Tram::Policy::Errors` from a policy (nepalez, sclinede)
|
12
|
+
|
13
|
+
Instead of the policy, the collection refers to the explicit scope used for error messages' translation.
|
14
|
+
This change breaks the signature of `Tram::Policy::Error` and `Tram::Policy::Errors`.
|
15
|
+
|
7
16
|
## [1.0.1] - [2019-05-06]
|
8
17
|
|
9
18
|
### Added
|
@@ -202,3 +211,4 @@ This is a first public release (@nepalez, @charlie-wasp, @JewelSam, @sergey-chec
|
|
202
211
|
[0.4.0]: https://github.com/tram-rb/tram-policy/compare/v0.3.1...v0.4.0
|
203
212
|
[1.0.0]: https://github.com/tram-rb/tram-policy/compare/v0.4.0...v1.0.0
|
204
213
|
[1.0.1]: https://github.com/tram-rb/tram-policy/compare/v1.0.0...v1.0.1
|
214
|
+
[2.0.0]: https://github.com/tram-rb/tram-policy/compare/v1.0.1...v2.0.0
|
data/lib/tram/policy.rb
CHANGED
data/lib/tram/policy/error.rb
CHANGED
@@ -12,10 +12,10 @@ class Tram::Policy
|
|
12
12
|
# If another error is send to the constructor, the error returned unchanged
|
13
13
|
#
|
14
14
|
# @param [Tram::Policy::Error, #to_s] value
|
15
|
-
# @param [Hash<Symbol, Object>]
|
15
|
+
# @param [Hash<Symbol, Object>] tags
|
16
16
|
# @return [Tram::Policy::Error]
|
17
17
|
#
|
18
|
-
def self.new(value, **
|
18
|
+
def self.new(value, **tags)
|
19
19
|
value.instance_of?(self) ? value : super
|
20
20
|
end
|
21
21
|
|
@@ -27,7 +27,7 @@ class Tram::Policy
|
|
27
27
|
# @return [Hash<Symbol, Object>] error tags
|
28
28
|
attr_reader :tags
|
29
29
|
|
30
|
-
#
|
30
|
+
# List of arguments for [I18n.t]
|
31
31
|
#
|
32
32
|
# @return [Array]
|
33
33
|
#
|
@@ -36,7 +36,7 @@ class Tram::Policy
|
|
36
36
|
end
|
37
37
|
alias to_a item
|
38
38
|
|
39
|
-
#
|
39
|
+
# Text of error message translated to the current locale
|
40
40
|
#
|
41
41
|
# @return [String]
|
42
42
|
#
|
@@ -60,8 +60,8 @@ class Tram::Policy
|
|
60
60
|
# @param [Proc] block
|
61
61
|
# @return [Object]
|
62
62
|
#
|
63
|
-
def fetch(tag, default =
|
64
|
-
if default ==
|
63
|
+
def fetch(tag, default = UNDEFINED, &block)
|
64
|
+
if default == UNDEFINED
|
65
65
|
tags.fetch(tag.to_sym, &block)
|
66
66
|
else
|
67
67
|
tags.fetch(tag.to_sym, default, &block)
|
@@ -92,9 +92,13 @@ class Tram::Policy
|
|
92
92
|
|
93
93
|
private
|
94
94
|
|
95
|
+
UNDEFINED = Dry::Initializer::UNDEFINED
|
96
|
+
DEFAULT_SCOPE = %w[tram-policy errors].freeze
|
97
|
+
|
95
98
|
def initialize(key, **tags)
|
96
99
|
@key = key
|
97
100
|
@tags = tags
|
101
|
+
@tags[:scope] = @tags.fetch(:scope) { DEFAULT_SCOPE } if key.is_a?(Symbol)
|
98
102
|
end
|
99
103
|
|
100
104
|
def respond_to_missing?(*)
|
data/lib/tram/policy/errors.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
class Tram::Policy
|
2
|
+
#
|
2
3
|
# Enumerable collection of unique unordered validation errors
|
3
4
|
#
|
4
5
|
# Notice: A collection is context-dependent;
|
@@ -8,11 +9,9 @@ class Tram::Policy
|
|
8
9
|
class Errors
|
9
10
|
include Enumerable
|
10
11
|
|
11
|
-
# @!attribute [r]
|
12
|
-
#
|
13
|
-
|
14
|
-
#
|
15
|
-
attr_reader :policy
|
12
|
+
# @!attribute [r] scope
|
13
|
+
# @return [Array<String>] the scope for error messages' translation
|
14
|
+
attr_reader :scope
|
16
15
|
|
17
16
|
# @!method add(message, tags)
|
18
17
|
# Adds error message to the collection
|
@@ -22,9 +21,12 @@ class Tram::Policy
|
|
22
21
|
# @return [self] the collection
|
23
22
|
#
|
24
23
|
def add(message, **tags)
|
25
|
-
tags = tags.merge(scope: policy.scope) unless tags.key?(:scope)
|
26
24
|
raise ArgumentError.new("Error message should be defined") unless message
|
27
|
-
|
25
|
+
|
26
|
+
tap do
|
27
|
+
tags = tags.merge(scope: scope) if message.is_a?(Symbol)
|
28
|
+
@set << Tram::Policy::Error.new(message, **tags)
|
29
|
+
end
|
28
30
|
end
|
29
31
|
|
30
32
|
# Iterates by collected errors
|
@@ -47,7 +49,7 @@ class Tram::Policy
|
|
47
49
|
list = each_with_object(Set.new) do |error, obj|
|
48
50
|
obj << error if error.contain?(key, tags)
|
49
51
|
end
|
50
|
-
self.class.new(
|
52
|
+
self.class.new(scope: scope, errors: list)
|
51
53
|
end
|
52
54
|
|
53
55
|
# @!method empty?
|
@@ -101,9 +103,9 @@ class Tram::Policy
|
|
101
103
|
|
102
104
|
private
|
103
105
|
|
104
|
-
def initialize(
|
105
|
-
@
|
106
|
-
@set
|
106
|
+
def initialize(**options)
|
107
|
+
@scope = options[:scope] || Error::DEFAULT_SCOPE
|
108
|
+
@set = Set.new options[:errors].to_a
|
107
109
|
end
|
108
110
|
end
|
109
111
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
RSpec.describe Tram::Policy::Error do
|
2
2
|
subject(:error) { described_class.new :bad, options }
|
3
3
|
|
4
|
-
let(:
|
4
|
+
let(:scope) { %w[tram-policy] }
|
5
|
+
let(:options) { { level: "warning", scope: scope } }
|
5
6
|
|
6
7
|
describe "#item" do
|
7
8
|
subject { error.item }
|
8
|
-
it { is_expected.to eq [:bad, level: "warning", scope:
|
9
|
+
it { is_expected.to eq [:bad, level: "warning", scope: scope] }
|
9
10
|
end
|
10
11
|
|
11
12
|
describe "#message" do
|
@@ -1,6 +1,6 @@
|
|
1
1
|
RSpec.describe Tram::Policy::Errors do
|
2
|
-
let(:
|
3
|
-
let(:errors) { described_class.new(
|
2
|
+
let(:scope) { %w[tram-policy] }
|
3
|
+
let(:errors) { described_class.new(scope: scope) }
|
4
4
|
|
5
5
|
describe ".new" do
|
6
6
|
subject { errors }
|
@@ -8,7 +8,7 @@ RSpec.describe Tram::Policy::Errors do
|
|
8
8
|
it { is_expected.to be_kind_of Enumerable }
|
9
9
|
it { is_expected.to respond_to :empty? }
|
10
10
|
it { is_expected.to be_empty }
|
11
|
-
its(:
|
11
|
+
its(:scope) { is_expected.to eql scope }
|
12
12
|
end
|
13
13
|
|
14
14
|
describe "#add" do
|
@@ -21,7 +21,7 @@ RSpec.describe Tram::Policy::Errors do
|
|
21
21
|
|
22
22
|
expect(error).to be_kind_of Tram::Policy::Error
|
23
23
|
expect(error)
|
24
|
-
.to eq [:omg, level: "info", field: "name", scope:
|
24
|
+
.to eq [:omg, level: "info", field: "name", scope: scope]
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -45,11 +45,11 @@ RSpec.describe Tram::Policy::Errors do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
describe "#merge" do
|
48
|
-
let(:other) { described_class.new(
|
48
|
+
let(:other) { described_class.new(scope: scope) }
|
49
49
|
|
50
50
|
before do
|
51
|
-
errors.add "D'OH!", level: "disaster"
|
52
|
-
other.add "OUCH!",
|
51
|
+
errors.add :"D'OH!", level: "disaster"
|
52
|
+
other.add "OUCH!", level: "error"
|
53
53
|
end
|
54
54
|
|
55
55
|
context "without a block:" do
|
@@ -58,8 +58,8 @@ RSpec.describe Tram::Policy::Errors do
|
|
58
58
|
it "merges other collection as is" do
|
59
59
|
expect(subject).to be_a Tram::Policy::Errors
|
60
60
|
expect(subject.items).to match_array [
|
61
|
-
["D'OH!", level: "disaster", scope:
|
62
|
-
["OUCH!", level: "error"
|
61
|
+
[:"D'OH!", level: "disaster", scope: scope],
|
62
|
+
["OUCH!", level: "error"]
|
63
63
|
]
|
64
64
|
end
|
65
65
|
end
|
@@ -70,8 +70,8 @@ RSpec.describe Tram::Policy::Errors do
|
|
70
70
|
it "merges filtered collection as is" do
|
71
71
|
expect(subject).to be_a Tram::Policy::Errors
|
72
72
|
expect(subject.items).to match_array [
|
73
|
-
["D'OH!", level: "disaster", scope:
|
74
|
-
["OUCH!", level: "error",
|
73
|
+
[:"D'OH!", level: "disaster", scope: scope],
|
74
|
+
["OUCH!", level: "error", source: "Homer"]
|
75
75
|
]
|
76
76
|
end
|
77
77
|
end
|
@@ -82,8 +82,8 @@ RSpec.describe Tram::Policy::Errors do
|
|
82
82
|
it "merges other collection with given options" do
|
83
83
|
expect(subject).to be_a Tram::Policy::Errors
|
84
84
|
expect(subject.items).to match_array [
|
85
|
-
["D'OH!", level: "disaster", scope:
|
86
|
-
["OUCH!", level: "error",
|
85
|
+
[:"D'OH!", level: "disaster", scope: scope],
|
86
|
+
["OUCH!", level: "error", source: "Homer"]
|
87
87
|
]
|
88
88
|
end
|
89
89
|
end
|
@@ -94,8 +94,8 @@ RSpec.describe Tram::Policy::Errors do
|
|
94
94
|
it "merges filtered collection with given options" do
|
95
95
|
expect(subject).to be_a Tram::Policy::Errors
|
96
96
|
expect(subject.items).to match_array [
|
97
|
-
["D'OH!", level: "disaster", scope:
|
98
|
-
["OUCH!", level: "error",
|
97
|
+
[:"D'OH!", level: "disaster", scope: scope],
|
98
|
+
["OUCH!", level: "error", id: 5, age: 4]
|
99
99
|
]
|
100
100
|
end
|
101
101
|
end
|
@@ -125,8 +125,8 @@ RSpec.describe Tram::Policy::Errors do
|
|
125
125
|
|
126
126
|
it "returns selected errors only" do
|
127
127
|
expect(subject).to match_array [
|
128
|
-
[:foo, field: "name", level: "error", scope:
|
129
|
-
[:foo, field: "email", level: "error", scope:
|
128
|
+
[:foo, field: "name", level: "error", scope: scope],
|
129
|
+
[:foo, field: "email", level: "error", scope: scope]
|
130
130
|
]
|
131
131
|
end
|
132
132
|
end
|
data/spec/tram/policy_spec.rb
CHANGED
@@ -63,8 +63,8 @@ RSpec.describe Tram::Policy do
|
|
63
63
|
describe "#errors" do
|
64
64
|
subject { policy.errors }
|
65
65
|
|
66
|
-
its(:class)
|
67
|
-
its(:
|
66
|
+
its(:class) { is_expected.to eq Tram::Policy::Errors }
|
67
|
+
its(:scope) { is_expected.to eql policy.scope }
|
68
68
|
end
|
69
69
|
|
70
70
|
describe "#valid?" do
|
data/tram-policy.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = "tram-policy"
|
3
|
-
gem.version = "
|
3
|
+
gem.version = "2.0.0"
|
4
4
|
gem.author = ["Viktor Sokolov (gzigzigzeo)", "Andrew Kozin (nepalez)"]
|
5
5
|
gem.email = "andrew.kozin@gmail.com"
|
6
6
|
gem.homepage = "https://github.com/tram/tram-policy"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tram-policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viktor Sokolov (gzigzigzeo)
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-07-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dry-initializer
|