yema 0.0.1 → 0.0.2
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.
- data/README.md +9 -2
- data/lib/yema/rule.rb +1 -5
- data/lib/yema/validator.rb +8 -9
- data/lib/yema/version.rb +1 -1
- data/spec/unit/validator/errors_spec.rb +4 -4
- data/spec/unit/validator/valid_predicate_spec.rb +2 -2
- metadata +1 -1
data/README.md
CHANGED
@@ -19,7 +19,14 @@ Or install it yourself as:
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
-
|
22
|
+
Inline:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
validator = Yema::Validator.new(params, Yema::Rule::Required.new(:title))
|
26
|
+
validator.valid?
|
27
|
+
```
|
28
|
+
|
29
|
+
Class:
|
23
30
|
|
24
31
|
```ruby
|
25
32
|
class User
|
@@ -27,7 +34,7 @@ class User
|
|
27
34
|
attr_accessor :age
|
28
35
|
end
|
29
36
|
|
30
|
-
User.rules.add(Yema::Rule::StrongType, Integer)
|
37
|
+
User.rules.add(Yema::Rule::StrongType.new(:age), Integer)
|
31
38
|
|
32
39
|
user = User.new
|
33
40
|
user.age = 3
|
data/lib/yema/rule.rb
CHANGED
data/lib/yema/validator.rb
CHANGED
@@ -1,23 +1,22 @@
|
|
1
1
|
module Yema
|
2
2
|
class Validator
|
3
3
|
|
4
|
-
attr_reader :
|
4
|
+
attr_reader :resource, :rules
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
@
|
8
|
-
end
|
9
|
-
|
10
|
-
def value
|
11
|
-
resource.public_send(rule.attribute_name)
|
6
|
+
def initialize(resource, *rules)
|
7
|
+
@resource, @rules = resource, rules
|
12
8
|
end
|
13
9
|
|
14
10
|
def valid?
|
15
|
-
|
11
|
+
errors.empty?
|
16
12
|
end
|
17
13
|
|
18
14
|
def errors
|
19
15
|
errors = []
|
20
|
-
|
16
|
+
rules.each do |rule|
|
17
|
+
value = resource.public_send(rule.attribute_name)
|
18
|
+
errors << Error.new(rule, resource) unless rule.matches?(value)
|
19
|
+
end
|
21
20
|
errors.to_set
|
22
21
|
end
|
23
22
|
|
data/lib/yema/version.rb
CHANGED
@@ -4,12 +4,12 @@ describe Yema::Validator, "#errors" do
|
|
4
4
|
|
5
5
|
subject { validator.errors }
|
6
6
|
|
7
|
-
let(:validator) { described_class.new(
|
8
|
-
let(:
|
9
|
-
let(:
|
7
|
+
let(:validator) { described_class.new(resource, rule) }
|
8
|
+
let(:resource) { stub('resource', foo: 'bar') }
|
9
|
+
let(:rule) { stub('rule', attribute_name: :foo) }
|
10
10
|
|
11
11
|
before do
|
12
|
-
|
12
|
+
rule.should_receive(:matches?).and_return(valid)
|
13
13
|
end
|
14
14
|
|
15
15
|
context "when the attribute is valid" do
|
@@ -4,9 +4,9 @@ describe Yema::Validator, "#valid?" do
|
|
4
4
|
|
5
5
|
subject { validator.valid? }
|
6
6
|
|
7
|
-
let(:validator) { described_class.new(
|
8
|
-
let(:rule) { mock('required_rule', attribute_name: :foo) }
|
7
|
+
let(:validator) { described_class.new(resource, rule) }
|
9
8
|
let(:resource) { mock('resource', foo: value) }
|
9
|
+
let(:rule) { mock('required_rule', attribute_name: :foo) }
|
10
10
|
|
11
11
|
before do
|
12
12
|
resource.should_receive(:foo).and_return(value)
|