tram-policy 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cb9b1d95aea1a9a93919d38150f6c65900e8247
4
- data.tar.gz: 686439c0267b6637fdcc888f1be4aef673a4ef89
3
+ metadata.gz: 8e014f25ceab7b1bdbea2a67a4ba98245986689c
4
+ data.tar.gz: 0ed033ca9a5e8a695e78dc8b89e9d927a5373bcb
5
5
  SHA512:
6
- metadata.gz: d3850a47c670108dba1c7c112aff4197bcf85a7ba41c1f4eac975c6e008e218e8bd4c4fa3eb3e871eaa9ab0e1604aed6a3484865104e21c357d993e813f89190
7
- data.tar.gz: 1d3a178078a6c88da436c97a6f6cd4cf096947732930840d336b8736e4f35529ec8bea37aa0534ed0517e5ea7d69fa2857f459b35ab7fce3f44b0fdff6bba631
6
+ metadata.gz: 361c6c5af4c893f09d3595eb22ecea7098134c1ddc5ae2854ba9177a35f5a89ea3e6d27142e92f0e66d89b0aa8d17a7373671dc2fbcb5d51e22407a61b33d587
7
+ data.tar.gz: a899980e7b03a31998d1e183026d9ad675aa865445f6c28392c3ee13bdd23de9e09b2e90f3cb0dd77f572931605e8fe436f372b71b086a4a7464ac4b6c91e6da
data/CHANGELOG.md CHANGED
@@ -4,6 +4,14 @@ 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
+ ## [0.1.1] - [2017-08-04]
8
+
9
+ ### Added
10
+ - Support for options in errors.merge (@nepalez)
11
+
12
+ # adds `field: "user"` to every merged error
13
+ errors.merge other_policy.errors, field: "user"
14
+
7
15
  ## [0.1.0] - [2017-05-31]
8
16
  Contains backward-incompatible change.
9
17
 
