attestor 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -8
- data/lib/attestor/policy.rb +21 -0
- data/lib/attestor/version.rb +1 -1
- data/spec/tests/policy_spec.rb +23 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbf1e027054a7c3baeeaa39f6e66f515e1b3fa2e
|
4
|
+
data.tar.gz: 546765bd0a8dfee528fa5ed45f3fe7a3b07047cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfa470608fa7882248c256abd5e594a7ed4d50cef97fbc230425a89703b1cd6c652586b57a760a76b78b9611a76ad41f0f58eb2573e2e3f50c88f2aabfd903df
|
7
|
+
data.tar.gz: 5694b759f83f0b281fc102753dce9537512510157e79a22941fd312d5ed141b7e62e7b1b6072d54035b43995e735b74ce746109acc43c9c5a6197b4553d2ea3c
|
data/README.md
CHANGED
@@ -157,15 +157,11 @@ Policy Objects
|
|
157
157
|
|
158
158
|
Extract a validator to the separate object (policy). Basically the policy includes `Attestor::Validations` with additional methods.
|
159
159
|
|
160
|
-
|
161
|
-
class ConsistencyPolicy < Struct.new(:debet, :credit)
|
162
|
-
include Attestor::Policy # includes Attestor::Validator as well
|
163
|
-
|
164
|
-
validate :consistent
|
160
|
+
To create a policy as a `Struct` use the builder method:
|
165
161
|
|
166
|
-
|
167
|
-
|
168
|
-
def
|
162
|
+
```ruby
|
163
|
+
ConsistencyPolicy = Attestor::Policy.new(:debet, :credit) do
|
164
|
+
def validate
|
169
165
|
fraud = credit - debet
|
170
166
|
invalid :inconsistent, fraud: fraud if fraud != 0
|
171
167
|
end
|
data/lib/attestor/policy.rb
CHANGED
@@ -16,6 +16,27 @@ module Attestor
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
# Builds the policy class
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# MyPolicy = Attestor::Policy.new(:foo, :bar) do
|
23
|
+
# attr_reader :baz
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @attribute [Array<#to_sym>] attributes
|
27
|
+
# the list of attributes
|
28
|
+
# @attribute [Proc] block
|
29
|
+
#
|
30
|
+
# @yield the block in the scope of created class
|
31
|
+
#
|
32
|
+
# @return [Class]
|
33
|
+
def self.new(*attributes, &block)
|
34
|
+
Struct.new(*attributes) do
|
35
|
+
include Attestor::Policy
|
36
|
+
instance_eval(&block) if block_given?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
19
40
|
# Checks whether the policy is valid
|
20
41
|
#
|
21
42
|
# @return [Boolean]
|
data/lib/attestor/version.rb
CHANGED
data/spec/tests/policy_spec.rb
CHANGED
@@ -18,6 +18,29 @@ describe Attestor::Policy do
|
|
18
18
|
|
19
19
|
describe ".new" do
|
20
20
|
|
21
|
+
let(:build) { described_class.new(:foo) { attr_reader :bar } }
|
22
|
+
subject { build.new(:baz) }
|
23
|
+
|
24
|
+
it "builds the struct" do
|
25
|
+
expect(subject).to be_kind_of Struct
|
26
|
+
end
|
27
|
+
|
28
|
+
it "adds given attributes" do
|
29
|
+
expect(subject.foo).to eq :baz
|
30
|
+
end
|
31
|
+
|
32
|
+
it "builds the policy" do
|
33
|
+
expect(subject).to be_kind_of described_class
|
34
|
+
end
|
35
|
+
|
36
|
+
it "yields the block in class scope" do
|
37
|
+
expect(subject).to respond_to :bar
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
describe ".included" do
|
43
|
+
|
21
44
|
it "creates a validator" do
|
22
45
|
expect(subject).to be_kind_of validator
|
23
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attestor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: extlib
|