simple_validate 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/lib/simple_validate.rb +58 -0
- data/lib/simple_validate/errors.rb +29 -0
- data/lib/simple_validate/validates_format_of.rb +16 -0
- data/lib/simple_validate/validates_numericality_of.rb +15 -0
- data/lib/simple_validate/validates_presence_of.rb +15 -0
- data/lib/simple_validate/version.rb +3 -0
- data/simple_validate.gemspec +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d1b5d4bc074cc25068e6f100d2cd522743f06285
|
4
|
+
data.tar.gz: 5751a6868737977c6a4c53a494a3a0e22e38e7d9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ed48e5e9575c3c229a3fbc4af5c162b0f4d7ae7e21d2aaa3fe506d1e837f3f0085ddea8298592e55e00f9cab840372d0715930e84fc5fa7f4ae7b9a41fd10466
|
7
|
+
data.tar.gz: 42486260a3ed6d152e56c63cfd9f50808377182756c86f040acdc6d33b8556f04cd4c2cc713084234c1fd1d0bf9f83b34ede218dd6cbd22e1e4d833fb5dcc1bd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Nick Palaniuk
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# SimpleValidate
|
2
|
+
|
3
|
+
Borrowing ideas from [validatable](https://github.com/jnunemaker/validatable) and Rails validations, this is a library for validations for any ruby object.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'simple_validate'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install simple_validate
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'simple_validate'
|
25
|
+
|
26
|
+
class Person
|
27
|
+
include SimpleValidate
|
28
|
+
attr_accessor :name, :age
|
29
|
+
|
30
|
+
validates_presence_of :name, :age
|
31
|
+
validates_numericality_of :age
|
32
|
+
end
|
33
|
+
|
34
|
+
=> p = Person.new
|
35
|
+
=> #<Person:0x007f9431536408>
|
36
|
+
=> p.valid?
|
37
|
+
=> false
|
38
|
+
=> p.errors
|
39
|
+
=> #<SimpleValidate::Errors:0x007f94318b4df0
|
40
|
+
@messages=
|
41
|
+
{:age=>["can't be empty", "must be a number"],
|
42
|
+
:name=>["can't be empty"]}>
|
43
|
+
```
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
`$ rake` to run the specs
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nikkypx/simple_validate.
|
52
|
+
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
57
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'simple_validate/version'
|
2
|
+
require 'simple_validate/validates_presence_of'
|
3
|
+
require 'simple_validate/validates_format_of'
|
4
|
+
require 'simple_validate/validates_numericality_of'
|
5
|
+
require 'simple_validate/errors'
|
6
|
+
require 'active_support/all'
|
7
|
+
|
8
|
+
module SimpleValidate
|
9
|
+
def self.included(klass)
|
10
|
+
klass.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?
|
14
|
+
self.class.validate(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
def invalid?
|
18
|
+
!valid?
|
19
|
+
end
|
20
|
+
|
21
|
+
def errors
|
22
|
+
@errors ||= Errors.new
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def method_missing(method, *args, &block)
|
27
|
+
if "#{method}" =~ /(validates_(format_of|presence_of|numericality_of))/
|
28
|
+
add_validations(args, const_get($1.classify))
|
29
|
+
else
|
30
|
+
super
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def respond_to_missing?(method, include_private = false)
|
35
|
+
"#{method}" =~ /validates_(format_of|presence_of|numericality_of)/ || super
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_validations(args, klass)
|
39
|
+
options = args.extract_options!
|
40
|
+
args.each do |attr|
|
41
|
+
validations << klass.new(attr, options)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def validations
|
46
|
+
@validations ||= []
|
47
|
+
end
|
48
|
+
|
49
|
+
def validate(instance)
|
50
|
+
validations.each do |validation|
|
51
|
+
unless validation.valid?(instance)
|
52
|
+
instance.errors.add(validation.attribute, validation.message)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
instance.errors.empty?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module SimpleValidate
|
2
|
+
class Errors
|
3
|
+
attr_reader :messages
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@messages = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(attribute, message)
|
10
|
+
if @messages.key?(attribute)
|
11
|
+
@messages[attribute] << message
|
12
|
+
else
|
13
|
+
@messages[attribute] = Array(message)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def on(key)
|
18
|
+
@messages.fetch(key)
|
19
|
+
end
|
20
|
+
|
21
|
+
def empty?
|
22
|
+
@messages.empty?
|
23
|
+
end
|
24
|
+
|
25
|
+
def full_messages
|
26
|
+
@messages.values.flatten.map(&:capitalize)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module SimpleValidate
|
2
|
+
class ValidatesFormatOf
|
3
|
+
attr_reader :message
|
4
|
+
attr_accessor :attribute
|
5
|
+
|
6
|
+
def initialize(attribute, options)
|
7
|
+
@regex = options[:with]
|
8
|
+
@message = options[:message] || 'is incorrect format'
|
9
|
+
@attribute = attribute
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?(instance)
|
13
|
+
!!(instance.send(attribute) =~ @regex)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module SimpleValidate
|
2
|
+
class ValidatesNumericalityOf
|
3
|
+
attr_reader :message
|
4
|
+
attr_accessor :attribute
|
5
|
+
|
6
|
+
def initialize(attribute, options)
|
7
|
+
@message = options[:message] || 'must be a number'
|
8
|
+
@attribute = attribute
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?(instance)
|
12
|
+
instance.send(attribute).is_a?(Numeric)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module SimpleValidate
|
2
|
+
class ValidatesPresenceOf
|
3
|
+
attr_reader :message
|
4
|
+
attr_accessor :attribute
|
5
|
+
|
6
|
+
def initialize(attribute, options)
|
7
|
+
@message = options[:message] || "can't be empty"
|
8
|
+
@attribute = attribute
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?(instance)
|
12
|
+
!instance.send(attribute).nil?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_validate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_validate"
|
8
|
+
spec.version = SimpleValidate::VERSION
|
9
|
+
spec.authors = ["Nick Palaniuk"]
|
10
|
+
spec.email = ["npalaniuk@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q(Validations for any plain old ruby object)
|
13
|
+
spec.description = %q(Validations for any ruby object)
|
14
|
+
spec.homepage = "https://github.com/nikkypx/simple_validate"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_validate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Palaniuk
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Validations for any ruby object
|
56
|
+
email:
|
57
|
+
- npalaniuk@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/simple_validate.rb
|
70
|
+
- lib/simple_validate/errors.rb
|
71
|
+
- lib/simple_validate/validates_format_of.rb
|
72
|
+
- lib/simple_validate/validates_numericality_of.rb
|
73
|
+
- lib/simple_validate/validates_presence_of.rb
|
74
|
+
- lib/simple_validate/version.rb
|
75
|
+
- simple_validate.gemspec
|
76
|
+
homepage: https://github.com/nikkypx/simple_validate
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.3
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Validations for any plain old ruby object
|
100
|
+
test_files: []
|
101
|
+
has_rdoc:
|