mv-core 2.0.2 → 2.1.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
  SHA1:
3
- metadata.gz: 98227c28745a49420cc684f8098cf870db2bd444
4
- data.tar.gz: 6d6b2f996839eae4233098b9f87dec56b82e13ef
3
+ metadata.gz: 24ffb629e586415bc6671bc83c69ab96f61c1133
4
+ data.tar.gz: 1a7d2b792298becc6cba981959ad868887921264
5
5
  SHA512:
6
- metadata.gz: 8ff9e5d4efa377b0f8d3d6910cf841dda1d2a35fd32ba8fefc717951bad87a77e3a048ed04421f2de9b6fd41a4cb0986188b18c1f870d2d68adbc8a1fedba927
7
- data.tar.gz: 42c43363d8bcf604ee87e9c30973586a141238161bf221a4c268e9f1d143cd9b8a88f932848694191cb1abf3f8c6ae61e9227c3598c85462ebace2d262bcd04e
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, :exclusion: { in: [1,2,3] }
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, validates: { uniqueness: true,
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, validates: { uniqueness: true,
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, validates: { uniqueness: true,
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, validates: { uniqueness: true,
106
- on: :update }
102
+ validates :table_name, :str_column, uniqueness: { on: :update }
107
103
  ```
108
104
 
109
- Supported validators and their properties might vary from one db driver to another. See detailed properties description in correspondent driver section.
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.delete(:validates))
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.delete(:validates))
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.delete(:validates))
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
- opts == true ? {} : opts
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
@@ -9,7 +9,7 @@ module Mv
9
9
  validate :nil_and_blank_can_not_be_both_denied
10
10
 
11
11
  def initialize(table_name, column_name, opts)
12
- super(table_name, column_name, opts)
12
+ super(table_name, column_name, opts == true ? {} : opts)
13
13
  end
14
14
 
15
15
  protected
@@ -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
@@ -12,6 +12,8 @@ module Mv
12
12
  validates :in, presence: true, array: true
13
13
 
14
14
  def initialize(table_name, column_name, opts)
15
+ opts = opts.is_a?(Hash) ? opts : { in: opts }
16
+
15
17
  super(table_name, column_name, opts)
16
18
 
17
19
  @in = opts.with_indifferent_access[:in]
@@ -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 = factroy_map[validation_type.to_sym]
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
- factroy_map[validation_type.to_sym] = klass
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, :register_validation, :register_validations, to: :instance
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 factroy_map
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
@@ -12,6 +12,8 @@ module Mv
12
12
  validates :in, presence: true, array: true
13
13
 
14
14
  def initialize(table_name, column_name, opts)
15
+ opts = opts.is_a?(Hash) ? opts : { in: opts }
16
+
15
17
  super(table_name, column_name, opts)
16
18
 
17
19
  @in = opts.with_indifferent_access[:in]
@@ -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|
@@ -9,7 +9,7 @@ module Mv
9
9
  validate :nil_and_blank_can_not_be_both_allowed
10
10
 
11
11
  def initialize(table_name, column_name, opts)
12
- super(table_name, column_name, opts)
12
+ super(table_name, column_name, opts == true ? {} : opts)
13
13
  end
14
14
 
15
15
  private
@@ -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.2
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-19 00:00:00.000000000 Z
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