hatch 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/hatch.gemspec +1 -1
- data/lib/hatch.rb +24 -3
- data/test/common_validations_test.rb +25 -0
- data/test/polymorphism_test.rb +2 -1
- data/test/validation_test.rb +2 -1
- metadata +3 -3
- data/test/helper.rb +0 -23
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NWM5Y2NiNzZiYjYzYWQ5MWVhOWJkMjYyYTZjMWYyOThkY2JkNzg4Yg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmVkMjEwZjRhZTEwMDY1MmRjMzNjZWUxZmJiYTdlOWZlMTgzNjFhMA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZDYxZTI3ODQ5MzZmNjBhNTczNjdkMzgwZWY4MGU0ZThkNzdmNzFmMDc1MmRm
|
10
|
+
NzNjNWEwYTU3YjZhNGY5ZjM0OGE1NDU4NjlmNzRmOTIyZTE4NWY4Mjc5ZTI1
|
11
|
+
Y2U3NDk5OTJlM2ZjYjQ3MjkzMzFjZmIxOWRmYzc2MTk3MTE5M2Q=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTI3ZjFmNWQwY2Q3MmU3ZDAwMjM4ZDNlMDZjNGIxZmQxZTRhZDMzYTY0N2Zm
|
14
|
+
N2FmNzM4ZjVmZjY1NmI1ZjI3OTM2MDI2NTY2MjU0ZDhhNTgwYWZjYzY3N2Uz
|
15
|
+
NTllYjk0OTBkYzhmMmYxZjA0MTE1OTRmZTQ2ZjI1OTM4MDNmOGQ=
|
data/hatch.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'hatch'
|
3
|
-
s.version = '0.0.
|
3
|
+
s.version = '0.0.6'
|
4
4
|
s.date = Time.now.strftime('%Y-%m-%d')
|
5
5
|
s.summary = 'Keep valid objects only'
|
6
6
|
s.description = "An address without a street? A person without a name? You don't need no invalid objects!"
|
data/lib/hatch.rb
CHANGED
@@ -32,7 +32,11 @@ module Hatch
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def certify(attribute, error, &block)
|
35
|
-
@@validations[self.to_s.to_sym][attribute] =
|
35
|
+
@@validations[self.to_s.to_sym][attribute] = Validation.new(error, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def certifies(attribute, validation, error = nil)
|
39
|
+
@@validations[self.to_s.to_sym][attribute] = Validation.send(validation, error)
|
36
40
|
end
|
37
41
|
|
38
42
|
def hatch(args = {})
|
@@ -42,8 +46,8 @@ module Hatch
|
|
42
46
|
validation = @@validations[klass_symbol][attribute]
|
43
47
|
validated_attributes << Validator.validate(attribute,
|
44
48
|
args[attribute],
|
45
|
-
validation
|
46
|
-
&validation
|
49
|
+
validation.error,
|
50
|
+
&validation.block)
|
47
51
|
end
|
48
52
|
|
49
53
|
build(validated_attributes)
|
@@ -130,5 +134,22 @@ module Hatch
|
|
130
134
|
!@valid
|
131
135
|
end
|
132
136
|
end
|
137
|
+
|
138
|
+
class Validation
|
139
|
+
attr_reader :error, :block
|
140
|
+
|
141
|
+
def initialize(error, &block)
|
142
|
+
@error = error
|
143
|
+
@block = block
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.presence(error)
|
147
|
+
new(error || "must be present") {|value| !value.nil? && !value.empty?}
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.positive_number(error)
|
151
|
+
new(error || "must be a positive number") {|value| !value.nil? && value > 0}
|
152
|
+
end
|
153
|
+
end
|
133
154
|
end
|
134
155
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'support/helper'
|
2
|
+
require_relative '../lib/hatch'
|
3
|
+
|
4
|
+
class CommonStuff
|
5
|
+
include Hatch
|
6
|
+
|
7
|
+
attributes :present, :positive
|
8
|
+
|
9
|
+
certifies :present, :presence
|
10
|
+
certifies :positive, :positive_number
|
11
|
+
end
|
12
|
+
|
13
|
+
class CommonValidationsTest < Test::Unit::TestCase
|
14
|
+
def test_presence
|
15
|
+
common_stuff = CommonStuff.hatch(present: nil, positive: 1)
|
16
|
+
assert common_stuff.is_a?(CommonStuff::InvalidCommonStuff)
|
17
|
+
assert common_stuff.errors.include?("must be present")
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_positive_number
|
21
|
+
common_stuff = CommonStuff.hatch(present: "here", positive: -1)
|
22
|
+
assert common_stuff.is_a?(CommonStuff::InvalidCommonStuff)
|
23
|
+
assert common_stuff.errors.include?("must be a positive number")
|
24
|
+
end
|
25
|
+
end
|
data/test/polymorphism_test.rb
CHANGED
data/test/validation_test.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hatch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Tolchinsky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An address without a street? A person without a name? You don't need
|
14
14
|
no invalid objects!
|
@@ -21,7 +21,7 @@ files:
|
|
21
21
|
- README.md
|
22
22
|
- hatch.gemspec
|
23
23
|
- Rakefile
|
24
|
-
- test/
|
24
|
+
- test/common_validations_test.rb
|
25
25
|
- test/polymorphism_test.rb
|
26
26
|
- test/validation_test.rb
|
27
27
|
- lib/hatch.rb
|
data/test/helper.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'pry'
|
3
|
-
require_relative '../lib/hatch'
|
4
|
-
|
5
|
-
class Address
|
6
|
-
attr_reader :city, :street, :number
|
7
|
-
|
8
|
-
include Hatch
|
9
|
-
attributes :city, :street, :number
|
10
|
-
|
11
|
-
certify(:street, "Address must have a street") do |street|
|
12
|
-
!street.nil? && !street.empty?
|
13
|
-
end
|
14
|
-
|
15
|
-
certify(:number, "Address must have a positive number") do |number|
|
16
|
-
!number.nil? && number > 0
|
17
|
-
end
|
18
|
-
|
19
|
-
certify(:city, "Address must have a city") do |city|
|
20
|
-
!city.nil? && !city.empty?
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|