violated 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db0d0c235783bdf4ea30a7894d85a8f8a1ea51ee
4
+ data.tar.gz: 8529cb5a53bf75caa91dc19fcb884b69a0fd2e32
5
+ SHA512:
6
+ metadata.gz: 38b42ff487c4e5c8ea56c2f9bd3775274475e7780d97a121b1bf2c4f1a7b438604c97e5463b8d48bb1b45a70e688bd39028e886e11ddf5aff9fda982b062f40f
7
+ data.tar.gz: c31af8e061d840ad88ec68e70f4d670ebb5ed68ad31aea85d817e757c38746f000354bc125298293495ca1308758590e3be62791caf56b6615bff59b048eecbe
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in violated.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 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/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Violated
2
+
3
+ Violated is a simple gem to do validation of any object.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'violated'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install violated
18
+
19
+ ## Usage
20
+
21
+ ### Quick start
22
+
23
+ Add validation to any object by including `Violated.new`:
24
+
25
+ class Person
26
+ include Violated.new
27
+ end
28
+
29
+ Then you can define the violations like this:
30
+
31
+ class Person
32
+ attr_reader :name
33
+
34
+ include Violated.new
35
+
36
+ violate(:name).when_not :present
37
+ end
38
+
39
+ Or multiple per field:
40
+
41
+ class Person
42
+ attr_reader :name
43
+
44
+ include Violated.new
45
+
46
+ violate(:name).when_not :present, [:unique, ->(attr) { uniqueness_check_for_your_application }]
47
+ end
48
+
49
+ You can see if the object is valid:
50
+
51
+ person.valid?
52
+
53
+ and lookup the violations if not:
54
+
55
+ person.violations
56
+
57
+ ### Built-in validators
58
+
59
+ #### :present
60
+
61
+ violate(:attr).when_not :present
62
+
63
+ This fails with a required reason, when the attribute is not present.
64
+
65
+ #### :unique
66
+ violate(:attr).when_not [:unique, ->(attr) { uniqueness_check_for_your_application }]
67
+
68
+ This fails with a duplicate reason, when the lambda given returns true.
69
+
70
+ ### Creating your own validators
71
+
72
+ First create the validator, a validator must be at least initialized with a field and it must respond to validate with an object:
73
+
74
+ class CustomValidator
75
+ def initialize(field)
76
+ @field = field
77
+ end
78
+
79
+ def validate(object)
80
+ object.violations << Violated::Violation.new(@field, :custom) if object.violates?(@field)
81
+ end
82
+ end
83
+
84
+ Register it with the `Registry`:
85
+
86
+ Violated::Registry.register :custom, CustomValidator
87
+
88
+ Use it in your class:
89
+
90
+ class Person
91
+ attr_reader :name
92
+
93
+ include Violated.new
94
+
95
+ violate(:name).when_not :custom
96
+ end
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it ( https://github.com/ThijsWouters/violated/fork )
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/violated.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'violated/version'
2
+ require 'violated/violated'
3
+ require 'violated/validators/presence_validator'
4
+ require 'violated/validators/unique_validator'
5
+
6
+ module Violated
7
+ def self.new
8
+ Violated.new
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ module Violated
2
+ module Registry
3
+ def self.register(name, klass)
4
+ validations[name] = klass
5
+ end
6
+
7
+ def self.get(name)
8
+ validations[name]
9
+ end
10
+
11
+ private
12
+ def self.validations
13
+ @validations ||= Hash.new do |_, name|
14
+ raise "#{name} not registered"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'violated/registry'
2
+ require 'violated/violation'
3
+
4
+ module Violated
5
+ class PresenceValidator
6
+ def initialize(_)
7
+ end
8
+
9
+ def reason
10
+ :required
11
+ end
12
+
13
+ def valid?(value)
14
+ !value.nil? && !value.empty?
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ Violated::Registry.register :present, Violated::PresenceValidator
@@ -0,0 +1,20 @@
1
+ require 'violated/registry'
2
+ require 'violated/violation'
3
+
4
+ module Violated
5
+ class UniqueValidator
6
+ def initialize(_, unique_check)
7
+ @unique_check = unique_check
8
+ end
9
+
10
+ def reason
11
+ :duplicate
12
+ end
13
+
14
+ def valid?(value)
15
+ !@unique_check.call(value)
16
+ end
17
+ end
18
+ end
19
+
20
+ Violated::Registry.register :unique, Violated::UniqueValidator
@@ -0,0 +1,3 @@
1
+ module Violated
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,44 @@
1
+ require_relative 'violator'
2
+
3
+ module Violated
4
+ class Violated < Module
5
+ module ClassMethods
6
+ def violates(field)
7
+ violator_hash[field]
8
+ end
9
+
10
+ def violators
11
+ violator_hash.values
12
+ end
13
+
14
+ def clear_violators
15
+ violator_hash.clear
16
+ end
17
+
18
+ def violator_hash
19
+ @violator_hash ||= Hash.new do |hash, field|
20
+ hash[field] = Violator.new(field)
21
+ end
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def valid?
27
+ violations.empty?
28
+ end
29
+
30
+ def violations
31
+ @violations ||= [].tap do |violations|
32
+ self.class.violators.each do |violator|
33
+ violations.concat(violator.violations_for(self))
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def included(other)
40
+ other.send(:include, InstanceMethods)
41
+ other.send(:extend, ClassMethods)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Violated
2
+ Violation = Struct.new(:field, :reason)
3
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'registry'
2
+
3
+ module Violated
4
+ class Violator
5
+ def initialize(field)
6
+ @field = field
7
+ end
8
+
9
+ def when_not(*validators)
10
+ validators.each do |validator|
11
+ if Array === validator
12
+ validator_type, *args = *validator
13
+ else
14
+ validator_type, args = validator, []
15
+ end
16
+
17
+ self.validators << Registry.get(validator_type).new(@field, *args)
18
+ end
19
+ end
20
+
21
+ def validators
22
+ @validators ||= []
23
+ end
24
+
25
+ def violations_for(object)
26
+ validators.reject { |validator| validator.valid?(object.send(@field)) }.map do |violation|
27
+ Violation.new(@field, violation.reason)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ RSpec.describe ':present' do
2
+ let(:test_class) { Struct.new(:name) do
3
+ include Violated.new
4
+
5
+ violates(:name).when_not :present
6
+ end }
7
+
8
+ context 'value is filled in' do
9
+ subject { test_class.new('Test Testington') }
10
+
11
+ it_behaves_like 'a valid object'
12
+ end
13
+
14
+ context 'value is nil' do
15
+ subject { test_class.new(nil) }
16
+
17
+ it_behaves_like 'an invalid object', [:name], [:required]
18
+ end
19
+
20
+ context 'value is empty' do
21
+ subject { test_class.new('') }
22
+
23
+ it_behaves_like 'an invalid object', [:name], [:required]
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ RSpec.describe ':unique' do
2
+ let(:test_class) { Struct.new(:name) do
3
+ include Violated.new
4
+
5
+ violates(:name).when_not [:unique, ->(name) { name == 'Exist Existington' }]
6
+ end }
7
+
8
+ context 'name is unique' do
9
+ subject { test_class.new('Test Testington') }
10
+
11
+ it_behaves_like 'a valid object'
12
+ end
13
+
14
+ context 'name is not unique' do
15
+ subject { test_class.new('Exist Existington') }
16
+
17
+ it_behaves_like 'an invalid object', [:name], [:duplicate]
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ RSpec.describe 'custom validator' do
2
+ let(:test_class) {
3
+ Struct.new(:name) do
4
+ include Violated.new
5
+
6
+ violates(:name).when_not :custom
7
+ end
8
+ }
9
+ subject { test_class.new }
10
+
11
+ before do
12
+ Violated::Registry.register :custom, custom_validator
13
+ end
14
+
15
+ context 'with a failing validator' do
16
+ let(:custom_validator) { Class.new do
17
+ def initialize(_)
18
+ end
19
+
20
+ def reason
21
+ :custom
22
+ end
23
+
24
+ def valid?(_)
25
+ false
26
+ end
27
+ end }
28
+
29
+ it_behaves_like 'an invalid object', [:name], [:custom]
30
+ end
31
+
32
+ context 'with a succeeding validator' do
33
+ let(:custom_validator) { Class.new do
34
+ def initialize(_)
35
+ end
36
+
37
+ def valid?(_)
38
+ true
39
+ end
40
+ end }
41
+
42
+ it_behaves_like 'a valid object'
43
+ end
44
+ end
@@ -0,0 +1,25 @@
1
+ RSpec.describe 'multiple violations per field' do
2
+ let(:test_class) {Struct.new(:name) do
3
+ include Violated.new
4
+
5
+ violates(:name).when_not :present, [:unique, ->(name) { name == '' }]
6
+ end}
7
+
8
+ context 'no violations' do
9
+ subject { test_class.new('Test Testington') }
10
+
11
+ it_behaves_like 'a valid object'
12
+ end
13
+
14
+ context 'name is not present but unique (1 violation)' do
15
+ subject { test_class.new(nil) }
16
+
17
+ it_behaves_like 'an invalid object', [:name], [:required]
18
+ end
19
+
20
+ context 'name is not present and exists (2 violations)' do
21
+ subject { test_class.new('') }
22
+
23
+ it_behaves_like 'an invalid object', [:name, :name], [:required, :duplicate]
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ RSpec.describe 'violates multiple fields' do
2
+ let(:test_class) { Struct.new(:name, :address) do
3
+ include Violated.new
4
+
5
+ violates(:name).when_not :present
6
+ violates(:address).when_not :present
7
+ end }
8
+
9
+ context 'no violations' do
10
+ subject { test_class.new('Test Testington', '123 Avenuestreet, Township') }
11
+
12
+ it_behaves_like 'a valid object'
13
+ end
14
+
15
+ context 'one violation' do
16
+ subject { test_class.new('Test Testington', '') }
17
+
18
+ it_behaves_like 'an invalid object', [:address], [:required]
19
+ end
20
+
21
+ context 'two violations' do
22
+ subject { test_class.new('', '') }
23
+
24
+ it_behaves_like 'an invalid object', [:name, :address], [:required, :required]
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ RSpec.shared_examples 'a valid object' do
2
+ describe '#valid?' do
3
+ it 'returns true' do
4
+ expect(subject).to be_valid
5
+ end
6
+ end
7
+
8
+ describe '#violations' do
9
+ it 'is empty' do
10
+ expect(subject.violations).to be_empty
11
+ end
12
+ end
13
+ end
14
+
15
+ RSpec.shared_examples 'an invalid object' do |expected_fields, expected_reasons|
16
+ describe '#valid?' do
17
+ it 'returns false' do
18
+ expect(subject).not_to be_valid
19
+ end
20
+ end
21
+
22
+ describe '#violations' do
23
+ it "contains #{expected_fields.count} violation(s)" do
24
+ expect(subject.violations.count).to eq(expected_fields.count)
25
+ end
26
+
27
+ it "contains the #{expected_reasons} reasons" do
28
+ expect(subject.violations.map(&:reason)).to contain_exactly(*expected_reasons)
29
+ end
30
+
31
+ it "contains the #{expected_fields} fields" do
32
+ expect(subject.violations.map(&:field)).to contain_exactly(*expected_fields)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'violated'
2
+ require 'shared_examples'
3
+
4
+ RSpec.configure do |config|
5
+ config.filter_run :focus
6
+ config.run_all_when_everything_filtered = true
7
+
8
+ if config.files_to_run.one?
9
+ config.default_formatter = 'doc'
10
+ end
11
+
12
+ config.profile_examples = 10
13
+
14
+ config.order = :random
15
+
16
+ Kernel.srand config.seed
17
+
18
+ config.expect_with :rspec do |expectations|
19
+ expectations.syntax = :expect
20
+ end
21
+
22
+ config.mock_with :rspec do |mocks|
23
+ mocks.syntax = :expect
24
+ mocks.verify_partial_doubles = true
25
+ end
26
+ end
data/violated.gemspec ADDED
@@ -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 'violated/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'violated'
8
+ spec.version = Violated::VERSION
9
+ spec.authors = ['Thijs Wouters']
10
+ spec.email = ['thijs@morewood.be']
11
+ spec.summary = 'A simple gem to do validation'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/ThijsWouters/violated'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: violated
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thijs Wouters
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-14 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: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A simple gem to do validation
56
+ email:
57
+ - thijs@morewood.be
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/violated.rb
69
+ - lib/violated/registry.rb
70
+ - lib/violated/validators/presence_validator.rb
71
+ - lib/violated/validators/unique_validator.rb
72
+ - lib/violated/version.rb
73
+ - lib/violated/violated.rb
74
+ - lib/violated/violation.rb
75
+ - lib/violated/violator.rb
76
+ - spec/built-in_validators/presence_validator_spec.rb
77
+ - spec/built-in_validators/unique_validator_spec.rb
78
+ - spec/custom_validators/custom_validator_spec.rb
79
+ - spec/multiple_violations/multiple_violations_per_field_spec.rb
80
+ - spec/multiple_violations/violates_multiple_fields_spec.rb
81
+ - spec/shared_examples.rb
82
+ - spec/spec_helper.rb
83
+ - violated.gemspec
84
+ homepage: https://github.com/ThijsWouters/violated
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: A simple gem to do validation
108
+ test_files:
109
+ - spec/built-in_validators/presence_validator_spec.rb
110
+ - spec/built-in_validators/unique_validator_spec.rb
111
+ - spec/custom_validators/custom_validator_spec.rb
112
+ - spec/multiple_violations/multiple_violations_per_field_spec.rb
113
+ - spec/multiple_violations/violates_multiple_fields_spec.rb
114
+ - spec/shared_examples.rb
115
+ - spec/spec_helper.rb