extracted_validator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c5409415dd85296173864c69d44038bc70fb964c
4
+ data.tar.gz: 7695fa2d67a5f593f22aca14ef503ae66a6556e4
5
+ SHA512:
6
+ metadata.gz: 4095402ad67ea3a1a84794be9bf0f0c9a8090ddefabdb3bc6131916947644df0210eb9acb340f57e066d90b3f5cfb20b7973dc23442058994867f8cce10a471a
7
+ data.tar.gz: 0310da19423cbe55c591f72108bc666ea3311cce9eb6f958836423ba96a3a71977d25c4dcf8939dc3197756d27ae1258020bba17ad94e066841b86e80cd50d99
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --colour
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1.2
5
+
6
+ before_install: 'gem install bundler -v 1.7'
7
+
8
+ script: 'CODECLIMATE_REPO_TOKEN=830e010682e6f678daaf56d47bd46eb9dc2e879a9305efd231baba6fec0df541 bundle exec rake'
9
+
10
+ addons:
11
+ code_climate:
12
+ repo_token: 830e010682e6f678daaf56d47bd46eb9dc2e879a9305efd231baba6fec0df541
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in extracted_validator.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sergey Gernyak
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.
@@ -0,0 +1,94 @@
1
+ # ExtractedValidator
2
+
3
+ [![Code Climate](https://codeclimate.com/github/alterego-labs/extracted_validator/badges/gpa.svg)](https://codeclimate.com/github/alterego-labs/extracted_validator)
4
+ [![Test Coverage](https://codeclimate.com/github/alterego-labs/extracted_validator/badges/coverage.svg)](https://codeclimate.com/github/alterego-labs/extracted_validator)
5
+ [![Build Status](https://travis-ci.org/alterego-labs/extracted_validator.svg)](https://travis-ci.org/alterego-labs/extracted_validator)
6
+
7
+ ActiveRecord and ActiveModel, as parts of Rails, are powerfull and cool
8
+ things. They can do many works with simple API call. But coupling of model,
9
+ validations and business logic in one place is awkward and ugly. Espessialy
10
+ in big projects. And sometimes simple creating record for testing is painfull
11
+ and I want to cry :cry:.
12
+
13
+ And we must make our life and coding easier! Extract validations rules!
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'extracted_validator'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install extracted_validator
30
+
31
+ ## Usage
32
+
33
+ ### Basic usage
34
+
35
+ Validator is defining as separated class. For example:
36
+
37
+ ```ruby
38
+ class PostValidator < ExtractedValidator::Base
39
+ validate :title_for_blank
40
+
41
+ def title_for_blank
42
+ add_error('Title must be set!') if title.blank?
43
+ end
44
+ end
45
+ ```
46
+
47
+ Then pass model that must be validated to initializer and use same API
48
+ as well as you use validations on model directly.
49
+
50
+ ```ruby
51
+ post = Post.new title: 'New amazing post'
52
+
53
+ validator = PostValidator.new(post)
54
+
55
+ # Check if model valid
56
+ validator.valid?
57
+
58
+ # Return Errors instance
59
+ validator.errors
60
+
61
+ # Return errors messages
62
+ validator.errors_messages
63
+ validator.errors_full_messages
64
+ ```
65
+
66
+ ### Model class required validations
67
+
68
+ Some validations is required model class for do work well. For example,
69
+ `validates_uniqueness_of` and others. To consider this cases we must pass
70
+ model class like in the next example:
71
+
72
+ ```ruby
73
+ class PostValidator < ExtractedValidator::Base[Post]
74
+ validates :title, presence: true
75
+ validates_uniqueness_of :title
76
+ end
77
+ ```
78
+
79
+ ### Place for validators
80
+
81
+ IMHO validators must be contained in `app/validators` folder. Don't forget
82
+ to add this path for autoloading in `config/application.rb`
83
+
84
+ ```ruby
85
+ config.autoload_paths += %W(#{config.root}/app/validators)
86
+ ```
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it ( https://github.com/alterego-labs/extracted_validator/fork )
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create a new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ require 'rubygems/specification'
4
+
5
+ task default: :spec
6
+ desc "Run specs"
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.pattern = FileList['spec/**/*_spec.rb']
9
+ t.rspec_opts = %w(--color)
10
+ end
11
+
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'extracted_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "extracted_validator"
8
+ spec.version = ExtractedValidator::VERSION
9
+ spec.authors = ["Sergey Gernyak"]
10
+ spec.email = ["sergeg1990@gmail.com"]
11
+ spec.summary = %q{Simple way to do validations more clear :)}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/alterego-labs/extracted_validator"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rails", ">= 3.2"
24
+
25
+ spec.add_development_dependency "rspec", "3.2.0"
26
+ spec.add_development_dependency "rspec-its", "1.2.0"
27
+ spec.add_development_dependency "pry-nav", "0.2.3"
28
+ spec.add_development_dependency "codeclimate-test-reporter", "0.4.0"
29
+ spec.add_development_dependency "sqlite3"
30
+ end
@@ -0,0 +1,5 @@
1
+ require "extracted_validator/version"
2
+
3
+ module ExtractedValidator
4
+ autoload :Base, 'extracted_validator/base'
5
+ end
@@ -0,0 +1,41 @@
1
+ require 'delegate'
2
+ require 'active_record'
3
+ require 'active_support/core_ext/module/delegation'
4
+
5
+ module ExtractedValidator
6
+ class Base < SimpleDelegator
7
+ include ActiveRecord::Validations
8
+
9
+ alias_method :model, :__getobj__
10
+
11
+ delegate :errors, to: :model
12
+
13
+ delegate :messages,
14
+ :full_messages,
15
+ to: :errors, prefix: true
16
+
17
+ def method_missing(name, *args, &block)
18
+ model.send name, *args, &block
19
+ end
20
+
21
+ def add_error(message, scope: :base)
22
+ errors[scope] << message
23
+ end
24
+
25
+ def self.method_missing(name, *args, &block)
26
+ model_class.send name, *args, &block
27
+ end
28
+
29
+ def self.[](model)
30
+ Class.new(self) do
31
+ define_singleton_method :model_class do
32
+ model
33
+ end
34
+
35
+ define_singleton_method :model_name do
36
+ ActiveModel::Name.new(model)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module ExtractedValidator
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ class AttrConfirmationSpecValidator < ExtractedValidator::Base[User]
4
+ delegate :password_confirmation, to: :model
5
+ delegate :password_confirmation=, to: :model
6
+
7
+ validates :password, confirmation: true
8
+ end
9
+
10
+ describe 'Testing confirmation validation' do
11
+ subject(:validator) { AttrConfirmationSpecValidator.new(user_params) }
12
+
13
+ context 'when original attr value equals confirmation' do
14
+ let(:user_params) { User.new(password: 'some_password', password_confirmation: 'some_password') }
15
+
16
+ its(:valid?) { is_expected.to be_truthy }
17
+ end
18
+
19
+ context 'when original attr value not equals confirmation' do
20
+ let(:user_params) { User.new(password: 'some_password', password_confirmation: 'some_another_password') }
21
+
22
+ its(:valid?) { is_expected.to be_falsey }
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ class AttrPresenceSpec < ExtractedValidator::Base[User]
4
+ validates :password, presence: true
5
+ end
6
+
7
+ describe 'Testing presence validation' do
8
+ subject(:validator) { AttrPresenceSpec.new(user) }
9
+
10
+ context 'when attr value is not set' do
11
+ let(:user) { User.new }
12
+
13
+ its(:valid?) { is_expected.to be_falsey }
14
+ end
15
+
16
+ context 'when attr value is set' do
17
+ let(:user) { User.new(password: 'some text') }
18
+
19
+ its(:valid?) { is_expected.to be_truthy }
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe ExtractedValidator::Base do
4
+ subject(:base_validator) { described_class.new(model) }
5
+
6
+ let(:model) { double(:model) }
7
+
8
+ it { is_expected.to respond_to :model }
9
+
10
+ describe '#model' do
11
+ it 'returns passed model' do
12
+ expect(base_validator).to eq model
13
+ end
14
+ end
15
+
16
+ describe '#errors' do
17
+ it 'delegates errors to model' do
18
+ expect(model).to receive(:errors)
19
+ base_validator.errors
20
+ end
21
+ end
22
+
23
+ describe '#errors_messages' do
24
+ it 'delegates messages to errors' do
25
+ expect(model).to receive_message_chain(:errors, :messages)
26
+ base_validator.errors_messages
27
+ end
28
+ end
29
+
30
+ describe '#errors_full_messages' do
31
+ it 'delegates full_messages to errors' do
32
+ expect(model).to receive_message_chain :errors, :full_messages
33
+ base_validator.errors_full_messages
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+
4
+ class PostValidator < ExtractedValidator::Base
5
+ validate :title_for_blank
6
+
7
+ def title_for_blank
8
+ add_error('Title must be set!') if title.blank?
9
+ end
10
+ end
11
+
12
+ describe 'Basic usage' do
13
+ subject(:validator) { PostValidator.new(post) }
14
+
15
+ let(:post) { Post.new }
16
+
17
+ context 'provides validations' do
18
+ it { is_expected.to respond_to :valid? }
19
+
20
+ its(:valid?) { is_expected.to be_falsey }
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+
4
+ class AnotherPostValidator < ExtractedValidator::Base[Post]
5
+ validates :title, presence: true
6
+ validates_uniqueness_of :title
7
+ end
8
+
9
+ describe 'Model required usage' do
10
+ subject(:validator) { AnotherPostValidator.new(post) }
11
+
12
+ let(:post) { Post.new }
13
+
14
+ context 'provides validations' do
15
+ it { is_expected.to respond_to :valid? }
16
+
17
+ its(:valid?) { is_expected.to be_falsey }
18
+ end
19
+
20
+ describe '.model_class' do
21
+ it 'returns passed model class' do
22
+ expect(validator.class.model_class).to eq Post
23
+ end
24
+ end
25
+
26
+ context 'test uniqueness validation' do
27
+ before do
28
+ Post.create title: 'Some title'
29
+ end
30
+
31
+ let(:post) { Post.new(title: 'Some title') }
32
+
33
+ its(:valid?) { is_expected.to be_falsey }
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ $TESTING = true
2
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ $CODECLIMATE_REPO_TOKEN = '830e010682e6f678daaf56d47bd46eb9dc2e879a9305efd231baba6fec0df541'
5
+
6
+ require 'rspec/its'
7
+ require 'extracted_validator'
8
+ require 'codeclimate-test-reporter'
9
+ require 'active_record'
10
+
11
+ require 'pry-nav'
12
+
13
+ CodeClimate::TestReporter.start
14
+
15
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
16
+
17
+ DB_FILE = 'tmp/test_db'
18
+
19
+ FileUtils.mkdir_p File.dirname(DB_FILE)
20
+ FileUtils.rm_f DB_FILE
21
+
22
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: DB_FILE
23
+ ActiveRecord::Base.connection.execute 'CREATE TABLE posts (id INTEGER NOT NULL PRIMARY KEY, title VARCHAR(125))'
24
+ ActiveRecord::Base.connection.execute 'CREATE TABLE users (id INTEGER NOT NULL PRIMARY KEY, password VARCHAR(125))'
25
+
26
+ class Post < ActiveRecord::Base
27
+ end
28
+
29
+ class User < ActiveRecord::Base
30
+ attr_accessor :password_confirmation
31
+ end
32
+
33
+ RSpec.configure do |config|
34
+ config.mock_with :rspec
35
+
36
+ config.filter_run focus: true
37
+ config.run_all_when_everything_filtered = true
38
+
39
+ config.order = 'random'
40
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extracted_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Gernyak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-03 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-its
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.2.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-nav
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.2.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.2.3
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.4.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.4.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: ''
126
+ email:
127
+ - sergeg1990@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - extracted_validator.gemspec
140
+ - lib/extracted_validator.rb
141
+ - lib/extracted_validator/base.rb
142
+ - lib/extracted_validator/version.rb
143
+ - spec/lib/extracted_validator/attr_confirmation_spec.rb
144
+ - spec/lib/extracted_validator/attr_presence_spec.rb
145
+ - spec/lib/extracted_validator/base_spec.rb
146
+ - spec/lib/extracted_validator/basic_usage_spec.rb
147
+ - spec/lib/extracted_validator/model_required_spec.rb
148
+ - spec/spec_helper.rb
149
+ homepage: https://github.com/alterego-labs/extracted_validator
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.4.4
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Simple way to do validations more clear :)
173
+ test_files:
174
+ - spec/lib/extracted_validator/attr_confirmation_spec.rb
175
+ - spec/lib/extracted_validator/attr_presence_spec.rb
176
+ - spec/lib/extracted_validator/base_spec.rb
177
+ - spec/lib/extracted_validator/basic_usage_spec.rb
178
+ - spec/lib/extracted_validator/model_required_spec.rb
179
+ - spec/spec_helper.rb