hatch 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +93 -0
  3. data/hatch.gemspec +2 -1
  4. data/lib/hatch.rb +5 -5
  5. metadata +3 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmM2NTJmMWJlZWRmMGRmNTliMmUwMmFkYjhiYTcyM2I2ZDkwY2Y5Mg==
4
+ ZmMxN2EyNTUzN2M2NWMyMmNiOWI3NzY0NDZkZTZmN2NmMGJiNWE0Mw==
5
5
  data.tar.gz: !binary |-
6
- ZDhmMDliNGQ3Zjk2MzRlNzk5YTRjMDI4Y2E4ZTI3Yjg1YWZlNjhmOQ==
6
+ YjEzZmZlMWRlNDkwNGZmMDg5MDc5NGU5ZjZjN2U4ZTU3NGI0MzlhZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YTUwMzA0MjVmOGJjZjc0OTRkM2FhNDRlNDM3MGFkMTM0YWY4ZTc5ZGYyMzlm
10
- NDc2ZDFhNjBlOWM1ODAwYTA3NTBmZjQzYjljNmI5OTkxYmEwMjRmZTVhMDcz
11
- ZWZmYTc3YzRmM2ZhODA2OGRmZDU5MjUxNTRjYjdhMTZjMGYyODE=
9
+ MzYxOTA2YzRlYjNiY2FhZTVkYTZkMTJmMWVlOTNiMjM3NWI3NWQ2MzlmNjVm
10
+ YzFjNDhlYjZkZTc2Y2IyNTViY2VhOGQ4YmUwMGFiY2U4YWNjMTE0MzU1MzZi
11
+ ZWFiODk3ZmY0MjQ4YjhkMDEwZTI0YmFhNGZkNmQwZTc2MDVkOGM=
12
12
  data.tar.gz: !binary |-
13
- ZTNlNTgwNjVjMGU0ZGU3NGIzNWMwNGNlMzlhZWEyYzJhN2EyNzZhNjQxNzE4
14
- ZTY3NjI3ZjAxNzU2MDQ4MDgwOTE0MmIzM2M4YjU4YmIxMGU4ZDAyNzQyOGJk
15
- ZGI1MmE0OGUzMGUxMTFjZWNjNzY1ZTUwMTgzODA2ZDZmMzgyM2I=
13
+ MWUwNjNjZDZlOWNmMjJiN2M0NDhhZDgzYzY0NjYzN2U4ZjE5MDZjOTI0Zjc1
14
+ OWFjOTA0Y2FiNTMxOGU5Mjg3MjE0ZDU3OGJiYWY1MmE0NjAzNjUzY2YyZTEz
15
+ YjUzYzM1YWE4YzQ4MTNkNzU1MTg5YWMwNmZiNjJjMjY2M2QwOWU=
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ Hatch
2
+ =====
3
+
4
+ An address without a street? A person without a name? Those are not valid objects!
5
+ Why should you have them hanging around your system?
6
+
7
+ Tell ```Hatch``` how to certify the attributes of your models, and he will give you
8
+ the appropiate object.
9
+
10
+ If you don't hatch your model with all the correct attributes, it will give you
11
+ an object representing an invalid instance of it.
12
+
13
+ ```ruby
14
+ require 'hatch'
15
+
16
+ class Address
17
+ include Hatch
18
+ attributes :street, :number
19
+
20
+ certify(:street, 'Address must have a street') do |street|
21
+ !street.nil? && !street.empty?
22
+ end
23
+
24
+ certify(:number, 'Address must have a positive number') do |number|
25
+ !number.nil? && number > 0
26
+ end
27
+ end
28
+
29
+ address = Address.hatch(street: 'Fake St', number: 1234)
30
+ address.class
31
+ # => Address
32
+
33
+ not_an_address = Address.hatch(street: '', number: 1234)
34
+ not_an_address.class
35
+ # => Address::InvalidAddress
36
+ ```
37
+
38
+ You declare your attributes to ```Hatch``` with the ```attributes``` message and
39
+ then use ```certify(:attribute, 'error message', &validation)``` to verify when an
40
+ attribute is valid.
41
+
42
+ You'll also get some handy ```errors``` and ```valid?``` methods for both your valid
43
+ and invalid model instances.
44
+
45
+ ```ruby
46
+ not_an_address = Address.hatch(street: '', number: 1234)
47
+ not_an_address.class
48
+ # => Address::InvalidAddress
49
+
50
+ not_an_address.errors
51
+ # => ['Address must have a street']
52
+
53
+ not_an_address.valid?
54
+ # => false
55
+ ```
56
+
57
+ In case you're wondering, the ```Model::InvalidModel``` is polymorphic with your
58
+ ```Model``` in all the reader methods declared by ```attr_reader``` or ```attr_accessor```
59
+
60
+ Installation
61
+ ------------
62
+
63
+ $ gem install hatch
64
+
65
+ Thanks
66
+ ------
67
+
68
+ To [@pote](https://github.com/pote) for the help, support and company!
69
+
70
+ License
71
+ -------
72
+
73
+ MIT License
74
+
75
+ Permission is hereby granted, free of charge, to any person obtaining
76
+ a copy of this software and associated documentation files (the
77
+ "Software"), to deal in the Software without restriction, including
78
+ without limitation the rights to use, copy, modify, merge, publish,
79
+ distribute, sublicense, and/or sell copies of the Software, and to
80
+ permit persons to whom the Software is furnished to do so, subject to
81
+ the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be
84
+ included in all copies or substantial portions of the Software.
85
+
86
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
89
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
90
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
91
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
92
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
93
+
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.4'
3
+ s.version = '0.0.5'
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!"
@@ -10,6 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.license = 'MIT'
11
11
 
12
12
  s.files = Dir[
13
+ 'README.md',
13
14
  '*.gemspec',
14
15
  'Rakefile',
15
16
  'test/*.*',
data/lib/hatch.rb CHANGED
@@ -20,11 +20,11 @@ module Hatch
20
20
  @@validations[klass_symbol] = {}
21
21
  @@attributes[klass_symbol] = []
22
22
 
23
- klass.class_eval <<-EOS
24
- class Invalid#{klass}
25
- include InvalidInstanceMethods
26
- end
27
- EOS
23
+ invalid_class = Class.new do
24
+ include InvalidInstanceMethods
25
+ end
26
+
27
+ klass.const_set("Invalid#{klass}", invalid_class)
28
28
  end
29
29
 
30
30
  def attributes(*args)
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
4
+ version: 0.0.5
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-04-19 00:00:00.000000000 Z
11
+ date: 2013-04-20 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!
@@ -18,6 +18,7 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - README.md
21
22
  - hatch.gemspec
22
23
  - Rakefile
23
24
  - test/helper.rb