validacity 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
+ SHA256:
3
+ metadata.gz: 37e0d8cfd4674671362980b8431da1c4ed1714e9c9217ec6453c7eb0179db44c
4
+ data.tar.gz: 6382080b9a6bcc1ec95c31539ba5e597fc7b9029e185aaae5527e31a1d2ef70f
5
+ SHA512:
6
+ metadata.gz: b3e8278b9b416944cffef47ae862b447d92d1ec9ec405f0f684cf0c95cfca8143b1864093ce06c38e9f6f9f52ce96e032ff8a34fd762a29173515d808881762e
7
+ data.tar.gz: 1c4d95d4ff6742ae90992d840e9e5b0b2561b8718f60068c8a453b544e0550ca83cb8f1c75af8e7e2be6ed7150539ef7217dd5be3becf34aaf241d554a854c6b
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Ivan Zinovyev
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Validacity
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'validacity'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install validacity
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Validacity'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ module Validacity
2
+ module Generators
3
+ class ValidacityGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ def create_initializer_file
7
+ content = <<-'RUBY'
8
+ Validacity.configure do |config|
9
+ config.search_paths "#{namespaced_path}/app/validators/**/*_validator.rb"
10
+ end
11
+ RUBY
12
+ create_file "#{namespaced_path}/config/initializers/validacity_initializer.rb",
13
+ content
14
+ end
15
+
16
+ def copy_application_policy
17
+ template "application_validator.rb",
18
+ "app/validators/application_validator.rb"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ <% module_namespacing do -%>
2
+ class ApplicationValidator < Validacity::BaseValidator
3
+ end
4
+ <% end -%>
@@ -0,0 +1,4 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Validator < ApplicationValidator
3
+ end
4
+ <% end -%>
@@ -0,0 +1,14 @@
1
+ module Validacity
2
+ module Generators
3
+ class ValidacityGenerator < ::Rails::Generators::NamedBase
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ def create_validator
7
+ validator_file = File.join("app/validators",
8
+ class_path,
9
+ "#{file_name}_validator.rb")
10
+ template "validator.rb", validator_file
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :validacity do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,47 @@
1
+ module Validacity
2
+ class BaseValidator
3
+ include ::ActiveModel::Validations
4
+
5
+ class << self
6
+ def inherited(klass)
7
+ Validacity.validators[validator_name(klass)] = klass
8
+ end
9
+
10
+ private
11
+
12
+ def validator_name(klass)
13
+ klass.name.gsub(/Validator$/, "").underscore.to_sym
14
+ end
15
+ end
16
+
17
+ def initialize(resource)
18
+ @resource = resource
19
+ end
20
+
21
+ def respond_to?(name)
22
+ @resource.respond_to?(method)
23
+ end
24
+
25
+ def respond_to_missing?
26
+ true
27
+ end
28
+
29
+ def method_missing(name, *attrs, &block)
30
+ @resource.public_send(name, *attrs, &block)
31
+ end
32
+
33
+ def validate
34
+ return true if valid?
35
+
36
+ errors.each do |field, error|
37
+ @resource.errors[field] << error
38
+ end
39
+
40
+ false
41
+ end
42
+
43
+ def validate!
44
+ validate or raise ActiveModel::ValidationError, @resource
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ module Validacity
2
+ class Configuration
3
+ attr_reader :options
4
+
5
+ def initialize
6
+ @options = {}
7
+ end
8
+
9
+ def search_paths(*paths)
10
+ @options[:search_paths] ||= []
11
+ return @options[:search_paths] if paths.empty?
12
+
13
+ @options[:search_paths].concat(paths)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ module Validacity
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,30 @@
1
+ module Validacity
2
+ module Validatable
3
+ extend ActiveSupport::Concern
4
+
5
+ def valid?
6
+ super
7
+ validacity_validators_run
8
+ errors.empty?
9
+ end
10
+
11
+ def validacity_validators_run
12
+ each_validator_instance(&:validate)
13
+ end
14
+
15
+ def each_validator_instance
16
+ @_validacity_validators_instances ||= {}
17
+ validacity_validators.each do |name|
18
+ unless (validator = @_validacity_validators_instances[name])
19
+ validator = Validacity.find_validator(name).new(self)
20
+ @_validacity_validators_instances[name] = validator
21
+ end
22
+ yield(validator)
23
+ end
24
+ end
25
+
26
+ def validacity_validators
27
+ @_validacity_validators ||= Set.new
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Validacity
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/validacity.rb ADDED
@@ -0,0 +1,47 @@
1
+ require "active_support"
2
+ require "active_model"
3
+
4
+ require "validacity/version"
5
+ require "validacity/configuration"
6
+ require "validacity/base_validator"
7
+ require "validacity/validatable"
8
+
9
+ module Validacity
10
+ class << self
11
+ def configuration
12
+ @configuration ||= Validacity::Configuration.new
13
+ end
14
+
15
+ def configure
16
+ yield configuration if block_given?
17
+ end
18
+
19
+ def validators
20
+ @_validators ||= {}
21
+ end
22
+
23
+ def find_validator(name)
24
+ validator = validators[name]
25
+ return validator if validator
26
+
27
+ reload_validators
28
+ validators[name]
29
+ end
30
+
31
+ private
32
+
33
+ def reload_validators
34
+ Dir.glob(configuration.search_paths) do |validator|
35
+ send(require_method, validator)
36
+ end
37
+ end
38
+
39
+ def require_method
40
+ @_require_method ||= if Kernel.respond_to?(:require_dependency)
41
+ :require_dependency
42
+ else
43
+ :require
44
+ end
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validacity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Zinovyev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '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'
111
+ description: 'Validacity adds an extra layer of validation objects where all your
112
+ validation logic can be moved to.
113
+
114
+ '
115
+ email:
116
+ - vanyazin@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - MIT-LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - lib/generators/validacity/install_generator.rb
125
+ - lib/generators/validacity/templates/application_validator.rb
126
+ - lib/generators/validator/templates/validator.rb
127
+ - lib/generators/validator/validator_generator.rb
128
+ - lib/tasks/validacity_tasks.rake
129
+ - lib/validacity.rb
130
+ - lib/validacity/base_validator.rb
131
+ - lib/validacity/configuration.rb
132
+ - lib/validacity/railtie.rb
133
+ - lib/validacity/validatable.rb
134
+ - lib/validacity/version.rb
135
+ homepage: https://github.com/zinovyev/validacity
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.7.3
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Validation objects for Rails
159
+ test_files: []