@@ -52,4 +60,6 @@ This is a first public release (@nepalez, @charlie-wasp, @JewelSam, @sergey-chec
52
60
  [Unreleased]: https://github.com/tram-rb/tram-policy
53
61
  [0.0.1]: https://github.com/tram-rb/tram-policy/releases/tag/v0.0.1
54
62
  [0.0.2]: https://github.com/tram-rb/tram-policy/compare/v0.0.1...v0.0.2
55
- [0.0.3]: https://github.com/tram-rb/tram-policy/compare/v0.0.2...v0.0.3
63
+ [0.0.3]: https://github.com/tram-rb/tram-policy/compare/v0.0.2...v0.0.3
64
+ [0.1.0]: https://github.com/tram-rb/tram-policy/compare/v0.0.3...v0.1.0
65
+ [0.1.1]: https://github.com/tram-rb/tram-policy/compare/v0.1.0...v0.1.1
@@ -80,13 +80,12 @@ class Tram::Policy
80
80
  # @example Add some tag to merged errors
81
81
  # policy.merge(other) { |err| err[:source] = "other" }
82
82
  #
83
- def merge(other)
84
- return self unless other.is_a?(self.class) && other.any?
83
+ def merge(other, **options)
84
+ return self unless other.is_a?(self.class)
85
85
 
86
- if block_given?
87
- other.each { |err| add yield(err.to_h) }
88
- else
89
- @set |= other.to_a
86
+ other.each do |err|
87
+ new_err = block_given? ? yield(err.to_h) : err.to_h
88
+ add new_err.merge(options)
90
89
  end
91
90
 
92
91
  self
@@ -0,0 +1,6 @@
1
+ require_relative "user_policy"
2
+
3
+ class Test::AdminPolicy < Test::UserPolicy
4
+ validate :login
5
+ validate :name
6
+ end
@@ -0,0 +1,12 @@
1
+ class Test::CustomerPolicy < Tram::Policy
2
+ option :name
3
+
4
+ validate :name_presence
5
+
6
+ private
7
+
8
+ def name_presence
9
+ return if name
10
+ errors.add :name_presence, field: "name"
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ en:
3
+ tram-policy:
4
+ test/customer_policy:
5
+ name_presence: Name is absent
6
+ test/user_policy:
7
+ name_presence: Name is absent
@@ -0,0 +1,21 @@
1
+ class Test::UserPolicy < Tram::Policy
2
+ param :user
3
+
4
+ validate :name
5
+ validate "email"
6
+ validate "name"
7
+
8
+ private
9
+
10
+ def name
11
+ errors.add "No name", level: "warning" unless user.name
12
+ end
13
+
14
+ def email
15
+ user.email
16
+ end
17
+
18
+ def login
19
+ user.login
20
+ end
21
+ end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,8 @@ require "tram/policy"
8
8
  require "tram/policy/rspec"
9
9
  require "rspec/its"
10
10
 
11
+ require_relative "support/fixtures_helper.rb"
12
+
11
13
  RSpec.configure do |config|
12
14
  config.example_status_persistence_file_path = ".rspec_status"
13
15
  config.expect_with :rspec do |c|
@@ -0,0 +1,11 @@
1
+ def fixture_file_path(filename)
2
+ File.expand_path "spec/fixtures/#{filename}"
3
+ end
4
+
5
+ def yaml_fixture_file(filename)
6
+ YAML.load_file(fixture_file_path(filename))
7
+ end
8
+
9
+ def load_fixture(filename)
10
+ load fixture_file_path(filename)
11
+ end
@@ -67,6 +67,30 @@ RSpec.describe Tram::Policy::Errors do
67
67
  end
68
68
  end
69
69
 
70
+ context "with options:" do
71
+ subject { errors.merge(other, source: "Homer") }
72
+
73
+ it "merges other collection with given options" do
74
+ expect(subject).to be_a Tram::Policy::Errors
75
+ expect(subject.map(&:to_h)).to match_array [
76
+ { message: "OMG!", level: "disaster" },
77
+ { message: "OMG!", level: "error", source: "Homer" }
78
+ ]
79
+ end
80
+ end
81
+
82
+ context "with block and options:" do
83
+ subject { errors.merge(other, id: 5) { |err| err.merge id: 3, age: 4 } }
84
+
85
+ it "merges filtered collection with given options" do
86
+ expect(subject).to be_a Tram::Policy::Errors
87
+ expect(subject.map(&:to_h)).to match_array [
88
+ { message: "OMG!", level: "disaster" },
89
+ { message: "OMG!", level: "error", id: 5, age: 4 }
90
+ ]
91
+ end
92
+ end
93
+
70
94
  context "not errors:" do
71
95
  subject { errors.merge 1 }
72
96
  it { is_expected.to eql errors }
@@ -1,25 +1,12 @@
1
1
  RSpec.describe "RSpec support:" do
2
- subject { Test::UserPolicy[name: nil] }
3
-
4
2
  before do
5
3
  I18n.available_locales = %i[en]
6
- I18n.backend.store_translations \
7
- :en, { "test/user_policy" => { "name_presence" => "Name is absent" } }
8
-
9
- class Test::UserPolicy < Tram::Policy
10
- option :name
11
-
12
- validate :name_presence
13
-
14
- private
15
-
16
- def name_presence
17
- return if name
18
- errors.add :name_presence, field: "name"
19
- end
20
- end
4
+ I18n.backend.store_translations :en, yaml_fixture_file("en.yml")["en"]
5
+ load_fixture "customer_policy.rb"
21
6
  end
22
7
 
8
+ subject { Test::CustomerPolicy[name: nil] }
9
+
23
10
  describe "to be_invalid_at" do
24
11
  it "passes when some translated error present w/o tags constraint" do
25
12
  expect do
@@ -1,38 +1,10 @@
1
1
  RSpec.describe Tram::Policy do
2
2
  before do
3
3
  I18n.available_locales = %w[en]
4
- I18n.backend.store_translations :en, {
5
- "tram-policy" => {
6
- "test/user_policy" => { "name_presence" => "Name is absent" }
7
- }
8
- }
9
-
10
- class Test::UserPolicy < Tram::Policy
11
- param :user
12
-
13
- validate :name
14
- validate "email"
15
- validate "name"
16
-
17
- private
18
-
19
- def name
20
- errors.add "No name", level: "warning" unless user.name
21
- end
4
+ I18n.backend.store_translations :en, yaml_fixture_file("en.yml")["en"]
22
5
 
23
- def email
24
- user.email
25
- end
26
-
27
- def login
28
- user.login
29
- end
30
- end
31
-
32
- class Test::AdminPolicy < Test::UserPolicy
33
- validate :login
34
- validate :name
35
- end
6
+ load_fixture "user_policy.rb"
7
+ load_fixture "admin_policy.rb"
36
8
  end
37
9
 
38
10
  let(:policy) { Test::UserPolicy[user] }
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 = "0.1.0"
3
+ gem.version = "0.1.1"
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: 0.1.0
4
+ version: 0.1.1
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: 2017-05-31 00:00:00.000000000 Z
12
+ date: 2017-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-initializer
@@ -141,7 +141,12 @@ files:
141
141
  - lib/tram/policy/rspec.rb
142
142
  - lib/tram/policy/validation_error.rb
143
143
  - lib/tram/policy/validator.rb
144
+ - spec/fixtures/admin_policy.rb
145
+ - spec/fixtures/customer_policy.rb
146
+ - spec/fixtures/en.yml
147
+ - spec/fixtures/user_policy.rb
144
148
  - spec/spec_helper.rb
149
+ - spec/support/fixtures_helper.rb
145
150
  - spec/tram/policy/error_spec.rb
146
151
  - spec/tram/policy/errors_spec.rb
147
152
  - spec/tram/policy/inflector_spec.rb
@@ -174,10 +179,16 @@ signing_key:
174
179
  specification_version: 4
175
180
  summary: Policy Object Pattern
176
181
  test_files:
182
+ - spec/fixtures/admin_policy.rb
183
+ - spec/fixtures/customer_policy.rb
184
+ - spec/fixtures/en.yml
185
+ - spec/fixtures/user_policy.rb
177
186
  - spec/spec_helper.rb
187
+ - spec/support/fixtures_helper.rb
178
188
  - spec/tram/policy/error_spec.rb
179
189
  - spec/tram/policy/errors_spec.rb
180
190
  - spec/tram/policy/inflector_spec.rb
181
191
  - spec/tram/policy/rspec_spec.rb
182
192
  - spec/tram/policy/validation_error_spec.rb
183
193
  - spec/tram/policy_spec.rb
194
+ has_rdoc: