Module: Sequel::Plugins::MoreValidations::InstanceMethods
- Defined in:
- lib/sequel/plugins/more_validations.rb
Instance Method Summary collapse
-
#validates_at_least_one_of(*cols) ⇒ Object
Ensures that at least one of the passed columns is not null.
-
#validates_exactly_one_of(*cols) ⇒ Object
Ensures that one and only one of the passed columns is not null.
-
#validates_ip_address(col) ⇒ Object
Ensures the value in the column is an IPAddr or can be parsed as one.
-
#validates_mutually_exclusive(*cols) ⇒ Object
Ensures that only one of the passed columns is not null.
-
#validates_pgrange(col) ⇒ Object
Ensure the value in the column is non-nil, non-empty, and the start is before the end.
Instance Method Details
#validates_at_least_one_of(*cols) ⇒ Object
Ensures that at least one of the passed columns is not null
21 22 23 24 |
# File 'lib/sequel/plugins/more_validations.rb', line 21 def validates_at_least_one_of(*cols) self.errors.add(cols.first, "must be set if all of #{cols[1..].join(', ')} are null") unless cols.any? { |col| !self[col].nil? } end |
#validates_exactly_one_of(*cols) ⇒ Object
Ensures that one and only one of the passed columns is not null
27 28 29 30 |
# File 'lib/sequel/plugins/more_validations.rb', line 27 def validates_exactly_one_of(*cols) self.validates_at_least_one_of(*cols) self.validates_mutually_exclusive(*cols) end |
#validates_ip_address(col) ⇒ Object
Ensures the value in the column is an IPAddr or can be parsed as one.
33 34 35 36 37 38 39 40 |
# File 'lib/sequel/plugins/more_validations.rb', line 33 def validates_ip_address(col) return if self[col].respond_to?(:ipv4?) begin IPAddr.new(self[col]) rescue IPAddr::Error self.errors.add(col, "is not a valid INET address") end end |
#validates_mutually_exclusive(*cols) ⇒ Object
Ensures that only one of the passed columns is not null
13 14 15 16 17 18 |
# File 'lib/sequel/plugins/more_validations.rb', line 13 def validates_mutually_exclusive(*cols) set_cols = cols.find_all { |col| !self[col].nil? } self.errors.add(set_cols.first, "is mutually exclusive with other set columns #{set_cols[1..].join(', ')}") if set_cols.length > 1 end |
#validates_pgrange(col) ⇒ Object
Ensure the value in the column is non-nil, non-empty, and the start is before the end.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sequel/plugins/more_validations.rb', line 43 def validates_pgrange(col) val = self[col] if val.nil? self.errors.add(col, "cannot be nil") return end return if val.end > val.begin if val.end == val.begin self.errors.add(col, "cannot be empty") return end self.errors.add(col, "lower bound must be less than upper bound") end |