validacity 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff1728df7b05d4d9882b039884cba8985df95824810d8f64f2a363d6f109655a
4
- data.tar.gz: 1b2071f2cf21c10aeda419eb264e456942550a50fe79acb9fdaabbd7b3110304
3
+ metadata.gz: 66d3268535c64fc2c2771ef680fa9b58e999e74296ce83687309e7e63f9e0975
4
+ data.tar.gz: 65c93549bfb2dffc693f7dddddcf588e28595de5c10d8c56187492b10497a9a3
5
5
  SHA512:
6
- metadata.gz: 15d81cf9ad56fd8255984d73e98a1e825bd380f70b002324f22c7e292b098ee6ec01266b95cae5c90906418fc27f7c4f74974a680cbdb9780cc9a1cb4401b69c
7
- data.tar.gz: 9ac2b3080c9c2cd2cc34ad483b58961050d4bf63fca2c33020bc9bdbb185e8499959207fc202463dcd7c8a0bdcf458510d210e8b2fdc154f2ba5900f97aff302
6
+ metadata.gz: 7392af3b88ec32311a21f80932eb7ff4c798bc2cd5808b88a447fb06e9f2672b935a46bca7fce6080075ad4d3e1ff1ba87a62277ae0f336cde383c289b414fc7
7
+ data.tar.gz: 9451ed7db81476d1226be00706c6f1d5175e12e9b1f28a45c5da13444a6ffe630a65b3547b967f7d9d58096a27d818191cb8c45ace560b47fe06352a261cc390
data/README.md CHANGED
@@ -1,16 +1,26 @@
1
1
  # Validacity
2
2
 
3
- Move your validation logic out of your models or form objects.
3
+ Move validation definitions out of you models and form objects.
4
4
 
5
5
  ## Usage
6
6
 
7
- You can append validators on-fly and then remove some of them if not needed.
7
+ You can apply multiple sets of validation rules on-fly,
8
+ remove any them if not needed and re-validate object against
9
+ new rules afterwards.
8
10
 
9
- It can be very suitable if you need to validate different states of the same object differently. Say you're using a state machine and each state have it's own field set and the fields from other satates shouldn't be validated at this time.
11
+ By the way, the validation errors will be putten to the object
12
+ so all your view helpers will be available to display the error list.
10
13
 
11
- Or perhaps you just have to many constructions like `validate :blablabla, presence: true` in your model and you want to get rid of it.
14
+ This approach will suitable if you need to validate different states
15
+ of the same object differently. Say you're using a state machine
16
+ and each state has it's own field set and the fields from the other
17
+ satates shouldn't be validated at this time.
12
18
 
13
- All you need is to add a concern to your model:
19
+ Or perhaps you just have to many constructions like
20
+ `validate :blablabla, presence: true` in your model class and you want to
21
+ get rid of it so you can focus on buisiness logic.
22
+
23
+ You can add a concern to your model so it becomes **validatable**:
14
24
 
15
25
  ```ruby
16
26
 
@@ -23,52 +33,78 @@ end
23
33
 
24
34
  ```
25
35
 
26
- And generate a validator:
36
+ Generate new validation:
37
+
38
+ ```bash
39
+
40
+ $ bundle exec rails g validacity:validation UserPersonalData
41
+
42
+ ```
27
43
 
28
44
  ```ruby
29
45
 
30
- # app/validators/user_personal_data_validator.rb
46
+ # app/validations/user_personal_data_validation.rb
31
47
 
32
- class UserPersonalDataValidator
48
+ class UserPersonalDataValidation
33
49
  validate :name, presence_of: true
34
- # ...a ton of different validators
50
+ # ...a ton of different validations
35
51
  end
36
52
 
37
53
  ```
38
54
 
39
- Now call validator with the regular methods like:
55
+ Now let's try to validate your user object:
40
56
 
41
57
 
42
58
  ```ruby
43
59
 
44
- user.valid?
60
+ user = User.new
61
+
62
+ user.valid? # => false
63
+
64
+ user.name = "John"
65
+
66
+ uesr.valid? # => true
45
67
 
46
68
  ```
47
69
 
48
70
  ## Installation
