policy 1.2.0 → 2.0.0
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 +4 -4
- data/Guardfile +4 -4
- data/README.md +177 -78
- data/config/metrics/flay.yml +1 -1
- data/config/metrics/roodi.yml +2 -2
- data/lib/policy.rb +52 -33
- data/lib/policy/base.rb +122 -0
- data/lib/policy/base/and.rb +38 -0
- data/lib/policy/base/negator.rb +52 -0
- data/lib/policy/base/node.rb +59 -0
- data/lib/policy/base/not.rb +42 -0
- data/lib/policy/base/or.rb +39 -0
- data/lib/policy/base/xor.rb +39 -0
- data/lib/policy/cli.rb +8 -3
- data/lib/policy/cli/attribute.rb +49 -0
- data/lib/policy/cli/locale.erb +1 -2
- data/lib/policy/cli/policy.erb +33 -6
- data/lib/policy/cli/spec.erb +31 -11
- data/lib/policy/follower.rb +54 -94
- data/lib/policy/follower/name_error.rb +53 -0
- data/lib/policy/follower/policies.rb +104 -0
- data/lib/policy/follower/violation_error.rb +60 -0
- data/lib/policy/version.rb +2 -2
- data/policy.gemspec +2 -3
- data/spec/support/composer.rb +28 -0
- data/spec/tests/lib/policy/base/and_spec.rb +62 -0
- data/spec/tests/lib/policy/base/negator_spec.rb +49 -0
- data/spec/tests/lib/policy/base/not_spec.rb +50 -0
- data/spec/tests/lib/policy/base/or_spec.rb +62 -0
- data/spec/tests/lib/policy/base/xor_spec.rb +73 -0
- data/spec/tests/lib/policy/base_spec.rb +123 -0
- data/spec/tests/lib/policy/cli/attribute_spec.rb +52 -0
- data/spec/tests/{policy → lib/policy}/cli_spec.rb +25 -24
- data/spec/tests/lib/policy/follower/name_error_spec.rb +51 -0
- data/spec/tests/lib/policy/follower/policies_spec.rb +156 -0
- data/spec/tests/lib/policy/follower/violation_error_spec.rb +60 -0
- data/spec/tests/lib/policy/follower_spec.rb +153 -0
- data/spec/tests/lib/policy_spec.rb +52 -0
- metadata +43 -44
- data/lib/policy/follower/followed_policies.rb +0 -45
- data/lib/policy/follower/followed_policy.rb +0 -104
- data/lib/policy/follower/names.rb +0 -29
- data/lib/policy/interface.rb +0 -48
- data/lib/policy/validations.rb +0 -28
- data/lib/policy/violation_error.rb +0 -52
- data/spec/features/follower_spec.rb +0 -95
- data/spec/tests/policy/follower/followed_policies_spec.rb +0 -87
- data/spec/tests/policy/follower/followed_policy_spec.rb +0 -117
- data/spec/tests/policy/follower/names_spec.rb +0 -19
- data/spec/tests/policy/follower_spec.rb +0 -220
- data/spec/tests/policy/interface_spec.rb +0 -83
- data/spec/tests/policy/validations_spec.rb +0 -13
- data/spec/tests/policy/violation_error_spec.rb +0 -75
- data/spec/tests/policy_spec.rb +0 -35
@@ -1,83 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Policy::Interface do
|
4
|
-
|
5
|
-
before { Test = Class.new.send :include, described_class }
|
6
|
-
after { Object.send :remove_const, :Test }
|
7
|
-
|
8
|
-
let(:test_class) { Test }
|
9
|
-
subject { test_class.new }
|
10
|
-
|
11
|
-
it "includes Policy::Validations" do
|
12
|
-
expect(test_class).to include Policy::Validations
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "#apply" do
|
16
|
-
|
17
|
-
context "when #valid? returns true" do
|
18
|
-
|
19
|
-
before { allow(subject).to receive(:valid?).and_return true }
|
20
|
-
|
21
|
-
it "doesn't raise error" do
|
22
|
-
expect { subject.apply }.not_to raise_error
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context "when #valid? returns false" do
|
27
|
-
|
28
|
-
before { allow(subject).to receive(:valid?).and_return false }
|
29
|
-
|
30
|
-
it "raises ViolationError" do
|
31
|
-
expect { subject.apply }.to raise_error(Policy::ViolationError)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "adds the policy to Exception" do
|
35
|
-
expect(Policy::ViolationError).to receive(:new).with(subject).once
|
36
|
-
subject.apply rescue nil
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end # describe #apply
|
41
|
-
|
42
|
-
describe "#messages" do
|
43
|
-
|
44
|
-
context "when #errors are present" do
|
45
|
-
|
46
|
-
let(:messages) { %w(foo bar) }
|
47
|
-
let(:errors) { double :errors, messages: { foo: messages } }
|
48
|
-
|
49
|
-
it "extracts a plain array of error messages" do
|
50
|
-
allow(subject).to receive(:errors) { errors }
|
51
|
-
expect(subject.messages).to eq messages
|
52
|
-
end
|
53
|
-
|
54
|
-
end # context
|
55
|
-
|
56
|
-
context "when #errors are absent" do
|
57
|
-
|
58
|
-
it "returns an empty array" do
|
59
|
-
expect(subject.messages).to eq []
|
60
|
-
end
|
61
|
-
|
62
|
-
end # context
|
63
|
-
|
64
|
-
end # describe #messages
|
65
|
-
|
66
|
-
describe ".apply" do
|
67
|
-
|
68
|
-
let(:attributes) { %i(foo bar) }
|
69
|
-
let(:follower) { double apply: nil }
|
70
|
-
|
71
|
-
it "creates a policy object with the attributes" do
|
72
|
-
expect(test_class).to receive(:new).with(attributes) { follower }
|
73
|
-
test_class.apply attributes
|
74
|
-
end
|
75
|
-
|
76
|
-
it "validates the policy object" do
|
77
|
-
expect(test_class).to receive_message_chain :new, :apply
|
78
|
-
test_class.apply attributes
|
79
|
-
end
|
80
|
-
|
81
|
-
end # describe .apply
|
82
|
-
|
83
|
-
end # describe Policy::Inteface
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Policy::Validations do
|
4
|
-
|
5
|
-
let(:validations) { ActiveModel::Validations }
|
6
|
-
let(:methods) { validations.public_instance_methods }
|
7
|
-
let(:test_class) { Class.new.send :include, described_class }
|
8
|
-
|
9
|
-
it "includes ActiveModel::Validatons" do
|
10
|
-
expect(test_class).to include validations
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require "ostruct"
|
3
|
-
|
4
|
-
describe Policy::ViolationError do
|
5
|
-
|
6
|
-
# OpenStruct is used instead of double because it should be frozen
|
7
|
-
let(:policy) { OpenStruct.new(messages: ["foo"]) }
|
8
|
-
|
9
|
-
subject { described_class.new policy }
|
10
|
-
|
11
|
-
describe ".new" do
|
12
|
-
|
13
|
-
it "takes a policy" do
|
14
|
-
expect { described_class.new }.to raise_error ArgumentError
|
15
|
-
expect { subject }.not_to raise_error
|
16
|
-
end
|
17
|
-
|
18
|
-
it "is a RuntimeError" do
|
19
|
-
expect(subject).to be_kind_of RuntimeError
|
20
|
-
end
|
21
|
-
|
22
|
-
it "is immutable" do
|
23
|
-
expect(subject).to be_frozen
|
24
|
-
end
|
25
|
-
|
26
|
-
end # describe .new
|
27
|
-
|
28
|
-
describe "#policy" do
|
29
|
-
|
30
|
-
it "returns the policy" do
|
31
|
-
expect(subject.policy).to eq policy
|
32
|
-
end
|
33
|
-
|
34
|
-
it "is immutable" do
|
35
|
-
expect(subject.policy).to be_frozen
|
36
|
-
end
|
37
|
-
|
38
|
-
it "doesn't freeze the source policy object" do
|
39
|
-
subject
|
40
|
-
expect(policy).not_to be_frozen
|
41
|
-
end
|
42
|
-
|
43
|
-
end # describe #policy
|
44
|
-
|
45
|
-
describe "#messages" do
|
46
|
-
|
47
|
-
it "delegated to the policy" do
|
48
|
-
expect(subject.messages).to eq subject.policy.messages
|
49
|
-
end
|
50
|
-
|
51
|
-
it "is immutable" do
|
52
|
-
expect(subject.messages).to be_frozen
|
53
|
-
end
|
54
|
-
|
55
|
-
end # describe #messages
|
56
|
-
|
57
|
-
describe "#inspect" do
|
58
|
-
|
59
|
-
it "returns a proper text" do
|
60
|
-
expect(subject.inspect)
|
61
|
-
.to eq "#<Policy::ViolationError: #{ subject.message }>"
|
62
|
-
end
|
63
|
-
|
64
|
-
end # describe #inspect
|
65
|
-
|
66
|
-
describe "#message" do
|
67
|
-
|
68
|
-
it "returns a proper text" do
|
69
|
-
expect(subject.message)
|
70
|
-
.to eq "#{ policy.inspect } violated: #{ subject.messages }"
|
71
|
-
end
|
72
|
-
|
73
|
-
end # describe #message
|
74
|
-
|
75
|
-
end # describe Policy::ViolationError
|
data/spec/tests/policy_spec.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
describe Policy do
|
4
|
-
|
5
|
-
describe ".new" do
|
6
|
-
|
7
|
-
subject { described_class.new :debet, :credit }
|
8
|
-
|
9
|
-
it "builds the Struct" do
|
10
|
-
expect(subject.ancestors).to include Struct
|
11
|
-
end
|
12
|
-
|
13
|
-
it "adds required attributes" do
|
14
|
-
methods = subject.instance_methods
|
15
|
-
|
16
|
-
%i(debet debet= credit credit=).each do |method|
|
17
|
-
expect(methods).to include method
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
it "includes Policy::Interface" do
|
22
|
-
expect(subject).to include(Policy::Interface)
|
23
|
-
end
|
24
|
-
|
25
|
-
end # describe .new
|
26
|
-
|
27
|
-
describe ".name" do
|
28
|
-
|
29
|
-
subject { described_class.name }
|
30
|
-
|
31
|
-
it { is_expected.to eq "Policy" }
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
end # describe Policy
|