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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d9eceeea7e032619394e2a9c57b3a780031e6716e99115ab4d7f70aaa215730
4
- data.tar.gz: 39fa500a2789eb528aac666711652ee0c7b9ed3a3a7d9b197333b133e94b17a3
3
+ metadata.gz: d2d94489718b8070f78e8a4792e67902336e87d03da02d216fe49069767684fb
4
+ data.tar.gz: 4c9e417c8a39f18e0faf75c2b7bae22f7d3aa0e53c2c9486e068910f59837f2f
5
5
  SHA512:
6
- metadata.gz: 0654b385b2dc583a467824fc473e9873408297795fc172ed70313d25ef9a352d34802640e10a8f68e861122dedbc6146678d225c4fff2d60510901a62f30d2e9
7
- data.tar.gz: c45529180fc6e8d99ab3b071ac3fc886b41bc84bf0b57769ad83ccbe046b0b03ea03d026fc974e6acd842c6e2807219707ea83cfbd258f1b27ea078465d52ba1
6
+ metadata.gz: 71862e68041ee87d414f60bcf79c5f84f4996db5bc1ddc143628598dc29f0ca4a9816c7669b63a1c10e9e737e371800ff9ad1b214ed814753ce4c393c3877165
7
+ data.tar.gz: 6278ffc6803fdb5ab6f96bc4c4d2a38ad7758e2eb723fca763234aa647e2ba6b1854220059d2ce0cc182215042b2926080ad39f39eb815614610850d12e2d72c
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  /*.gem
11
11
  .rspec_status
12
+ .idea/
@@ -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
@@ -40,7 +40,7 @@ module Tram
40
40
  # @return [Tram::Policy::Errors]
41
41
  #
42
42
  def errors
43
- @errors ||= Errors.new(self)
43
+ @errors ||= Errors.new(scope: scope)
44
44
  end
45
45
 
46
46
  # The array of error items for lazy translation
@@ -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>] opts
15
+ # @param [Hash<Symbol, Object>] tags
16
16
  # @return [Tram::Policy::Error]
17
17
  #
18
- def self.new(value, **opts)
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
- # The list of arguments for [I18n.t]
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
- # The text of error message translated to the current locale
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 = Dry::Initializer::UNDEFINED, &block)
64
- if default == Dry::Initializer::UNDEFINED
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?(*)
@@ -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] policy
12
- #
13
- # @return [Tram::Policy] the poplicy errors provided by
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
- tap { @set << Tram::Policy::Error.new(message, **tags) }
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(policy, list)
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(policy, errors = [])
105
- @policy = policy
106
- @set = Set.new(errors)
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(:options) { { level: "warning", scope: %w[tram-policy] } }
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: %w[tram-policy]] }
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(:policy) { double :policy, scope: %w[tram-policy] }
3
- let(:errors) { described_class.new(policy) }
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(:policy) { is_expected.to eql policy }
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: %w[tram-policy]]
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(policy) }
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!", level: "error"
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: %w[tram-policy]],
62
- ["OUCH!", level: "error", scope: %w[tram-policy]]
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: %w[tram-policy]],
74
- ["OUCH!", level: "error", scope: %w[tram-policy], source: "Homer"]
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: %w[tram-policy]],
86
- ["OUCH!", level: "error", scope: %w[tram-policy], source: "Homer"]
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: %w[tram-policy]],
98
- ["OUCH!", level: "error", scope: %w[tram-policy], id: 5, age: 4]
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: %w[tram-policy]],
129
- [:foo, field: "email", level: "error", scope: %w[tram-policy]]
128
+ [:foo, field: "name", level: "error", scope: scope],
129
+ [:foo, field: "email", level: "error", scope: scope]
130
130
  ]
131
131
  end
132
132
  end
@@ -63,8 +63,8 @@ RSpec.describe Tram::Policy do
63
63
  describe "#errors" do
64
64
  subject { policy.errors }
65
65
 
66
- its(:class) { is_expected.to eq Tram::Policy::Errors }
67
- its(:policy) { is_expected.to eql policy }
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "tram-policy"
3
- gem.version = "1.0.1"
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: 1.0.1
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-05-06 00:00:00.000000000 Z
12
+ date: 2019-07-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-initializer