hatch 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +6 -10
- data/hatch.gemspec +1 -1
- data/lib/hatch.rb +2 -10
- data/test/common_validations_test.rb +0 -2
- data/test/validation_test.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NWIyNzI5OWMyNjA5ZjQ0ZmE1OWExZDIxYWFhMmYxMWQwMTNjNjM0NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MzUwYzM2NzI1YmRiNjNhMjYxMjQwYWUyYzZjODNiM2QyMDE4Nzc2Mw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGIxMWE4YTg3OTEzNTllZjA4YjFlNjY1NmU4ZjhiNWQwMmIzN2Q2ODQ1ZDIw
|
10
|
+
OWZiMDM0MmNjNmRkNTQxZGEzM2QzMDY5YTIxNWExZDFhMjEzYzQzMzg3YWZl
|
11
|
+
OTBkZDgzYTI4Y2Q1NWJkYmVmMmUxNWNhYjEwOTNkYTNiM2QwZjM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OGU1YmJjYTgzNjY4MGJiMjI4ZDYzOTIxZmVhMmVjMmY0YjcyNTVlNGMyYzJj
|
14
|
+
M2FjYzYwNzQ0MzMwNDcyNmRjYzdiZWZkNjAzMzZmMTU0ZDEzNjY0ZmIwMGEw
|
15
|
+
MjY0NTU2M2M4Yjg2Zjc4YTdjZjVmNzZlYWM4ZmRjYmYxNjA5NzU=
|
data/README.md
CHANGED
@@ -12,18 +12,16 @@ Usage
|
|
12
12
|
An address without a street? A person without a name? Those are not valid objects!
|
13
13
|
Why should you have them hanging around your system?
|
14
14
|
|
15
|
-
Tell `Hatch` how to certify the attributes of your models, and
|
15
|
+
Tell `Hatch` how to certify the attributes of your models, and it will give you
|
16
16
|
the appropriate object.
|
17
17
|
|
18
|
-
If you don't hatch your model with all the correct attributes,
|
19
|
-
an object representing an invalid instance of it.
|
18
|
+
If you don't hatch your model with all the correct attributes, you'll get an object representing an invalid instance of it.
|
20
19
|
|
21
20
|
```ruby
|
22
21
|
require 'hatch'
|
23
22
|
|
24
23
|
class Address
|
25
24
|
include Hatch
|
26
|
-
attributes :street, :number
|
27
25
|
|
28
26
|
certify(:street, 'Address must have a street') do |street|
|
29
27
|
!street.nil? && !street.empty?
|
@@ -47,27 +45,25 @@ not_an_address.valid?
|
|
47
45
|
# => false
|
48
46
|
```
|
49
47
|
|
50
|
-
|
51
|
-
then use `certify(:attribute, 'error message', &validation)` to verify when an
|
48
|
+
Use `certify(:attribute, 'error message', &validation)` to verify when an
|
52
49
|
attribute is valid.
|
53
50
|
|
54
51
|
In case you're wondering, the `Model::InvalidModel` is polymorphic with your
|
55
52
|
`Model` in all the reader methods declared by `attr_reader` or `attr_accessor`
|
56
53
|
|
57
|
-
`Hatch` also supports some common validations we all like to have!
|
58
|
-
|
54
|
+
`Hatch` also supports some common validations we all like to have!
|
55
|
+
`certifies(:attribute, :validation, 'error message')` will do the trick.
|
59
56
|
|
60
57
|
```ruby
|
61
58
|
class Address
|
62
59
|
include Hatch
|
63
|
-
attributes :street, :number
|
64
60
|
|
65
61
|
certifies(:street, :presence, "This is an error! Where's my street?!")
|
66
62
|
certifies(:number, :positive_number)
|
67
63
|
end
|
68
64
|
```
|
69
65
|
|
70
|
-
Common validations come in the following flavours (along with default
|
66
|
+
Common validations come in the following flavours (along with default error messages)
|
71
67
|
|
72
68
|
* `:presence` - `"must be present"`
|
73
69
|
* `:positive_number` - `"must be a positive number"`
|
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.1.0'
|
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
@@ -13,12 +13,10 @@ module Hatch
|
|
13
13
|
|
14
14
|
module ClassMethods
|
15
15
|
@@validations = {}
|
16
|
-
@@attributes = {}
|
17
16
|
|
18
17
|
def self.extended(klass)
|
19
18
|
klass_symbol = klass.to_s.to_sym
|
20
19
|
@@validations[klass_symbol] = {}
|
21
|
-
@@attributes[klass_symbol] = []
|
22
20
|
|
23
21
|
invalid_class = Class.new do
|
24
22
|
include InvalidInstanceMethods
|
@@ -27,10 +25,6 @@ module Hatch
|
|
27
25
|
klass.const_set("Invalid#{klass}", invalid_class)
|
28
26
|
end
|
29
27
|
|
30
|
-
def attributes(*args)
|
31
|
-
@@attributes[self.to_s.to_sym] = args
|
32
|
-
end
|
33
|
-
|
34
28
|
def certify(attribute, error, &block)
|
35
29
|
@@validations[self.to_s.to_sym][attribute] = Validation.new(error, &block)
|
36
30
|
end
|
@@ -41,9 +35,7 @@ module Hatch
|
|
41
35
|
|
42
36
|
def hatch(args = {})
|
43
37
|
validated_attributes = []
|
44
|
-
|
45
|
-
@@attributes[klass_symbol].each do |attribute|
|
46
|
-
validation = @@validations[klass_symbol][attribute]
|
38
|
+
@@validations[self.to_s.to_sym].each_pair do |attribute, validation|
|
47
39
|
validated_attributes << Validator.validate(attribute,
|
48
40
|
args[attribute],
|
49
41
|
validation.error,
|
@@ -102,7 +94,7 @@ module Hatch
|
|
102
94
|
end
|
103
95
|
|
104
96
|
def set_instance_variables(instance, *args)
|
105
|
-
@@
|
97
|
+
@@validations[instance.class.to_s.to_sym].keys.each_with_index do |attribute, index|
|
106
98
|
instance.instance_variable_set("@#{attribute}", args[index].value)
|
107
99
|
end
|
108
100
|
instance
|
data/test/validation_test.rb
CHANGED
@@ -15,5 +15,11 @@ class ValidationTest < Test::Unit::TestCase
|
|
15
15
|
assert address.respond_to?(:valid?)
|
16
16
|
assert !address.valid?
|
17
17
|
end
|
18
|
+
|
19
|
+
def test_ignore_attributes
|
20
|
+
address = Address.hatch(street: 'Fake St', city: 'Buenos Aires', number: 1234, sorry: :oops)
|
21
|
+
assert address.is_a?(Address)
|
22
|
+
assert address.valid?
|
23
|
+
end
|
18
24
|
end
|
19
25
|
|
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.1.0
|
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-06-02 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!
|