71
+
49
72
  Add this line to your application's Gemfile:
50
73
 
51
74
  ```ruby
75
+
52
76
  gem 'validacity'
77
+
53
78
  ```
54
79
 
55
80
  And then execute:
81
+
56
82
  ```bash
83
+
57
84
  $ bundle
85
+
58
86
  ```
59
87
 
60
- Now run the validator installation command:
88
+ Now run the validation installation command:
89
+
61
90
  ```bash
91
+
62
92
  $ bundle exec rails g validacity:install
93
+
63
94
  ```
64
95
 
65
- And the event validator:
96
+ And the event validation:
97
+
66
98
  ```bash
67
- $ bundle exec rails g validacity:validator Event
99
+
100
+ $ bundle exec rails g validacity:validation Event
101
+
68
102
  ```
69
103
 
70
104
  ## Contributing
105
+
71
106
  Contribution directions go here.
72
107
 
73
108
  ## License
109
+
74
110
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -17,16 +17,16 @@ module Validacity
17
17
  require_dependency "validacity"
18
18
 
19
19
  Validacity.configure do |config|
20
- config.search_paths "#{__dir__}/../app/validators/**/*_validator.rb"
20
+ config.search_paths "#{__dir__}/../../app/validations/**/*_validation.rb"
21
21
  end
22
22
  RUBY
23
23
  create_file "config/initializers/validacity_initializer.rb", content
24
24
  end
25
25
 
26
26
  def copy_application_policy
27
- validator_file = File.join("app/validators", class_path,
28
- "application_validator.rb")
29
- template "application_validator.rb", validator_file
27
+ validation_file = File.join("app/validations", class_path,
28
+ "application_validation.rb")
29
+ template "application_validation.rb", validation_file
30
30
  end
31
31
 
32
32
  include Thor::Actions
@@ -0,0 +1,4 @@
1
+ <% module_namespacing do -%>
2
+ class ApplicationValidation < Validacity::BaseValidation
3
+ end
4
+ <% end -%>
@@ -0,0 +1,4 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Validation < ApplicationValidation
3
+ end
4
+ <% end -%>
@@ -0,0 +1,13 @@
1
+ module Validacity
2
+ module Generators
3
+ class ValidationGenerator < ::Rails::Generators::NamedBase
4
+ source_root File.expand_path("templates", __dir__)
5
+
6
+ def create_validation
7
+ validation_file = File.join("app/validations", class_path,
8
+ "#{file_name}_validation.rb")
9
+ template "validation.rb", validation_file
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/validacity.rb CHANGED
@@ -3,7 +3,7 @@ require "active_model"
3
3
 
4
4
  require "validacity/version"
5
5
  require "validacity/configuration"
6
- require "validacity/base_validator"
6
+ require "validacity/base_validation"
7
7
  require "validacity/validatable"
8
8
 
9
9
  module Validacity
@@ -16,23 +16,23 @@ module Validacity
16
16
  yield configuration if block_given?
17
17
  end
18
18
 
19
- def validators
20
- @_validators ||= {}
19
+ def validations
20
+ @_validations ||= {}
21
21
  end
22
22
 
23
- def find_validator(name)
24
- validator = validators[name]
25
- return validator if validator
23
+ def find_validation(name)
24
+ validation = validations[name]
25
+ return validation if validation
26
26
 
27
- reload_validators
28
- validators[name]
27
+ reload_validations
28
+ validations[name]
29
29
  end
30
30
 
31
31
  private
32
32
 
33
- def reload_validators
34
- Dir.glob(configuration.search_paths) do |validator|
35
- send(require_method, validator)
33
+ def reload_validations
34
+ Dir.glob(configuration.search_paths) do |validation|
35
+ send(require_method, validation)
36
36
  end
37
37
  end
38
38
 
@@ -1,16 +1,16 @@
1
1
  module Validacity
2
- class BaseValidator
2
+ class BaseValidation
3
3
  include ::ActiveModel::Validations
4
4
 
5
5
  class << self
6
6
  def inherited(klass)
7
- Validacity.validators[validator_name(klass)] = klass
7
+ Validacity.validations[validation_name(klass)] = klass
8
8
  end
9
9
 
