rein 3.0.0 → 3.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: a9f1047d5663ba27fedf399e87223e024fe7a6fa
4
- data.tar.gz: fe37532d4ee714cd1e1fa281611f4ae61cd5561a
3
+ metadata.gz: cc7bf976b250a0cfaa15db1ec4a937afcdf9e6af
4
+ data.tar.gz: 79dcf97dbe8bdb932f42198675c9cc229f5f9ac3
5
5
  SHA512:
6
- metadata.gz: b3c700dd2bc15a2edf547ac390ec78e4972bc1bb3c7d4f534a495969d4e2e8b014121f9c11a230e415ba892c0e22a16d62bf391f21c69cf5d88bc7eef3d43f02
7
- data.tar.gz: 4153810c938897f244d72088e9c0095e99f7f8476c09069f647a4b974d7ad180860d019f6962b3192cf28fa70c2f8a0e549a32f9c6b3cd366d03ca9577bb8d42
6
+ metadata.gz: 3a2353e83aeae5a7f05787725a25c553689efb27c7093315e48a900ad0ed2e35069f8c6706571cb8b41080c6e04cff484e1574e9768d4f15bf11f7f6d2458ec7
7
+ data.tar.gz: 2cfeef3a3d05f9c483309fb0585bfc2733b1862dbb0b73dbccace17f652e28a606d53553f1b3cbe7d0dbbf59eea3c501cdbeb2c5bb772198db94008ba6e2bb33
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.1.0
4
+ - Add length constraint.
5
+
3
6
  ## 3.0.0
4
7
 
5
8
  - Dropped MySQL support.
data/README.md CHANGED
@@ -23,6 +23,7 @@ advantage of reversible Rails migrations.
23
23
  * [Constraint types](#constraint-types)
24
24
  * [Foreign key constraints](#foreign-key-constraints)
25
25
  * [Inclusion constraints](#inclusion-constraints)
26
+ * [Length constraints](#length-constraints)
26
27
  * [Numericality constraints](#numericality-constraints)
27
28
  * [Presence constraints](#presence-constraints)
28
29
  * [Null constraints](#null-constraints)
@@ -146,6 +147,54 @@ add_inclusion_constraint :books, :state,
146
147
  name: "books_state_is_valid"
147
148
  ```
148
149
 
150
+ ### Length constraints
151
+
152
+ A length constraint specifies the range of values that the length of a string
153
+ column value can take.
154
+
155
+ For example, we can ensure that the `call_number` can only ever be a
156
+ value between 1 and 255:
157
+
158
+ ```ruby
159
+ add_length_constraint :books, :call_number,
160
+ greater_than_or_equal_to: 1,
161
+ less_than_or_equal_to: 255
162
+ ```
163
+
164
+ Here's all the options for constraining the values:
165
+
166
+ - `equal_to`
167
+ - `not_equal_to`
168
+ - `less_than`
169
+ - `less_than_or_equal_to`
170
+ - `greater_than`
171
+ - `greater_than_or_equal_to`
172
+
173
+ You may also include an `if` option to enforce the constraint only under
174
+ certain conditions, like so:
175
+
176
+ ```ruby
177
+ add_length_constraint :books, :call_number,
178
+ greater_than_or_equal_to: 1,
179
+ less_than_or_equal_to: 12,
180
+ if: "status = 'published'"
181
+ ```
182
+
183
+ You may optionally provide a `name` option to customize the name:
184
+
185
+ ```ruby
186
+ add_length_constraint :books, :call_number,
187
+ greater_than_or_equal_to: 1,
188
+ less_than_or_equal_to: 12,
189
+ name: "books_call_number_is_valid"
190
+ ```
191
+
192
+ To remove a length constraint:
193
+
194
+ ```ruby
195
+ remove_length_constraint :books, :call_number
196
+ ```
197
+
149
198
  ### Numericality constraints
150
199
 
151
200
  A numericality constraint specifies the range of values that a numeric column
data/lib/rein.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "active_record"
2
2
  require "rein/constraint/foreign_key"
3
3
  require "rein/constraint/inclusion"
4
+ require "rein/constraint/length"
4
5
  require "rein/constraint/null"
5
6
  require "rein/constraint/numericality"
6
7
  require "rein/constraint/presence"
@@ -13,6 +14,7 @@ module ActiveRecord
13
14
  class Migration # :nodoc:
14
15
  include Rein::Constraint::ForeignKey
15
16
  include Rein::Constraint::Inclusion
17
+ include Rein::Constraint::Length
16
18
  include Rein::Constraint::Null
17
19
  include Rein::Constraint::Numericality
18
20
  include Rein::Constraint::Presence
@@ -0,0 +1,49 @@
1
+ require "rein/util"
2
+
3
+ module Rein
4
+ module Constraint
5
+ # This module contains methods for defining length constraints.
6
+ module Length
7
+ OPERATORS = {
8
+ greater_than: :>,
9
+ greater_than_or_equal_to: :>=,
10
+ equal_to: :"=",
11
+ not_equal_to: :"!=",
12
+ less_than: :<,
13
+ less_than_or_equal_to: :<=
14
+ }.freeze
15
+
16
+ def add_length_constraint(*args)
17
+ reversible do |dir|
18
+ dir.up { _add_length_constraint(*args) }
19
+ dir.down { _remove_length_constraint(*args) }
20
+ end
21
+ end
22
+
23
+ def remove_length_constraint(*args)
24
+ reversible do |dir|
25
+ dir.up { _remove_length_constraint(*args) }
26
+ dir.down { _add_length_constraint(*args) }
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def _add_length_constraint(table, attribute, options = {})
33
+ name = Util.constraint_name(table, attribute, "length", options)
34
+ attribute_length = "length(#{attribute})"
35
+ conditions = OPERATORS.slice(*options.keys).map do |key, operator|
36
+ value = options[key]
37
+ [attribute_length, operator, value].join(" ")
38
+ end.join(" AND ")
39
+ conditions = Util.conditions_with_if(conditions, options)
40
+ execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
41
+ end
42
+
43
+ def _remove_length_constraint(table, attribute, options = {})
44
+ name = Util.constraint_name(table, attribute, "length", options)
45
+ execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
46
+ end
47
+ end
48
+ end
49
+ end
data/lib/rein/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rein
2
- VERSION = "3.0.0".freeze
2
+ VERSION = "3.1.0".freeze
3
3
  end
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.0.0
4
+ version: 3.1.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-04-12 00:00:00.000000000 Z
11
+ date: 2017-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -147,6 +147,7 @@ files:
147
147
  - lib/rein.rb
148
148
  - lib/rein/constraint/foreign_key.rb
149
149
  - lib/rein/constraint/inclusion.rb
150
+ - lib/rein/constraint/length.rb
150
151
  - lib/rein/constraint/null.rb
151
152
  - lib/rein/constraint/numericality.rb
152
153
  - lib/rein/constraint/presence.rb
@@ -176,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
177
  version: '0'
177
178
  requirements: []
178
179
  rubyforge_project:
179
- rubygems_version: 2.6.11
180
+ rubygems_version: 2.5.2
180
181
  signing_key:
181
182
  specification_version: 4
182
183
  summary: Database constraints made easy for ActiveRecord.