mv-core 2.0.2 → 2.1.0
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 +4 -4
- data/README.md +30 -10
- data/lib/mv/core/active_record/connection_adapters/abstract_adapter_decorator.rb +9 -2
- data/lib/mv/core/active_record/connection_adapters/table_definition_decorator.rb +8 -1
- data/lib/mv/core/db/helpers/column_validators.rb +4 -3
- data/lib/mv/core/services/parse_validation_options.rb +28 -0
- data/lib/mv/core/validation/absence.rb +1 -1
- data/lib/mv/core/validation/builder/custom.rb +26 -0
- data/lib/mv/core/validation/builder/factory.rb +3 -1
- data/lib/mv/core/validation/custom.rb +25 -0
- data/lib/mv/core/validation/exclusion.rb +2 -0
- data/lib/mv/core/validation/factory.rb +14 -5
- data/lib/mv/core/validation/inclusion.rb +2 -0
- data/lib/mv/core/validation/length.rb +5 -0
- data/lib/mv/core/validation/presence.rb +1 -1
- data/lib/mv/core/validation/uniqueness.rb +2 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24ffb629e586415bc6671bc83c69ab96f61c1133
|
4
|
+
data.tar.gz: 1a7d2b792298becc6cba981959ad868887921264
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d38da1fab2a62805a97466da3c5ee6d43596d1ff207bf8fb4343983df89b56b524d6a83be938557a36a3b8df8e3e9266b4c92c05898961d376e82f839206074
|
7
|
+
data.tar.gz: 20c0fcb064136a2bf20623b96c4137557f82e54b32b075f594b6a89c8779b047f206c4c2b7bdadc73734c6a3d797272d8b3afed80750e0643ae73f6e7ae82c47
|
data/README.md
CHANGED
@@ -61,7 +61,7 @@ This gem is not intended to be installed directly and referenced from within the
|
|
61
61
|
Update validator definition:
|
62
62
|
|
63
63
|
```ruby
|
64
|
-
validates :table_name, :str_column,
|
64
|
+
validates :table_name, :str_column, exclusion: { in: [1,2,3] }
|
65
65
|
```
|
66
66
|
|
67
67
|
Remove existing validators:
|
@@ -79,15 +79,13 @@ This gem is not intended to be installed directly and referenced from within the
|
|
79
79
|
as trigger:
|
80
80
|
|
81
81
|
```ruby
|
82
|
-
validates :table_name, :str_column,
|
83
|
-
as: :trigger }
|
82
|
+
validates :table_name, :str_column, uniqueness: { as: :trigger }
|
84
83
|
```
|
85
84
|
|
86
85
|
as check constraint:
|
87
86
|
|
88
87
|
```ruby
|
89
|
-
validates :table_name, :str_column,
|
90
|
-
as: :check }
|
88
|
+
validates :table_name, :str_column, uniqueness: { as: :check }
|
91
89
|
```
|
92
90
|
|
93
91
|
Also there is possibility to define when validations should occur:
|
@@ -95,18 +93,40 @@ This gem is not intended to be installed directly and referenced from within the
|
|
95
93
|
when new record created:
|
96
94
|
|
97
95
|
```ruby
|
98
|
-
validates :table_name, :str_column,
|
99
|
-
on: :create }
|
96
|
+
validates :table_name, :str_column, uniqueness: { on: :create }
|
100
97
|
```
|
101
98
|
|
102
99
|
or when existing record updated:
|
103
100
|
|
104
101
|
```ruby
|
105
|
-
validates :table_name, :str_column,
|
106
|
-
on: :update }
|
102
|
+
validates :table_name, :str_column, uniqueness: { on: :update }
|
107
103
|
```
|
108
104
|
|
109
|
-
|
105
|
+
And if you need to define some custom validation you can use custom validation (version >= 2.1 is required):
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
validates :table_name, :str_column,
|
109
|
+
custom: { statement: 'LENGTH(TRIM({str_column})) > 10',
|
110
|
+
on: :update }
|
111
|
+
```
|
112
|
+
|
113
|
+
as result only values with length greater than 10 will be allowed and that condition will be implemented inside ON UPDATE trigger
|
114
|
+
|
115
|
+
Almost all validations supports shorter notation (simplification) that is not compatible with ActiveRecord validation but much shorter (version >= 2.1 is required):
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
validates :table_name, :str_column, uniqueness: true, presence: true
|
119
|
+
```
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
validates :table_name, :str_column, length: 1..3
|
123
|
+
```
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
validates :table_name, :str_column, custom: 'LENGTH(TRIM({str_column})) > 10'
|
127
|
+
```
|
128
|
+
|
129
|
+
Supported validators, simplification and their properties might vary from one db driver to another. See detailed properties description in correspondent driver section.
|
110
130
|
|
111
131
|
# Maintenance tasks
|
112
132
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'mv/core/migration/base'
|
2
|
+
require 'mv/core/services/parse_validation_options'
|
2
3
|
|
3
4
|
module Mv
|
4
5
|
module Core
|
@@ -6,7 +7,7 @@ module Mv
|
|
6
7
|
module ConnectionAdapters
|
7
8
|
module AbstractAdapterDecorator
|
8
9
|
def add_column table_name, column_name, type, opts
|
9
|
-
Mv::Core::Migration::Base.add_column(table_name, column_name, opts
|
10
|
+
Mv::Core::Migration::Base.add_column(table_name, column_name, params(opts))
|
10
11
|
|
11
12
|
super
|
12
13
|
end
|
@@ -24,7 +25,7 @@ module Mv
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def change_column table_name, column_name, type, opts
|
27
|
-
Mv::Core::Migration::Base.change_column(table_name, column_name, opts
|
28
|
+
Mv::Core::Migration::Base.change_column(table_name, column_name, params(opts))
|
28
29
|
|
29
30
|
super
|
30
31
|
end
|
@@ -44,6 +45,12 @@ module Mv
|
|
44
45
|
|
45
46
|
super
|
46
47
|
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def params opts
|
52
|
+
Mv::Core::Services::ParseValidationOptions.new(opts).execute
|
53
|
+
end
|
47
54
|
end
|
48
55
|
end
|
49
56
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'mv/core/migration/base'
|
2
|
+
require 'mv/core/services/parse_validation_options'
|
2
3
|
|
3
4
|
module Mv
|
4
5
|
module Core
|
@@ -6,7 +7,7 @@ module Mv
|
|
6
7
|
module ConnectionAdapters
|
7
8
|
module TableDefinitionDecorator
|
8
9
|
def column column_name, type, opts = {}
|
9
|
-
Mv::Core::Migration::Base.add_column(name, column_name, opts
|
10
|
+
Mv::Core::Migration::Base.add_column(name, column_name, params(opts))
|
10
11
|
|
11
12
|
super
|
12
13
|
end
|
@@ -14,6 +15,12 @@ module Mv
|
|
14
15
|
def validates column_name, opts
|
15
16
|
Mv::Core::Migration::Base.change_column(name, column_name, opts)
|
16
17
|
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def params opts
|
22
|
+
Mv::Core::Services::ParseValidationOptions.new(opts).execute
|
23
|
+
end
|
17
24
|
end
|
18
25
|
end
|
19
26
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'mv/core/validation/factory'
|
1
2
|
require 'mv/core/db/helpers/table_validators'
|
2
3
|
|
3
4
|
module Mv
|
@@ -27,7 +28,7 @@ module Mv
|
|
27
28
|
return delete_validators(column_validators.where(validation_type: validation_type)) if opts == false
|
28
29
|
|
29
30
|
column_validators.where(validation_type: validation_type).first_or_initialize.tap do |validator|
|
30
|
-
validator.options = normalize_opts(opts)
|
31
|
+
validator.options = normalize_opts(validation_type, opts)
|
31
32
|
end.save!
|
32
33
|
end
|
33
34
|
|
@@ -37,8 +38,8 @@ module Mv
|
|
37
38
|
|
38
39
|
private
|
39
40
|
|
40
|
-
def normalize_opts opts
|
41
|
-
|
41
|
+
def normalize_opts validation_type, opts
|
42
|
+
Mv::Core::Validation::Factory.create_validation(table_name, column_name, validation_type, opts).options
|
42
43
|
end
|
43
44
|
|
44
45
|
def raise_column_validation_error validation_type, opts
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'mv/core/validation/factory'
|
2
|
+
|
3
|
+
module Mv
|
4
|
+
module Core
|
5
|
+
module Services
|
6
|
+
class ParseValidationOptions
|
7
|
+
attr_reader :opts
|
8
|
+
|
9
|
+
def initialize(opts)
|
10
|
+
@opts = opts || {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
validates = opts.delete(:validates) || opts.delete("validates")
|
15
|
+
|
16
|
+
return validates if validates.is_a?(Hash)
|
17
|
+
return { custom: validates } if validates.present?
|
18
|
+
|
19
|
+
Mv::Core::Validation::Factory.registered_validations.inject({}) do |res, validation_type|
|
20
|
+
validation_options = opts.delete(validation_type.to_sym) || opts.delete(validation_type.to_s)
|
21
|
+
res[validation_type] = validation_options if validation_options
|
22
|
+
res
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'mv/core/validation/builder/base'
|
2
|
+
|
3
|
+
module Mv
|
4
|
+
module Core
|
5
|
+
module Validation
|
6
|
+
module Builder
|
7
|
+
class Custom < Base
|
8
|
+
delegate :statement, to: :validation
|
9
|
+
|
10
|
+
def conditions
|
11
|
+
[{
|
12
|
+
statement: apply_allow_nil_and_blank("(#{preprocess_statement})"),
|
13
|
+
message: message
|
14
|
+
}]
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def preprocess_statement
|
20
|
+
statement.gsub(/\{\s*#{column_name}\s*\}/, column_reference.to_s)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -4,6 +4,7 @@ require 'mv/core/validation/builder/length'
|
|
4
4
|
require 'mv/core/validation/builder/presence'
|
5
5
|
require 'mv/core/validation/builder/absence'
|
6
6
|
require 'mv/core/validation/builder/uniqueness'
|
7
|
+
require 'mv/core/validation/builder/custom'
|
7
8
|
|
8
9
|
module Mv
|
9
10
|
module Core
|
@@ -33,7 +34,8 @@ module Mv
|
|
33
34
|
Mv::Core::Validation::Length => Mv::Core::Validation::Builder::Length,
|
34
35
|
Mv::Core::Validation::Presence => Mv::Core::Validation::Builder::Presence,
|
35
36
|
Mv::Core::Validation::Absence => Mv::Core::Validation::Builder::Absence,
|
36
|
-
Mv::Core::Validation::Uniqueness => Mv::Core::Validation::Builder::Uniqueness
|
37
|
+
Mv::Core::Validation::Uniqueness => Mv::Core::Validation::Builder::Uniqueness,
|
38
|
+
Mv::Core::Validation::Custom => Mv::Core::Validation::Builder::Custom
|
37
39
|
}
|
38
40
|
end
|
39
41
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'mv/core/validation/base'
|
2
|
+
|
3
|
+
module Mv
|
4
|
+
module Core
|
5
|
+
module Validation
|
6
|
+
class Custom < Base
|
7
|
+
attr_reader :statement
|
8
|
+
|
9
|
+
validates :statement, presence: true
|
10
|
+
|
11
|
+
def initialize(table_name, column_name, opts)
|
12
|
+
opts = opts.is_a?(Hash) ? opts : { statement: opts }
|
13
|
+
|
14
|
+
super(table_name, column_name, opts)
|
15
|
+
|
16
|
+
@statement = opts.with_indifferent_access[:statement]
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_a
|
20
|
+
super + [statement.to_s]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -4,6 +4,7 @@ require 'mv/core/validation/inclusion'
|
|
4
4
|
require 'mv/core/validation/length'
|
5
5
|
require 'mv/core/validation/presence'
|
6
6
|
require 'mv/core/validation/absence'
|
7
|
+
require 'mv/core/validation/custom'
|
7
8
|
require 'mv/core/error'
|
8
9
|
|
9
10
|
module Mv
|
@@ -13,7 +14,7 @@ module Mv
|
|
13
14
|
include Singleton
|
14
15
|
|
15
16
|
def create_validation table_name, column_name, validation_type, opts
|
16
|
-
validation_class =
|
17
|
+
validation_class = factory_map[validation_type.to_sym]
|
17
18
|
|
18
19
|
raise Mv::Core::Error.new(table_name: table_name,
|
19
20
|
column_name: column_name,
|
@@ -25,7 +26,7 @@ module Mv
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def register_validation validation_type, klass
|
28
|
-
|
29
|
+
factory_map[validation_type.to_sym] = klass
|
29
30
|
end
|
30
31
|
|
31
32
|
def register_validations opts
|
@@ -34,20 +35,28 @@ module Mv
|
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
38
|
+
def registered_validations
|
39
|
+
factory_map.keys
|
40
|
+
end
|
41
|
+
|
37
42
|
class << self
|
38
|
-
delegate :create_validation,
|
43
|
+
delegate :create_validation,
|
44
|
+
:registered_validations,
|
45
|
+
:register_validation,
|
46
|
+
:register_validations, to: :instance
|
39
47
|
end
|
40
48
|
|
41
49
|
private
|
42
50
|
|
43
|
-
def
|
51
|
+
def factory_map
|
44
52
|
@factory_map ||= {
|
45
53
|
uniqueness: Mv::Core::Validation::Uniqueness,
|
46
54
|
exclusion: Mv::Core::Validation::Exclusion,
|
47
55
|
inclusion: Mv::Core::Validation::Inclusion,
|
48
56
|
length: Mv::Core::Validation::Length,
|
49
57
|
presence: Mv::Core::Validation::Presence,
|
50
|
-
absence: Mv::Core::Validation::Absence
|
58
|
+
absence: Mv::Core::Validation::Absence,
|
59
|
+
custom: Mv::Core::Validation::Custom
|
51
60
|
}
|
52
61
|
end
|
53
62
|
end
|
@@ -16,6 +16,11 @@ module Mv
|
|
16
16
|
validate :in_within_is_maximum_minimum_allowance
|
17
17
|
|
18
18
|
def initialize(table_name, column_name, opts)
|
19
|
+
unless opts.is_a?(Hash)
|
20
|
+
opts = { in: opts } if opts.respond_to?(:to_a)
|
21
|
+
opts = { is: opts } if opts.is_a?(Integer)
|
22
|
+
end
|
23
|
+
|
19
24
|
super(table_name, column_name, opts)
|
20
25
|
|
21
26
|
opts.with_indifferent_access.tap do |opts|
|
@@ -11,6 +11,8 @@ module Mv
|
|
11
11
|
validates :index_name, absence: { message: 'allowed when :as == :index' }, unless: :index?
|
12
12
|
|
13
13
|
def initialize(table_name, column_name, opts)
|
14
|
+
opts = opts == true ? {} : opts
|
15
|
+
|
14
16
|
super(table_name, column_name, opts)
|
15
17
|
|
16
18
|
@index_name = opts.with_indifferent_access[:index_name] || default_index_name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mv-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valeriy Prokopchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -218,6 +218,7 @@ files:
|
|
218
218
|
- lib/mv/core/services/create_migration_validators_table.rb
|
219
219
|
- lib/mv/core/services/delete_constraints.rb
|
220
220
|
- lib/mv/core/services/load_constraints.rb
|
221
|
+
- lib/mv/core/services/parse_validation_options.rb
|
221
222
|
- lib/mv/core/services/say_constraints_diff.rb
|
222
223
|
- lib/mv/core/services/show_constraints.rb
|
223
224
|
- lib/mv/core/services/synchronize_constraints.rb
|
@@ -226,12 +227,14 @@ files:
|
|
226
227
|
- lib/mv/core/validation/base.rb
|
227
228
|
- lib/mv/core/validation/builder/absence.rb
|
228
229
|
- lib/mv/core/validation/builder/base.rb
|
230
|
+
- lib/mv/core/validation/builder/custom.rb
|
229
231
|
- lib/mv/core/validation/builder/exclusion.rb
|
230
232
|
- lib/mv/core/validation/builder/factory.rb
|
231
233
|
- lib/mv/core/validation/builder/inclusion.rb
|
232
234
|
- lib/mv/core/validation/builder/length.rb
|
233
235
|
- lib/mv/core/validation/builder/presence.rb
|
234
236
|
- lib/mv/core/validation/builder/uniqueness.rb
|
237
|
+
- lib/mv/core/validation/custom.rb
|
235
238
|
- lib/mv/core/validation/exclusion.rb
|
236
239
|
- lib/mv/core/validation/factory.rb
|
237
240
|
- lib/mv/core/validation/inclusion.rb
|