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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzJjNWMxMjRlMzM0OGM0YTk2YWY3MmQ5YjAyZWNiMDZjYjg1YWQ0OA==
4
+ NWIyNzI5OWMyNjA5ZjQ0ZmE1OWExZDIxYWFhMmYxMWQwMTNjNjM0NQ==
5
5
  data.tar.gz: !binary |-
6
- NzJhNTQyYjJhMTUyOWZmOWE1OTdjMzY2ZDZmMDQ0MmViY2Y5ODEzZA==
6
+ MzUwYzM2NzI1YmRiNjNhMjYxMjQwYWUyYzZjODNiM2QyMDE4Nzc2Mw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MTY4NzUzN2Y3OGYyMWFlYmVmNGVjMTI1NDg5Yjc0Y2JiOGY2YmM4NWQyY2Fm
10
- OTFlMTc0Y2Q5ZWE0YmFhODJkNTc4YWY3YjNhMmQ1MDU3ZWQyNzI1ZmIxZmFk
11
- ZjA4ZDNlODYwMmY0NTgzMmQyYTY1YTFiMzQwNDc3YzBkMTVlYzY=
9
+ ZGIxMWE4YTg3OTEzNTllZjA4YjFlNjY1NmU4ZjhiNWQwMmIzN2Q2ODQ1ZDIw
10
+ OWZiMDM0MmNjNmRkNTQxZGEzM2QzMDY5YTIxNWExZDFhMjEzYzQzMzg3YWZl
11
+ OTBkZDgzYTI4Y2Q1NWJkYmVmMmUxNWNhYjEwOTNkYTNiM2QwZjM=
12
12
  data.tar.gz: !binary |-
13
- M2E5M2QyNzE3YjE1NDRmZWM4MDAzNDg2ZjU1ZmY4YTNhMjRkODcxZjc1Yzg2
14
- MmQyMGM4NzY0ZTU1NjIwNGQzNDlkNzc5MmQ4NTFhNjY5MzY3NjhiYmFiMTBk
15
- ODQ5M2IwNjI0ZjYzNDdhN2MyMzQ5MWMzODBiNDg1ZWI5M2JjNTU=
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 he will give you
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, it will give you
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
- You declare your attributes to `Hatch` with the `attributes` message and
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! You can pass an error
58
- of your own or just use the default.
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 errors)
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"`
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'hatch'
3
- s.version = '0.0.7'
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!"
@@ -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
- klass_symbol = self.to_s.to_sym
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
- @@attributes[instance.class.to_s.to_sym].each_with_index do |attribute, index|
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
@@ -4,8 +4,6 @@ require_relative '../lib/hatch'
4
4
  class CommonStuff
5
5
  include Hatch
6
6
 
7
- attributes :present, :positive
8
-
9
7
  certifies :present, :presence
10
8
  certifies :positive, :positive_number
11
9
  end
@@ -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.7
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-05-31 00:00:00.000000000 Z
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!