rein 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +34 -2
- data/lib/rein.rb +2 -0
- data/lib/rein/constraint/match.rb +46 -0
- data/lib/rein/version.rb +1 -1
- data/lib/rein/view.rb +1 -1
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 174a8d4fd2ac014ff07be51f2066f24031093af5
|
4
|
+
data.tar.gz: a87d03529fbac3e218d7f9a8bb525e168ecd9942
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1a4ee06b5194406e7505c727851d9d8d1d2e5e89e01bd0f4f4a4e27beca20a232b7ec9cad514f2b1f3eecf91e5debe31268a69e4c92a5619ea283915b6e5f1e
|
7
|
+
data.tar.gz: 43fac121fcf44b69e6ff326cf6a3b291b4c534ee4c2af0aa6b5e5a8cfa274795d6c34abdbe55045044c8e7ba8cc33cef7f4a3003e0009bb540f15344e7ef1545
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -24,6 +24,7 @@ advantage of reversible Rails migrations.
|
|
24
24
|
* [Foreign key constraints](#foreign-key-constraints)
|
25
25
|
* [Inclusion constraints](#inclusion-constraints)
|
26
26
|
* [Length constraints](#length-constraints)
|
27
|
+
* [Match constraints](#match-constraints)
|
27
28
|
* [Numericality constraints](#numericality-constraints)
|
28
29
|
* [Presence constraints](#presence-constraints)
|
29
30
|
* [Null constraints](#null-constraints)
|
@@ -195,6 +196,37 @@ To remove a length constraint:
|
|
195
196
|
remove_length_constraint :books, :call_number
|
196
197
|
```
|
197
198
|
|
199
|
+
### Match constraints
|
200
|
+
|
201
|
+
A match constraint ensures that a string column value matches (or does not match)
|
202
|
+
a POSIX-style regular expression.
|
203
|
+
|
204
|
+
For example, we can ensure that the `title` can only contain printable ASCII
|
205
|
+
characters, but not ampersands:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
add_match_constraint :books, :title, accepts: '\A[ -~]*\Z', rejects: '&'
|
209
|
+
```
|
210
|
+
|
211
|
+
If you only want to enforce the constraint under certain conditions,
|
212
|
+
you can pass an optional `if` option:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
add_match_constraint :books, :title, accepts: '\A[ -~]*\Z', if: "status = 'published'"
|
216
|
+
```
|
217
|
+
|
218
|
+
You may optionally provide a `name` option to customize the name:
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
add_match_constraint :books, :title, name: "books_title_is_valid"
|
222
|
+
```
|
223
|
+
|
224
|
+
To remove a match constraint:
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
remove_match_constraint :books, :title
|
228
|
+
```
|
229
|
+
|
198
230
|
### Numericality constraints
|
199
231
|
|
200
232
|
A numericality constraint specifies the range of values that a numeric column
|
@@ -247,8 +279,8 @@ remove_numericality_constraint :books, :publication_month
|
|
247
279
|
|
248
280
|
A presence constraint ensures that a string column value is non-empty.
|
249
281
|
|
250
|
-
A `NOT NULL` constraint will be satisfied by an empty string, but sometimes
|
251
|
-
|
282
|
+
A `NOT NULL` constraint will be satisfied by an empty string, but sometimes you
|
283
|
+
may want to ensure that there is an actual value for a string:
|
252
284
|
|
253
285
|
```ruby
|
254
286
|
add_presence_constraint :books, :title
|
data/lib/rein.rb
CHANGED
@@ -2,6 +2,7 @@ require "active_record"
|
|
2
2
|
require "rein/constraint/foreign_key"
|
3
3
|
require "rein/constraint/inclusion"
|
4
4
|
require "rein/constraint/length"
|
5
|
+
require "rein/constraint/match"
|
5
6
|
require "rein/constraint/null"
|
6
7
|
require "rein/constraint/numericality"
|
7
8
|
require "rein/constraint/presence"
|
@@ -15,6 +16,7 @@ module ActiveRecord
|
|
15
16
|
include Rein::Constraint::ForeignKey
|
16
17
|
include Rein::Constraint::Inclusion
|
17
18
|
include Rein::Constraint::Length
|
19
|
+
include Rein::Constraint::Match
|
18
20
|
include Rein::Constraint::Null
|
19
21
|
include Rein::Constraint::Numericality
|
20
22
|
include Rein::Constraint::Presence
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "rein/util"
|
2
|
+
|
3
|
+
module Rein
|
4
|
+
module Constraint
|
5
|
+
# This module contains methods for defining regex match constraints.
|
6
|
+
module Match
|
7
|
+
include ActiveRecord::ConnectionAdapters::Quoting
|
8
|
+
|
9
|
+
OPERATORS = {
|
10
|
+
accepts: :~,
|
11
|
+
rejects: :"!~"
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
def add_match_constraint(*args)
|
15
|
+
reversible do |dir|
|
16
|
+
dir.up { _add_match_constraint(*args) }
|
17
|
+
dir.down { _remove_match_constraint(*args) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def remove_match_constraint(*args)
|
22
|
+
reversible do |dir|
|
23
|
+
dir.up { _remove_match_constraint(*args) }
|
24
|
+
dir.down { _add_match_constraint(*args) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def _add_match_constraint(table, attribute, options = {})
|
31
|
+
name = Util.constraint_name(table, attribute, "match", options)
|
32
|
+
conditions = OPERATORS.slice(*options.keys).map do |key, operator|
|
33
|
+
value = options[key]
|
34
|
+
[attribute, operator, "'#{value}'"].join(" ")
|
35
|
+
end.join(" AND ")
|
36
|
+
conditions = Util.conditions_with_if(conditions, options)
|
37
|
+
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
|
38
|
+
end
|
39
|
+
|
40
|
+
def _remove_match_constraint(table, attribute, options = {})
|
41
|
+
name = Util.constraint_name(table, attribute, "match", options)
|
42
|
+
execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/rein/version.rb
CHANGED
data/lib/rein/view.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rein
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Bassett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -112,28 +112,28 @@ dependencies:
|
|
112
112
|
requirements:
|
113
113
|
- - "~>"
|
114
114
|
- !ruby/object:Gem::Version
|
115
|
-
version: '3.
|
115
|
+
version: '3.7'
|
116
116
|
type: :development
|
117
117
|
prerelease: false
|
118
118
|
version_requirements: !ruby/object:Gem::Requirement
|
119
119
|
requirements:
|
120
120
|
- - "~>"
|
121
121
|
- !ruby/object:Gem::Version
|
122
|
-
version: '3.
|
122
|
+
version: '3.7'
|
123
123
|
- !ruby/object:Gem::Dependency
|
124
124
|
name: rubocop
|
125
125
|
requirement: !ruby/object:Gem::Requirement
|
126
126
|
requirements:
|
127
127
|
- - "~>"
|
128
128
|
- !ruby/object:Gem::Version
|
129
|
-
version: '0.
|
129
|
+
version: '0.51'
|
130
130
|
type: :development
|
131
131
|
prerelease: false
|
132
132
|
version_requirements: !ruby/object:Gem::Requirement
|
133
133
|
requirements:
|
134
134
|
- - "~>"
|
135
135
|
- !ruby/object:Gem::Version
|
136
|
-
version: '0.
|
136
|
+
version: '0.51'
|
137
137
|
description: Rein adds bunch of methods to your ActiveRecord migrations so you can
|
138
138
|
easily tame your database.
|
139
139
|
email: josh.bassett@gmail.com
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- lib/rein/constraint/foreign_key.rb
|
149
149
|
- lib/rein/constraint/inclusion.rb
|
150
150
|
- lib/rein/constraint/length.rb
|
151
|
+
- lib/rein/constraint/match.rb
|
151
152
|
- lib/rein/constraint/null.rb
|
152
153
|
- lib/rein/constraint/numericality.rb
|
153
154
|
- lib/rein/constraint/presence.rb
|
@@ -177,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
178
|
version: '0'
|
178
179
|
requirements: []
|
179
180
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.
|
181
|
+
rubygems_version: 2.6.13
|
181
182
|
signing_key:
|
182
183
|
specification_version: 4
|
183
184
|
summary: Database constraints made easy for ActiveRecord.
|