Module: SexyPgConstraints::Constraints
- Defined in:
- lib/sexy_pg_constraints/constraints.rb
Class Method Summary collapse
-
.alphanumeric(column, options) ⇒ Object
Allow only alphanumeric values.
-
.blacklist(column, options) ⇒ Object
Prohibit listed values.
- .constrain_like(column, operator, string) ⇒ Object
-
.email(column, options) ⇒ Object
Allow only valid email format.
-
.even(column, options) ⇒ Object
Allow only even values.
-
.exact_length(column, options) ⇒ Object
Allow only text/strings of the exact length specified, no more, no less.
-
.format(column, options) ⇒ Object
Allow only values that match the regular expression.
-
.greater_than(column, options) ⇒ Object
Allow only values greater than the provided limit.
-
.greater_than_or_equal_to(column, options) ⇒ Object
Allow only values greater than or equal to the provided limit.
-
.length_within(column, options) ⇒ Object
Check the length of strings/text to be within the range.
-
.less_than(column, options) ⇒ Object
Allow only values less than the provided limit.
-
.less_than_or_equal_to(column, options) ⇒ Object
Allow only values less than or equal to the provided limit.
-
.like(column, options) ⇒ Object
Exclude values not matching the like pattern provided.
-
.lowercase(column, options) ⇒ Object
Allow only lower case values.
-
.not_like(column, options) ⇒ Object
Exclude values matching the like pattern provided.
-
.not_only(column, options) ⇒ Object
The value must have characters other than those listed in the option string.
-
.odd(column, options) ⇒ Object
Allow only odd values.
-
.positive(column, options) ⇒ Object
Allow only positive values.
-
.present(column, options) ⇒ Object
The value must have at least 1 non-space character.
-
.reference(column, options) ⇒ Object
Add foreign key constraint.
-
.stripped(column, options) ⇒ Object
The value must not have leading or trailing spaces.
-
.unique(column, options) ⇒ Object
Make sure every entry in the column is unique.
-
.whitelist(column, options) ⇒ Object
Only allow listed values.
-
.within(column, options) ⇒ Object
The numeric value must be within given range.
-
.xor(column, options) ⇒ Object
Allow only one of the values in the given columns to be true.
Class Method Details
.alphanumeric(column, options) ⇒ Object
Allow only alphanumeric values.
Example:
constrain :books, :author, :alphanumeric => true
114 115 116 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 114 def alphanumeric(column, ) %{check ((("#{column}")::text ~* '^[a-z0-9]+$'::text))} end |
.blacklist(column, options) ⇒ Object
Prohibit listed values.
Example:
constrain :books, :isbn, :blacklist => %w(invalid_isbn1 invalid_isbn2)
21 22 23 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 21 def blacklist(column, ) %{check ("#{column}" not in (#{ .collect{|v| "'#{v}'"}.join(',') }))} end |
.constrain_like(column, operator, string) ⇒ Object
263 264 265 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 263 def constrain_like(column, operator, string) %{check (("#{column}")::text #{operator} '#{string.gsub(/([^'])'([^'])/, '\1\'\'\2')}')} end |
.email(column, options) ⇒ Object
Allow only valid email format.
Example:
constrain :books, :author, :email => true
104 105 106 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 104 def email(column, ) %{check ((("#{column}")::text ~ E'^([-a-z0-9]+)@([-a-z0-9]+[.]+[a-z]{2,4})$'::text))} end |
.even(column, options) ⇒ Object
Allow only even values.
Example:
constrain :books, :quantity, :even => true
194 195 196 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 194 def even(column, ) %{check (mod("#{column}", 2) = 0)} end |
.exact_length(column, options) ⇒ Object
Allow only text/strings of the exact length specified, no more, no less.
Example:
constrain :books, :hash, :exact_length => 32
229 230 231 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 229 def exact_length(column, ) %{check ( length(trim(both from "#{column}")) = #{} )} end |
.format(column, options) ⇒ Object
Allow only values that match the regular expression.
Example:
constrain :orders, :visa, :format => /^([4]{1})([0-9]{12,15})$/
239 240 241 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 239 def format(column, ) %{check ((("#{column}")::text #{.casefold? ? '~*' : '~'} E'#{.source}'::text ))} end |
.greater_than(column, options) ⇒ Object
Allow only values greater than the provided limit.
Example:
constrain :books, :quantity, :greater_than => 12
164 165 166 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 164 def greater_than(column, ) %{check ("#{column}" > #{})} end |
.greater_than_or_equal_to(column, options) ⇒ Object
Allow only values greater than or equal to the provided limit.
Example:
constrain :books, :quantity, :greater_than_or_equal_to => 12
174 175 176 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 174 def greater_than_or_equal_to(column, ) %{check ("#{column}" >= #{})} end |
.length_within(column, options) ⇒ Object
Check the length of strings/text to be within the range.
Example:
constrain :books, :author, :length_within => 4..50
94 95 96 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 94 def length_within(column, ) within(%{length("#{column}")}, ) end |
.less_than(column, options) ⇒ Object
Allow only values less than the provided limit.
Example:
constrain :books, :quantity, :greater_than => 12
144 145 146 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 144 def less_than(column, ) %{check ("#{column}" < #{})} end |
.less_than_or_equal_to(column, options) ⇒ Object
Allow only values less than or equal to the provided limit.
Example:
constrain :books, :quantity, :greater_than => 12
154 155 156 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 154 def less_than_or_equal_to(column, ) %{check ("#{column}" <= #{})} end |
.like(column, options) ⇒ Object
Exclude values not matching the like pattern provided
Example:
constrain :orders, :visa, :like => '%FOO%'
249 250 251 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 249 def like(column, ) constrain_like(column, 'LIKE', ) end |
.lowercase(column, options) ⇒ Object
Allow only lower case values.
Example:
constrain :books, :author, :lowercase => true
124 125 126 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 124 def lowercase(column, ) %{check ("#{column}" = lower("#{column}"))} end |
.not_like(column, options) ⇒ Object
Exclude values matching the like pattern provided
Example:
constrain :orders, :visa, :not_like => '%FOO%'
259 260 261 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 259 def not_like(column, ) constrain_like(column, 'NOT LIKE', ) end |
.not_only(column, options) ⇒ Object
The value must have characters other than those listed in the option string.
Example:
constrain :books, :title, :not_only => 'abcd'
41 42 43 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 41 def not_only(column, ) %{check ( length(btrim("#{column}", E'#{}')) > 0 )} end |
.odd(column, options) ⇒ Object
Allow only odd values.
Example:
constrain :books, :quantity, :odd => true
184 185 186 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 184 def odd(column, ) %{check (mod("#{column}", 2) != 0)} end |
.positive(column, options) ⇒ Object
Allow only positive values.
Example:
constrain :books, :quantity, :positive => true
134 135 136 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 134 def positive(column, ) greater_than_or_equal_to(column, 0) end |
.present(column, options) ⇒ Object
The value must have at least 1 non-space character.
Example:
constrain :books, :title, :present => true
31 32 33 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 31 def present(column, ) %{check ( length(btrim("#{column}")) > 0 )} end |
.reference(column, options) ⇒ Object
Add foreign key constraint.
Example:
constrain :books, :author_id, :reference => {:authors => :id, :on_delete => :cascade}
274 275 276 277 278 279 280 281 282 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 274 def reference(column, ) on_delete = .delete(:on_delete) fk_table = .keys.first fk_column = [fk_table] on_delete = "on delete #{on_delete}" if on_delete %{foreign key ("#{column}") references #{fk_table} (#{fk_column}) #{on_delete}} end |
.stripped(column, options) ⇒ Object
The value must not have leading or trailing spaces.
You can pass a string as an option to indicate what characters are stripped.
Example:
constrain :books, :title, :stripped => true
constrain :books, :title, :stripped => "abc"
54 55 56 57 58 59 60 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 54 def stripped(column, ) if == true %{check (length("#{column}") = length(btrim("#{column}")))} else %{check (length("#{column}") = length(btrim("#{column}", E'#{}')))} end end |
.unique(column, options) ⇒ Object
Make sure every entry in the column is unique.
Example:
constrain :books, :isbn, :unique => true
204 205 206 207 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 204 def unique(column, ) columns = Array(column).map {|c| %{"#{c}"} }.join(', ') "unique(#{columns})" end |
.whitelist(column, options) ⇒ Object
Only allow listed values.
Example:
constrain :books, :variation, :whitelist => %w(hardcover softcover)
11 12 13 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 11 def whitelist(column, ) %{check ("#{column}" in (#{ .collect{|v| "'#{v}'"}.join(',') }))} end |
.within(column, options) ⇒ Object
The numeric value must be within given range.
Example:
constrain :books, :year, :within => 1980..2008
constrain :books, :year, :within => 1980...2009
constrain :books, :year, :within => {:range => 1979..2008, :exclude_beginning => true}
constrain :books, :year, :within => {:range => 1979..2009, :exclude_beginning => true, :exclude_end => true}
(the four lines above do the same thing)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 72 def within(column, ) column_ref = column.to_s.include?('"') ? column : %{"#{column}"} if .respond_to?(:to_hash) = .to_hash .assert_valid_keys(:range, :exclude_end, :exclude_beginning) range = .fetch(:range) exclude_end = .has_key?(:exclude_end) ? .fetch(:exclude_end) : range.exclude_end? exclude_beginning = .has_key?(:exclude_beginning) ? .fetch(:exclude_beginning) : false else range = exclude_end = range.exclude_end? exclude_beginning = false end "check (#{column_ref} >#{'=' unless exclude_beginning} #{range.begin} and #{column_ref} <#{'=' unless exclude_end} #{range.end})" end |
.xor(column, options) ⇒ Object
Allow only one of the values in the given columns to be true. Only reasonable with more than one column. See Enterprise Rails, Chapter 10 for details.
Example:
constrain :books, [], :xor => true
217 218 219 220 221 |
# File 'lib/sexy_pg_constraints/constraints.rb', line 217 def xor(column, ) addition = Array(column).map {|c| %{("#{c}" is not null)::integer} }.join(' + ') "check (#{addition} = 1)" end |