rein 0.1.0 → 0.2.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.
- data/README.md +5 -1
- data/lib/rein.rb +10 -0
- data/lib/rein/constraint/foreign_key.rb +31 -0
- data/lib/rein/constraint/numericality.rb +3 -2
- data/lib/rein/version.rb +1 -1
- metadata +47 -40
data/README.md
CHANGED
@@ -2,10 +2,14 @@
|
|
2
2
|
|
3
3
|
Database constraints made easy for ActiveRecord.
|
4
4
|
|
5
|
+
|
5
6
|
## Introduction
|
6
7
|
|
8
|
+
Rein adds bunch of methods to your ActiveRecord migrations so you can easily tame your database.
|
9
|
+
|
7
10
|
ActiveRecord doesn't make it easy for you to weild the power of your database.
|
8
11
|
|
12
|
+
|
9
13
|
## Quick Start
|
10
14
|
|
11
15
|
First, install the gem:
|
@@ -14,4 +18,4 @@ First, install the gem:
|
|
14
18
|
|
15
19
|
Then slap some constraints in your migrations. For example:
|
16
20
|
|
17
|
-
add_numericality_constraint :
|
21
|
+
add_numericality_constraint :books, :publication_month, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 12
|
data/lib/rein.rb
CHANGED
@@ -7,5 +7,15 @@ end
|
|
7
7
|
|
8
8
|
RC = Rein::Constraint
|
9
9
|
|
10
|
+
require 'rein/constraint/foreign_key'
|
10
11
|
require 'rein/constraint/numericality'
|
11
12
|
require 'rein/version'
|
13
|
+
|
14
|
+
if defined?(ActiveRecord)
|
15
|
+
module ActiveRecord::ConnectionAdapters
|
16
|
+
class PostgreSQLAdapter < AbstractAdapter
|
17
|
+
include RC::ForeignKey
|
18
|
+
include RC::Numericality
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RC
|
2
|
+
module ForeignKey
|
3
|
+
def add_foreign_key_constraint(referencing_table, referenced_table, options = {})
|
4
|
+
referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id"
|
5
|
+
referenced_attribute = "id"
|
6
|
+
constraint_name = options[:name] || "#{referencing_attribute}_fk"
|
7
|
+
|
8
|
+
sql = "ALTER TABLE #{referencing_table} ADD CONSTRAINT #{constraint_name} FOREIGN KEY (#{referencing_attribute}) REFERENCES #{referenced_table} (#{referenced_attribute})"
|
9
|
+
sql << " ON DELETE #{referential_action(options[:on_delete])}" if options[:on_delete]
|
10
|
+
sql << " ON UPDATE #{referential_action(options[:on_update])}" if options[:on_update]
|
11
|
+
|
12
|
+
execute(sql)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def referential_action(action)
|
17
|
+
case action.to_sym
|
18
|
+
when :cascade
|
19
|
+
"CASCADE"
|
20
|
+
when :restrict
|
21
|
+
"RESTRICT"
|
22
|
+
when :nullify
|
23
|
+
"SET NULL"
|
24
|
+
when :default
|
25
|
+
"SET DEFAULT"
|
26
|
+
else
|
27
|
+
raise "Unknown referential action '#{action}'"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -8,15 +8,16 @@ module RC
|
|
8
8
|
:less_than_or_equal_to => :<=
|
9
9
|
}.freeze
|
10
10
|
|
11
|
-
def
|
11
|
+
def add_numericality_constraint(table, attribute, options = {})
|
12
12
|
conditions = OPERATORS.slice(*options.keys).map do |key, operator|
|
13
13
|
value = options[key]
|
14
14
|
[attribute, operator, value].join(" ")
|
15
15
|
end
|
16
16
|
|
17
17
|
conditions_sql = conditions.map {|condition| "(#{condition})" }.join(" AND ")
|
18
|
+
conditions_sql = "(#{conditions_sql})" if conditions.length > 1
|
18
19
|
|
19
|
-
"ALTER TABLE #{table} ADD CONSTRAINT #{attribute} CHECK #{conditions_sql}"
|
20
|
+
execute "ALTER TABLE #{table} ADD CONSTRAINT #{attribute} CHECK #{conditions_sql}"
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
data/lib/rein/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rein
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 27
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
-
|
7
|
+
- 2
|
9
8
|
- 0
|
10
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Josh Bassett
|
@@ -19,119 +18,127 @@ date: 2010-08-28 00:00:00 +10:00
|
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
21
|
+
name: activesupport
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
23
|
none: false
|
24
24
|
requirements:
|
25
25
|
- - ~>
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
hash: 7712042
|
28
27
|
segments:
|
29
28
|
- 3
|
30
29
|
- 0
|
31
30
|
- 0
|
32
31
|
- rc
|
33
32
|
version: 3.0.0.rc
|
34
|
-
requirement: *id001
|
35
|
-
name: activesupport
|
36
|
-
prerelease: false
|
37
33
|
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *id001
|
38
36
|
- !ruby/object:Gem::Dependency
|
39
|
-
|
37
|
+
name: bundler
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
39
|
none: false
|
41
40
|
requirements:
|
42
41
|
- - ~>
|
43
42
|
- !ruby/object:Gem::Version
|
44
|
-
hash: 23
|
45
43
|
segments:
|
46
44
|
- 1
|
47
45
|
- 0
|
48
46
|
- 0
|
49
47
|
version: 1.0.0
|
50
|
-
requirement: *id002
|
51
|
-
name: bundler
|
52
|
-
prerelease: false
|
53
48
|
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: *id002
|
54
51
|
- !ruby/object:Gem::Dependency
|
55
|
-
|
52
|
+
name: hirb
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
54
|
none: false
|
57
55
|
requirements:
|
58
56
|
- - ~>
|
59
57
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 23
|
61
58
|
segments:
|
62
59
|
- 0
|
63
60
|
- 3
|
64
61
|
- 2
|
65
62
|
version: 0.3.2
|
66
|
-
requirement: *id003
|
67
|
-
name: hirb
|
68
|
-
prerelease: false
|
69
63
|
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: *id003
|
70
66
|
- !ruby/object:Gem::Dependency
|
71
|
-
|
67
|
+
name: rake
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
72
69
|
none: false
|
73
70
|
requirements:
|
74
71
|
- - ~>
|
75
72
|
- !ruby/object:Gem::Version
|
76
|
-
hash: 49
|
77
73
|
segments:
|
78
74
|
- 0
|
79
75
|
- 8
|
80
76
|
- 7
|
81
77
|
version: 0.8.7
|
82
|
-
requirement: *id004
|
83
|
-
name: rake
|
84
|
-
prerelease: false
|
85
78
|
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *id004
|
86
81
|
- !ruby/object:Gem::Dependency
|
87
|
-
|
82
|
+
name: rcov
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
88
84
|
none: false
|
89
85
|
requirements:
|
90
86
|
- - ~>
|
91
87
|
- !ruby/object:Gem::Version
|
92
|
-
hash: 43
|
93
88
|
segments:
|
94
89
|
- 0
|
95
90
|
- 9
|
96
91
|
- 8
|
97
92
|
version: 0.9.8
|
98
|
-
requirement: *id005
|
99
|
-
name: rcov
|
100
|
-
prerelease: false
|
101
93
|
type: :development
|
94
|
+
prerelease: false
|
95
|
+
version_requirements: *id005
|
102
96
|
- !ruby/object:Gem::Dependency
|
103
|
-
|
97
|
+
name: rspec
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
104
99
|
none: false
|
105
100
|
requirements:
|
106
101
|
- - ~>
|
107
102
|
- !ruby/object:Gem::Version
|
108
|
-
hash: 27
|
109
103
|
segments:
|
110
104
|
- 1
|
111
105
|
- 3
|
112
106
|
- 0
|
113
107
|
version: 1.3.0
|
114
|
-
requirement: *id006
|
115
|
-
name: rspec
|
116
|
-
prerelease: false
|
117
108
|
type: :development
|
109
|
+
prerelease: false
|
110
|
+
version_requirements: *id006
|
118
111
|
- !ruby/object:Gem::Dependency
|
119
|
-
|
112
|
+
name: rr
|
113
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
120
114
|
none: false
|
121
115
|
requirements:
|
122
116
|
- - ~>
|
123
117
|
- !ruby/object:Gem::Version
|
124
|
-
hash: 23
|
125
118
|
segments:
|
126
119
|
- 1
|
127
120
|
- 0
|
128
121
|
- 0
|
129
122
|
version: 1.0.0
|
130
|
-
|
131
|
-
name: rr
|
123
|
+
type: :development
|
132
124
|
prerelease: false
|
125
|
+
version_requirements: *id007
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: wirble
|
128
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ~>
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
- 1
|
136
|
+
- 3
|
137
|
+
version: 0.1.3
|
133
138
|
type: :development
|
134
|
-
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: *id008
|
141
|
+
description: Rein adds bunch of methods to your ActiveRecord migrations so you can easily tame your database.
|
135
142
|
email: josh.bassett@gmail.com
|
136
143
|
executables: []
|
137
144
|
|
@@ -141,6 +148,7 @@ extra_rdoc_files: []
|
|
141
148
|
|
142
149
|
files:
|
143
150
|
- lib/rein/alone.rb
|
151
|
+
- lib/rein/constraint/foreign_key.rb
|
144
152
|
- lib/rein/constraint/numericality.rb
|
145
153
|
- lib/rein/version.rb
|
146
154
|
- lib/rein.rb
|
@@ -160,7 +168,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
168
|
requirements:
|
161
169
|
- - ">="
|
162
170
|
- !ruby/object:Gem::Version
|
163
|
-
hash:
|
171
|
+
hash: 1844054734367271197
|
164
172
|
segments:
|
165
173
|
- 0
|
166
174
|
version: "0"
|
@@ -169,7 +177,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
177
|
requirements:
|
170
178
|
- - ">="
|
171
179
|
- !ruby/object:Gem::Version
|
172
|
-
hash: 23
|
173
180
|
segments:
|
174
181
|
- 1
|
175
182
|
- 3
|