sanatio 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 479bf5da1408bc5c3ff984ad7baa6899a49a5152
4
+ data.tar.gz: 1dff2a76f7fe9f3393ee37b4312df7a30cf9e6a0
5
+ SHA512:
6
+ metadata.gz: 3703329eb46d2bd728dab0e55c0e38a59f3569fbe005767f36f2d23952c588ffde5e8e27a48ea93496ac9e7e2e77823bc0ca14f5072d109c4758c0399e00ccad
7
+ data.tar.gz: 74bf2a27049e5d97281505679c3a71fde78d10176844fc6f987e94546148efa7761bd3a1a34b29c2b1fe96fb4fe561c071826fda580b281da425ddfc300f65d9
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-version
11
+ .ruby-gemset
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ - 2.1
5
+ - 2.0
6
+ - 1.9.3
7
+ - 1.9.2
8
+ - 1.8.7
9
+ - jruby-18mode
10
+ - jruby-19mode
11
+ - rbx
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sanatio.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Thijs Wouters
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Thijs Wouters
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,86 @@
1
+ # Sanatio
2
+
3
+ ![Build status](https://travis-ci.org/ThijsWouters/sanatio.svg)
4
+
5
+ Sanatio is a gem that does validation. It has no external dependencies.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sanatio'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sanatio
22
+
23
+ ## Usage
24
+
25
+ ### One-time validation
26
+
27
+ If you only need a validation in a single place. You can use a block.
28
+
29
+ If your block evaluates to a falsey value, then the validation fails with the reason that is given.
30
+
31
+ With field level validation:
32
+
33
+ ```ruby
34
+ class Person
35
+ include Sanatio
36
+
37
+ def initialize(name)
38
+ @name = name
39
+ end
40
+
41
+ attr_reader :name
42
+
43
+ ensure_that(:name).is { !nil? }.reason = :not_nil
44
+ end
45
+
46
+ person = Person.new(nil)
47
+ error = person.errors.first
48
+ error.field # :name
49
+ error.reason # :not_nil
50
+ person.valid? # false
51
+ ```
52
+
53
+ Or, with a class level validation:
54
+
55
+ ```ruby
56
+ class Person
57
+ include Sanatio
58
+
59
+ def initialize(name)
60
+ @name = name
61
+ end
62
+
63
+ attr_reader :name
64
+
65
+ ensure_that(self).is { !name.nil? }.reason = :name_not_nil
66
+ end
67
+
68
+ person = Person.new(nil)
69
+ error = person.errors.first
70
+ error.reason # :name_not_nil
71
+ person.valid? # false
72
+ ```
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( https://github.com/[my-github-username]/sanatio/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList["**/test_*.rb"]
7
+ end
8
+
9
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sanatio"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ gem install bundler
6
+
7
+ bundle install
8
+
9
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ require 'sanatio/usage_error'
2
+
3
+ module Sanatio
4
+ class ClassValidator
5
+ attr_accessor :reason
6
+
7
+ def initialize
8
+ end
9
+
10
+ def is(&validation_block)
11
+ unless block_given?
12
+ raise UsageError.new("self")
13
+ end
14
+
15
+ @validation_block = validation_block
16
+ self
17
+ end
18
+
19
+ def valid?(object)
20
+ object.instance_eval(&@validation_block)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ require 'forwardable'
2
+
3
+ module Sanatio
4
+ class Error
5
+ extend Forwardable
6
+
7
+ def_delegators :@validation, :field, :reason
8
+
9
+ def initialize(validation)
10
+ @validation = validation
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ require 'sanatio/usage_error'
2
+
3
+ module Sanatio
4
+ class FieldValidator
5
+ attr_reader :field
6
+ attr_accessor :reason
7
+
8
+ def initialize(field)
9
+ @field = field
10
+ end
11
+
12
+ def is(&validation_block)
13
+ unless block_given?
14
+ raise UsageError.new(":#{field}")
15
+ end
16
+
17
+ @validation_block = validation_block
18
+ self
19
+ end
20
+
21
+ def valid?(object)
22
+ object.send(@field).instance_eval &@validation_block
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Sanatio
2
+ class UsageError < StandardError
3
+ def initialize(target)
4
+ super("You need to give a block to #is. The correct usage is:\nensure_that(#{target}).is { validation_test }")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ require "sanatio/field_validator"
2
+ require "sanatio/class_validator"
3
+
4
+ module Sanatio
5
+ module ValidatorFactory
6
+ def self.create(target)
7
+ if target.instance_of? Class
8
+ ClassValidator.new
9
+ else
10
+ FieldValidator.new(target)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Sanatio
2
+ VERSION = "0.1.0"
3
+ end
data/lib/sanatio.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "sanatio/version"
2
+ require "sanatio/error"
3
+ require "sanatio/validator_factory"
4
+
5
+ module Sanatio
6
+ module ClassMethods
7
+ def ensure_that(target)
8
+ ValidatorFactory.create(target).tap do |validator|
9
+ validators << validator
10
+ end
11
+ end
12
+
13
+ def validators
14
+ @validation ||= []
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+ def valid?
20
+ errors.empty?
21
+ end
22
+
23
+ def errors
24
+ self.class.validators.reject do |validator|
25
+ validator.valid?(self)
26
+ end.map(&Error.method(:new))
27
+ end
28
+ end
29
+
30
+ def self.included(other)
31
+ other.send(:include, InstanceMethods)
32
+ other.send(:extend, ClassMethods)
33
+ end
34
+ end
data/sanatio.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sanatio/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sanatio"
8
+ spec.version = Sanatio::VERSION
9
+ spec.authors = ["Thijs Wouters"]
10
+ spec.email = ["thijs@morewood.be"]
11
+
12
+ spec.summary = "A gem for validation, without all the dependencies."
13
+ spec.homepage = "https://github.com/ThijsWouters/sanatio"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sanatio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thijs Wouters
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-10 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - thijs@morewood.be
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/sanatio.rb
73
+ - lib/sanatio/class_validator.rb
74
+ - lib/sanatio/error.rb
75
+ - lib/sanatio/field_validator.rb
76
+ - lib/sanatio/usage_error.rb
77
+ - lib/sanatio/validator_factory.rb
78
+ - lib/sanatio/version.rb
79
+ - sanatio.gemspec
80
+ homepage: https://github.com/ThijsWouters/sanatio
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.6
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: A gem for validation, without all the dependencies.
104
+ test_files: []