at-validations 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in at-validations.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Kenneth Ballenegger
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # At::Validations
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'at-validations'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install at-validations
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'at-validations/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'at-validations'
8
+ gem.version = ATValidations::VERSION
9
+ gem.authors = ['Kenneth Ballenegger']
10
+ gem.email = ['kenneth@ballenegger.com']
11
+ gem.description = %q{Awesome validation and object matching library}
12
+ gem.summary = %q{Awesome validation and object matching library}
13
+ gem.homepage = 'http://kswizz.com'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+ end
@@ -0,0 +1,3 @@
1
+ module ATValidations
2
+ VERSION = '0.1'
3
+ end
@@ -0,0 +1,112 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/at-validations/version.rb")
2
+
3
+
4
+ # We're adding (monkey-patching) a method to Object and to Hash.
5
+ #
6
+ # Hash gets this convenience method:
7
+ #
8
+ # {hello: :world}.matches_mask({
9
+ # _id: atv_string,
10
+ # status: atv_number + atv_block {|e| e >= 200 && e < 300 }
11
+ # })
12
+ #
13
+
14
+ class Hash
15
+ def matches_mask(mask, opts = {})
16
+ ATValidations::Predicates.atv_hash(mask, opts).call(self)
17
+ end
18
+ end
19
+
20
+
21
+ module ATValidations
22
+
23
+ module Predicates
24
+ def atv_block(&b)
25
+ b
26
+ end
27
+
28
+ def atv_hash(mask, opts = {})
29
+ atv_union(
30
+ atv_instance_of(Hash),
31
+ atv_block do |e|
32
+ errors = {}
33
+ mask.each do |k, v|
34
+ r = v.call(e[k])
35
+ errors[k] = r unless true == r
36
+ end
37
+ if false == opts[:allow_extra] && (extra = (e.keys - mask.keys)).count > 0
38
+ extra.each {|k| errors[k] = 'is not present in predicate' }
39
+ end
40
+
41
+ errors.empty? || Error.new(:error => 'must match hash predicate', :failures => errors)
42
+ end
43
+ )
44
+ end
45
+
46
+ def atv_array_of(predicate)
47
+ atv_union(
48
+ atv_instance_of(Array),
49
+ atv_block do |e|
50
+ errs = {}
51
+ e.each_index {|i| errs[i] = err unless true == (err = predicate.call(e)) }
52
+ errs.count == 0 || Error.new(:error => 'array contains elements which do not match predicate', :failures => errs)
53
+ end
54
+ )
55
+ end
56
+
57
+ def atv_union(*predicates)
58
+ atv_block do |e|
59
+ err = nil
60
+ nil == predicates.find {|p| true != (err = p.call(e)) } ||
61
+ Error.new(:error => 'must match all predicate in union', :failure => err)
62
+ end
63
+ end
64
+
65
+ def atv_option(*predicates)
66
+ atv_block do |e|
67
+ errs = []
68
+ nil != predicates.find {|p| true == (errs << p.call(e)).last } ||
69
+ Error.new(:error => 'must match at least one predicate in option', :failure => errs)
70
+ end
71
+ end
72
+
73
+ def atv_in_set(*set)
74
+ atv_block do |e|
75
+ set.include?(e) || Error.new(:error => "must be in set #{set}")
76
+ end
77
+ end
78
+
79
+ def atv_equal(value)
80
+ atv_block do |e|
81
+ e == value || Error.new(:error => "must be equal to #{value}")
82
+ end
83
+ end
84
+
85
+ def atv_instance_of(klass)
86
+ atv_block do |e|
87
+ e.is_a?(klass) || Error.new(:error => "must be a #{klass}")
88
+ end
89
+ end
90
+
91
+ def atv_numeric
92
+ atv_instance_of(Numeric)
93
+ end
94
+
95
+ def atv_string
96
+ atv_instance_of(String)
97
+ end
98
+
99
+ def atv_symbol
100
+ atv_instance_of(Symbol)
101
+ end
102
+ end
103
+
104
+ class Error < StandardError
105
+ def initialize(info = {})
106
+ @info = info
107
+ end
108
+ def to_s
109
+ @info.to_s
110
+ end
111
+ end
112
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: at-validations
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kenneth Ballenegger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Awesome validation and object matching library
15
+ email:
16
+ - kenneth@ballenegger.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - at-validations.gemspec
27
+ - lib/at-validations.rb
28
+ - lib/at-validations/version.rb
29
+ homepage: http://kswizz.com
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Awesome validation and object matching library
53
+ test_files: []
54
+ has_rdoc: