tram-policy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +15 -0
- data/.gitignore +11 -0
- data/.rspec +4 -0
- data/.rubocop.yml +22 -0
- data/.travis.yml +27 -0
- data/CHANGELOG.md +13 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +373 -0
- data/Rakefile +7 -0
- data/bin/tram-policy +4 -0
- data/lib/tram-policy.rb +1 -0
- data/lib/tram/policy.rb +113 -0
- data/lib/tram/policy/error.rb +88 -0
- data/lib/tram/policy/errors.rb +102 -0
- data/lib/tram/policy/generator.rb +111 -0
- data/lib/tram/policy/generator/policy.erb +20 -0
- data/lib/tram/policy/generator/policy_spec.erb +17 -0
- data/lib/tram/policy/inflector.rb +26 -0
- data/lib/tram/policy/matchers.rb +112 -0
- data/lib/tram/policy/validation_error.rb +18 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/tram/policy/error_spec.rb +61 -0
- data/spec/tram/policy/errors_spec.rb +112 -0
- data/spec/tram/policy/inflector_spec.rb +14 -0
- data/spec/tram/policy/matchers_spec.rb +70 -0
- data/spec/tram/policy/validation_error_spec.rb +23 -0
- data/spec/tram/policy_spec.rb +173 -0
- data/tram-policy.gemspec +25 -0
- metadata +182 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
RSpec.describe Tram::Policy::ValidationError do
|
2
|
+
subject(:error) { described_class.new policy, filter }
|
3
|
+
|
4
|
+
let(:one) { double full_message: "OMG!", level: "error" }
|
5
|
+
let(:two) { double full_message: "phew!", level: "warning" }
|
6
|
+
let(:policy) { double :policy, errors: [one, two] }
|
7
|
+
|
8
|
+
shared_examples :exception_with_messages do |text|
|
9
|
+
it { is_expected.to be_a RuntimeError }
|
10
|
+
its(:policy) { is_expected.to eq policy }
|
11
|
+
its(:message) { is_expected.to eq "Validation failed with errors:#{text}" }
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with a liberal filter" do
|
15
|
+
let(:filter) { proc { false } }
|
16
|
+
it_behaves_like :exception_with_messages, "\n- OMG!\n- phew!"
|
17
|
+
end
|
18
|
+
|
19
|
+
context "with a restricting filter" do
|
20
|
+
let(:filter) { proc { |error| error.level != "error" } }
|
21
|
+
it_behaves_like :exception_with_messages, "\n- OMG!"
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
RSpec.describe Tram::Policy do
|
2
|
+
before do
|
3
|
+
I18n.available_locales = %w[en]
|
4
|
+
|
5
|
+
class Test::UserPolicy < Tram::Policy
|
6
|
+
param :user
|
7
|
+
|
8
|
+
validate :name, "email", %w[email name]
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def name
|
13
|
+
errors.add "No name", level: "warning" unless user.name
|
14
|
+
end
|
15
|
+
|
16
|
+
def email
|
17
|
+
user.email
|
18
|
+
end
|
19
|
+
|
20
|
+
def login
|
21
|
+
user.login
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Test::AdminPolicy < Test::UserPolicy
|
26
|
+
validate :login
|
27
|
+
validate :name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:policy) { Test::UserPolicy[user] }
|
32
|
+
let(:user) { double :user, name: name, email: nil, login: nil }
|
33
|
+
let(:name) { nil }
|
34
|
+
|
35
|
+
describe "Dry::Initializer interface" do
|
36
|
+
it "is accessible" do
|
37
|
+
expect(described_class.ancestors).to include Dry::Initializer
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe ".validate" do
|
42
|
+
it "defines validators to be called by initializer in proper order" do
|
43
|
+
expect(user).to receive(:name).once.ordered
|
44
|
+
expect(user).to receive(:email).once.ordered
|
45
|
+
|
46
|
+
Test::UserPolicy.new(user)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "preserves order of parent class validators" do
|
50
|
+
expect(user).to receive(:name).once.ordered
|
51
|
+
expect(user).to receive(:email).once.ordered
|
52
|
+
expect(user).to receive(:login).once.ordered
|
53
|
+
|
54
|
+
Test::AdminPolicy.new(user)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#inspect" do
|
59
|
+
subject { policy.inspect }
|
60
|
+
it { is_expected.to eq "#<Test::UserPolicy[{:user=>#<Double :user>}]>" }
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#errors" do
|
64
|
+
subject { policy.errors }
|
65
|
+
|
66
|
+
its(:class) { is_expected.to eq Tram::Policy::Errors }
|
67
|
+
its(:policy) { is_expected.to eql policy }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "#valid?" do
|
71
|
+
context "when #errors are present" do
|
72
|
+
subject { policy.valid? }
|
73
|
+
let(:name) { nil }
|
74
|
+
|
75
|
+
it { is_expected.to eq false }
|
76
|
+
end
|
77
|
+
|
78
|
+
context "with a filter" do
|
79
|
+
subject { policy.valid? { |err| err.level != "error" } }
|
80
|
+
let(:name) { nil }
|
81
|
+
|
82
|
+
it "takes into account filtered errors" do
|
83
|
+
expect(subject).to eq true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when #errors are absent" do
|
88
|
+
subject { policy.valid? }
|
89
|
+
let(:name) { :foo }
|
90
|
+
|
91
|
+
it { is_expected.to eq true }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "#invalid?" do
|
96
|
+
context "when #errors are present" do
|
97
|
+
subject { policy.invalid? }
|
98
|
+
let(:name) { nil }
|
99
|
+
|
100
|
+
it { is_expected.to eq true }
|
101
|
+
end
|
102
|
+
|
103
|
+
context "with a filter" do
|
104
|
+
subject { policy.invalid? { |err| err.level == "error" } }
|
105
|
+
let(:name) { nil }
|
106
|
+
|
107
|
+
it "filters errors out" do
|
108
|
+
expect(subject).to eq false
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "when #errors are absent" do
|
113
|
+
subject { policy.invalid? }
|
114
|
+
let(:name) { :foo }
|
115
|
+
|
116
|
+
it { is_expected.to eq false }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "#validate!" do
|
121
|
+
context "when #errors are present" do
|
122
|
+
subject { policy.validate! }
|
123
|
+
let(:name) { nil }
|
124
|
+
|
125
|
+
it "raises an exception" do
|
126
|
+
expect { subject }.to raise_error Tram::Policy::ValidationError
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context "with a filter" do
|
131
|
+
subject { policy.validate! { |err| err.level != "error" } }
|
132
|
+
let(:name) { nil }
|
133
|
+
|
134
|
+
it "takes into account filtered errors" do
|
135
|
+
expect { subject }.not_to raise_error
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "when #errors are absent" do
|
140
|
+
subject { policy.validate! }
|
141
|
+
let(:name) { :foo }
|
142
|
+
|
143
|
+
it "doesn't raise an exception" do
|
144
|
+
expect { subject }.not_to raise_error
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "#t" do
|
150
|
+
subject { policy.t(value, level: "error") }
|
151
|
+
|
152
|
+
before do
|
153
|
+
I18n.backend.store_translations :en, {
|
154
|
+
"test/user_policy" => { "name_presence" => "%{level}: Name is absent" }
|
155
|
+
}
|
156
|
+
end
|
157
|
+
|
158
|
+
context "string" do
|
159
|
+
let(:value) { "Name should be present" }
|
160
|
+
it { is_expected.to eq value }
|
161
|
+
end
|
162
|
+
|
163
|
+
context "non-symbol" do
|
164
|
+
let(:value) { 42 }
|
165
|
+
it { is_expected.to eq "42" }
|
166
|
+
end
|
167
|
+
|
168
|
+
context "symbol" do
|
169
|
+
let(:value) { :name_presence }
|
170
|
+
it { is_expected.to eq "error: Name is absent" }
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/tram-policy.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "tram-policy"
|
3
|
+
gem.version = "0.0.1"
|
4
|
+
gem.author = ["Viktor Sokolov (gzigzigzeo)", "Andrew Kozin (nepalez)"]
|
5
|
+
gem.email = ["andrew.kozin@gmail.com"]
|
6
|
+
gem.homepage = "https://github.com/tram/tram-policy"
|
7
|
+
gem.summary = "Policy Object Pattern"
|
8
|
+
gem.license = "MIT"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
11
|
+
gem.test_files = gem.files.grep(/^spec/)
|
12
|
+
gem.extra_rdoc_files = Dir["README.md", "LICENSE", "CHANGELOG.md"]
|
13
|
+
gem.executables = %w[tram-policy]
|
14
|
+
|
15
|
+
gem.required_ruby_version = ">= 2.2"
|
16
|
+
|
17
|
+
gem.add_runtime_dependency "dry-initializer", "~> 1.4"
|
18
|
+
gem.add_runtime_dependency "i18n", "~> 0.8"
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake", "> 10"
|
21
|
+
gem.add_development_dependency "rspec", "~> 3.0"
|
22
|
+
gem.add_development_dependency "rspec-its", "~> 1.2"
|
23
|
+
gem.add_development_dependency "rubocop", "~> 0.42"
|
24
|
+
gem.add_development_dependency "thor", "~> 0.19"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tram-policy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Viktor Sokolov (gzigzigzeo)
|
8
|
+
- Andrew Kozin (nepalez)
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-04-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: dry-initializer
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.4'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.4'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: i18n
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0.8'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0.8'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '10'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '10'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3.0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec-its
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '1.2'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '1.2'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rubocop
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0.42'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0.42'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: thor
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - "~>"
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0.19'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - "~>"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0.19'
|
112
|
+
description:
|
113
|
+
email:
|
114
|
+
- andrew.kozin@gmail.com
|
115
|
+
executables:
|
116
|
+
- tram-policy
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files:
|
119
|
+
- README.md
|
120
|
+
- CHANGELOG.md
|
121
|
+
files:
|
122
|
+
- ".codeclimate.yml"
|
123
|
+
- ".gitignore"
|
124
|
+
- ".rspec"
|
125
|
+
- ".rubocop.yml"
|
126
|
+
- ".travis.yml"
|
127
|
+
- CHANGELOG.md
|
128
|
+
- Gemfile
|
129
|
+
- LICENSE.txt
|
130
|
+
- README.md
|
131
|
+
- Rakefile
|
132
|
+
- bin/tram-policy
|
133
|
+
- lib/tram-policy.rb
|
134
|
+
- lib/tram/policy.rb
|
135
|
+
- lib/tram/policy/error.rb
|
136
|
+
- lib/tram/policy/errors.rb
|
137
|
+
- lib/tram/policy/generator.rb
|
138
|
+
- lib/tram/policy/generator/policy.erb
|
139
|
+
- lib/tram/policy/generator/policy_spec.erb
|
140
|
+
- lib/tram/policy/inflector.rb
|
141
|
+
- lib/tram/policy/matchers.rb
|
142
|
+
- lib/tram/policy/validation_error.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
- spec/tram/policy/error_spec.rb
|
145
|
+
- spec/tram/policy/errors_spec.rb
|
146
|
+
- spec/tram/policy/inflector_spec.rb
|
147
|
+
- spec/tram/policy/matchers_spec.rb
|
148
|
+
- spec/tram/policy/validation_error_spec.rb
|
149
|
+
- spec/tram/policy_spec.rb
|
150
|
+
- tram-policy.gemspec
|
151
|
+
homepage: https://github.com/tram/tram-policy
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata: {}
|
155
|
+
post_install_message:
|
156
|
+
rdoc_options: []
|
157
|
+
require_paths:
|
158
|
+
- lib
|
159
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '2.2'
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 2.5.2
|
172
|
+
signing_key:
|
173
|
+
specification_version: 4
|
174
|
+
summary: Policy Object Pattern
|
175
|
+
test_files:
|
176
|
+
- spec/spec_helper.rb
|
177
|
+
- spec/tram/policy/error_spec.rb
|
178
|
+
- spec/tram/policy/errors_spec.rb
|
179
|
+
- spec/tram/policy/inflector_spec.rb
|
180
|
+
- spec/tram/policy/matchers_spec.rb
|
181
|
+
- spec/tram/policy/validation_error_spec.rb
|
182
|
+
- spec/tram/policy_spec.rb
|