simple_validation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ simple_validation-*.gem
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ v0.0.1
2
+ ------
3
+
4
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in easy_validation.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) <2011> <Chirantan Mitra>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ Overview
2
+ --------
3
+
4
+ This is very simple gem to allow custom validations in a ruby object. You are supposed to define your validations.
5
+
6
+ Dependencies
7
+ ------------
8
+
9
+ These are no dependencies for this gem. You need minitest to run the tests for this gem.
10
+
11
+ Installation
12
+ ------------
13
+
14
+ ``` script
15
+ gem install simple_validation
16
+ ```
17
+
18
+ Usage
19
+ ------
20
+
21
+ ``` ruby
22
+ require "simple_validation"
23
+
24
+ class AlienNumber
25
+ include SimpleValidation
26
+
27
+ validate :digits_are_positive
28
+ validate :digits_are_less_than_ten
29
+
30
+ def initialize(*digits)
31
+ @digits = digits
32
+ end
33
+
34
+ def value
35
+ @digits.reduce(1) {|result, digit| result + digit}
36
+ end
37
+
38
+ private
39
+
40
+ def digits_are_positive
41
+ @digits.each do |digit|
42
+ add_error("#{digit} is negative") unless digit > 0
43
+ end
44
+ end
45
+
46
+ def digits_are_less_than_ten
47
+ @digits.each do |digit|
48
+ add_error("#{digit} is greater than 9") unless digit < 10
49
+ end
50
+ end
51
+ end
52
+
53
+ valid_number = AlienNumber.new(1, 2, 3)
54
+ valid_number.valid? # true
55
+ valid_number.invalid? # false
56
+ valid_number.errors # []
57
+
58
+ invalid_number = AlienNumber.new(-1, 12)
59
+ invalid_number.valid? # false
60
+ invalid_number.invalid? # true
61
+ invalid_number.errors # ["-1 is negative", "12 is greater than 9"]
62
+ ```
63
+
64
+ Running tests
65
+ -------------
66
+
67
+ Clone the repository and run the following 'rake' from the repository.
68
+
69
+ License
70
+ -------
71
+
72
+ This tool is released under the MIT license. Please refer to LICENSE for more details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.pattern = "spec/*_spec.rb"
5
+ end
6
+
7
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module SimpleValidation
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,41 @@
1
+ module SimpleValidation
2
+ def self.included(base)
3
+ base.class_eval do
4
+ base.extend SimpleValidation::ClassMethods
5
+ end
6
+ end
7
+
8
+ module ClassMethods
9
+ def validate(method_name)
10
+ validation_methods << method_name
11
+ end
12
+
13
+ def validation_methods
14
+ @validation_methods ||= []
15
+ end
16
+ end
17
+
18
+ def valid?
19
+ self.class.validation_methods.each do |method_name|
20
+ send method_name
21
+ end
22
+
23
+ errors.empty?
24
+ end
25
+
26
+ def invalid?
27
+ not valid?
28
+ end
29
+
30
+ def add_error(error)
31
+ errors << error unless errors.include?(error)
32
+ end
33
+
34
+ def add_errors(more_errors)
35
+ errors.concat(more_errors)
36
+ end
37
+
38
+ def errors
39
+ @errors ||= []
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+
5
+ require "simple_validation/version"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "simple_validation"
9
+ s.version = SimpleValidation::VERSION
10
+ s.authors = ["Chirantan Mitra"]
11
+ s.email = ["chirantan.mitra@gmail.com"]
12
+ s.homepage = "https://github.com/chiku/simple_validation"
13
+ s.summary = "Validations for a ruby object"
14
+ s.description = <<-EOS
15
+ A simple way to validate custom ruby objects using validation defined in the class.
16
+ EOS
17
+ s.rubyforge_project = "simple_validation"
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_development_dependency "simplecov"
24
+ s.add_development_dependency "minitest"
25
+ end
@@ -0,0 +1,119 @@
1
+ require_relative "spec_helper"
2
+
3
+ require_relative "../lib/simple_validation"
4
+
5
+ class TestEntity
6
+ include SimpleValidation
7
+ end
8
+
9
+ class TestEntityWithValidation
10
+ include SimpleValidation
11
+
12
+ attr_reader :always_valid_invoked
13
+ attr_reader :always_invalid_invoked
14
+
15
+ validate :always_valid
16
+ validate :always_invalid
17
+
18
+ def initialize
19
+ @always_valid_invoked = false
20
+ @always_invalid_invoked = false
21
+ end
22
+
23
+ def always_valid
24
+ @always_valid_invoked = true
25
+ end
26
+
27
+ def always_invalid
28
+ @always_invalid_invoked = true
29
+ add_error "Always invalid"
30
+ end
31
+ end
32
+
33
+ describe "An entity that can be validated" do
34
+ describe "when it has errors" do
35
+ before do
36
+ @entity = TestEntity.new
37
+ @entity.add_error("An error")
38
+ end
39
+
40
+ it "is invalid" do
41
+ @entity.invalid?.must_equal true
42
+ end
43
+
44
+ it "is not valid" do
45
+ @entity.valid?.must_equal false
46
+ end
47
+
48
+ it "exposes its errors" do
49
+ @entity.errors.must_equal ["An error"]
50
+ end
51
+
52
+ it "allows custom validation" do
53
+ @entity.errors.must_equal ["An error"]
54
+ end
55
+ end
56
+
57
+ describe "when it doesn't have any error" do
58
+ before do
59
+ @entity = TestEntity.new
60
+ end
61
+
62
+ it "is not invalid" do
63
+ @entity.invalid?.must_equal false
64
+ end
65
+
66
+ it "is valid" do
67
+ @entity.valid?.must_equal true
68
+ end
69
+
70
+ it "exposes its errors as an empty array" do
71
+ @entity.errors.must_equal []
72
+ end
73
+ end
74
+
75
+ describe "when it has a custom validation" do
76
+ before do
77
+ @entity = TestEntityWithValidation.new
78
+ end
79
+
80
+ describe "on validation" do
81
+ before do
82
+ @entity.valid?
83
+ end
84
+
85
+ it "invokes the validations" do
86
+ @entity.always_valid_invoked.must_equal true
87
+ @entity.always_invalid_invoked.must_equal true
88
+ end
89
+
90
+ it "has errors added during validation" do
91
+ @entity.errors.must_equal ["Always invalid"]
92
+ end
93
+ end
94
+
95
+ describe "with already existing errors" do
96
+ it "doesn't lose its older errors on validation" do
97
+ @entity = TestEntityWithValidation.new
98
+ @entity.add_errors(["An error", "Another error"])
99
+ @entity.valid?
100
+ @entity.errors.must_equal ["An error", "Another error", "Always invalid"]
101
+ end
102
+ end
103
+
104
+ describe "when double validated" do
105
+ it "doesn't duplicate its errors" do
106
+ @entity = TestEntityWithValidation.new
107
+ @entity.valid?
108
+ @entity.valid?
109
+ @entity.errors.must_equal ["Always invalid"]
110
+ end
111
+ end
112
+ end
113
+
114
+ it "can be added an array of errors" do
115
+ @entity = TestEntity.new
116
+ @entity.add_errors(["An error", "Another error"])
117
+ @entity.errors.must_equal ["An error", "Another error"]
118
+ end
119
+ end
@@ -0,0 +1,2 @@
1
+ require "minitest/autorun"
2
+ require "minitest/spec"
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chirantan Mitra
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: simplecov
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! 'A simple way to validate custom ruby objects using validation defined
47
+ in the class.
48
+
49
+ '
50
+ email:
51
+ - chirantan.mitra@gmail.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - !binary |-
57
+ LmdpdGlnbm9yZQ==
58
+ - !binary |-
59
+ Q0hBTkdFTE9HLm1k
60
+ - !binary |-
61
+ R2VtZmlsZQ==
62
+ - !binary |-
63
+ TElDRU5TRQ==
64
+ - !binary |-
65
+ UkVBRE1FLm1k
66
+ - !binary |-
67
+ UmFrZWZpbGU=
68
+ - !binary |-
69
+ bGliL3NpbXBsZV92YWxpZGF0aW9uLnJi
70
+ - !binary |-
71
+ bGliL3NpbXBsZV92YWxpZGF0aW9uL3ZlcnNpb24ucmI=
72
+ - !binary |-
73
+ c2ltcGxlX3ZhbGlkYXRpb24uZ2Vtc3BlYw==
74
+ - !binary |-
75
+ c3BlYy9zaW1wbGVfdmFsaWRhdGlvbl9zcGVjLnJi
76
+ - !binary |-
77
+ c3BlYy9zcGVjX2hlbHBlci5yYg==
78
+ homepage: https://github.com/chiku/simple_validation
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project: simple_validation
98
+ rubygems_version: 1.8.24
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Validations for a ruby object
102
+ test_files: []