10
10
  private
11
11
 
12
- def validator_name(klass)
13
- klass.name.gsub(/Validator$/, "").underscore.to_sym
12
+ def validation_name(klass)
13
+ klass.name.gsub(/Validation$/, "").underscore.to_sym
14
14
  end
15
15
  end
16
16
 
@@ -4,44 +4,44 @@ module Validacity
4
4
 
5
5
  class_methods do
6
6
  def validations(*names)
7
- names.each { |n| validacity_validators << n }
7
+ names.each { |n| validacity_validations << n }
8
8
  end
9
9
 
10
- def validacity_validators
11
- @_validacity_validators ||= Set.new
10
+ def validacity_validations
11
+ @_validacity_validations ||= Set.new
12
12
  end
13
13
 
14
14
  def new(resource)
15
15
  instance = super
16
- instance.validacity_validators.merge(validacity_validators.dup)
16
+ instance.validacity_validations.merge(validacity_validations.dup)
17
17
  instance
18
18
  end
19
19
  end
20
20
 
21
- def validacity_validators
22
- @_validacity_validators ||= Set.new
21
+ def validacity_validations
22
+ @_validacity_validations ||= Set.new
23
23
  end
24
24
 
25
25
  def valid?(context = nil)
26
26
  super
27
- validacity_validators_run(context)
27
+ validacity_validations_run(context)
28
28
  errors.empty?
29
29
  end
30
30
 
31
31
  private
32
32
 
33
- def validacity_validators_run(_context = nil)
34
- each_validator_instance(&:validate)
33
+ def validacity_validations_run(_context = nil)
34
+ each_validation_instance(&:validate)
35
35
  end
36
36
 
37
- def each_validator_instance
38
- @_validacity_validators_instances ||= {}
39
- validacity_validators.each do |name|
40
- unless (validator = @_validacity_validators_instances[name])
41
- validator = Validacity.find_validator(name).new(self)
42
- @_validacity_validators_instances[name] = validator
37
+ def each_validation_instance
38
+ @_validacity_validations_instances ||= {}
39
+ validacity_validations.each do |name|
40
+ unless (validation = @_validacity_validations_instances[name])
41
+ validation = Validacity.find_validation(name).new(self)
42
+ @_validacity_validations_instances[name] = validation
43
43
  end
44
- yield(validator)
44
+ yield(validation)
45
45
  end
46
46
  end
47
47
  end
@@ -1,3 +1,3 @@
1
1
  module Validacity
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validacity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Zinovyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-03 00:00:00.000000000 Z
11
+ date: 2018-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -122,11 +122,11 @@ files:
122
122
  - README.md
123
123
  - Rakefile
124
124
  - lib/generators/validacity/install_generator.rb
125
- - lib/generators/validacity/templates/application_validator.rb
126
- - lib/generators/validacity/templates/validator.rb
127
- - lib/generators/validacity/validator_generator.rb
125
+ - lib/generators/validacity/templates/application_validation.rb
126
+ - lib/generators/validacity/templates/validation.rb
127
+ - lib/generators/validacity/validation_generator.rb
128
128
  - lib/validacity.rb
129
- - lib/validacity/base_validator.rb
129
+ - lib/validacity/base_validation.rb
130
130
  - lib/validacity/configuration.rb
131
131
  - lib/validacity/railtie.rb
132
132
  - lib/validacity/validatable.rb
@@ -1,4 +0,0 @@
1
- <% module_namespacing do -%>
2
- class ApplicationValidator < Validacity::BaseValidator
3
- end
4
- <% end -%>
@@ -1,4 +0,0 @@
1
- <% module_namespacing do -%>
2
- class <%= class_name %>Validator < ApplicationValidator
3
- end
4
- <% end -%>
@@ -1,13 +0,0 @@
1
- module Validacity
2
- module Generators
3
- class ValidatorGenerator < ::Rails::Generators::NamedBase
4
- source_root File.expand_path("templates", __dir__)
5
-
6
- def create_validator
7
- validator_file = File.join("app/validators", class_path,
8
- "#{file_name}_validator.rb")
9
- template "validator.rb", validator_file
10
- end
11
- end
12
- end
13
- end