Module: Sequel::ConstraintValidations
- Defined in:
- lib/sequel/extensions/constraint_validations.rb
Defined Under Namespace
Modules: AlterTableGeneratorMethods, CreateTableGeneratorMethods Classes: Generator
Constant Summary collapse
- DEFAULT_CONSTRAINT_VALIDATIONS_TABLE =
The default table name used for the validation metadata.
:sequel_constraint_validations
- OPERATORS =
{:< => :lt, :<= => :lte, :> => :gt, :>= => :gte}.freeze
- REVERSE_OPERATOR_MAP =
{:str_lt => :<, :str_lte => :<=, :str_gt => :>, :str_gte => :>=, :int_lt => :<, :int_lte => :<=, :int_gt => :>, :int_gte => :>=}.freeze
Instance Attribute Summary collapse
-
#constraint_validations_table ⇒ Object
The name of the table storing the validation metadata.
Class Method Summary collapse
-
.extended(db) ⇒ Object
Set the default validation metadata table name if it has not already been set.
Instance Method Summary collapse
-
#alter_table_generator(&block) ⇒ Object
Modify the default alter_table generator to include the constraint validation methods.
-
#create_constraint_validations_table ⇒ Object
Create the table storing the validation metadata for all of the constraints created by this extension.
-
#create_table_generator(&block) ⇒ Object
Modify the default create_table generator to include the constraint validation methods.
-
#drop_constraint_validations_for(opts = OPTS) ⇒ Object
Delete validation metadata for specific constraints.
-
#drop_constraint_validations_table ⇒ Object
Drop the constraint validations table.
-
#drop_table(*names) ⇒ Object
Drop all constraint validations for a table if dropping the table.
Instance Attribute Details
#constraint_validations_table ⇒ Object
The name of the table storing the validation metadata. If modifying this from the default, this should be changed directly after loading the extension into the database
245 246 247 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 245 def constraint_validations_table @constraint_validations_table end |
Class Method Details
.extended(db) ⇒ Object
Set the default validation metadata table name if it has not already been set.
150 151 152 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 150 def self.extended(db) db.constraint_validations_table ||= DEFAULT_CONSTRAINT_VALIDATIONS_TABLE end |
Instance Method Details
#alter_table_generator(&block) ⇒ Object
Modify the default alter_table generator to include the constraint validation methods.
321 322 323 324 325 326 327 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 321 def alter_table_generator(&block) super do extend AlterTableGeneratorMethods @validations = [] instance_exec(&block) if block end end |
#create_constraint_validations_table ⇒ Object
Create the table storing the validation metadata for all of the constraints created by this extension.
249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 249 def create_constraint_validations_table create_table(constraint_validations_table) do String :table, :null=>false String :constraint_name String :validation_type, :null=>false String :column, :null=>false String :argument String :message TrueClass :allow_nil end end |
#create_table_generator(&block) ⇒ Object
Modify the default create_table generator to include the constraint validation methods.
263 264 265 266 267 268 269 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 263 def create_table_generator(&block) super do extend CreateTableGeneratorMethods @validations = [] instance_exec(&block) if block end end |
#drop_constraint_validations_for(opts = OPTS) ⇒ Object
Delete validation metadata for specific constraints. At least one of the following options should be specified:
- :table
-
The table containing the constraint
- :column
-
The column affected by the constraint
- :constraint
-
The name of the related constraint
The main reason for this method is when dropping tables or columns. If you have previously defined a constraint validation on the table or column, you should delete the related metadata when dropping the table or column. For a table, this isn’t a big issue, as it will just result in some wasted space, but for columns, if you don’t drop the related metadata, it could make it impossible to save rows, since a validation for a nonexistent column will be created.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 302 def drop_constraint_validations_for(opts=OPTS) ds = from(constraint_validations_table) if table = opts[:table] ds = ds.where(:table=>constraint_validations_literal_table(table)) end if column = opts[:column] ds = ds.where(:column=>column.to_s) end if constraint = opts[:constraint] ds = ds.where(:constraint_name=>constraint.to_s) end unless table || column || constraint raise Error, "must specify :table, :column, or :constraint when dropping constraint validations" end ds.delete end |
#drop_constraint_validations_table ⇒ Object
Drop the constraint validations table.
282 283 284 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 282 def drop_constraint_validations_table drop_table(constraint_validations_table) end |
#drop_table(*names) ⇒ Object
Drop all constraint validations for a table if dropping the table.
272 273 274 275 276 277 278 279 |
# File 'lib/sequel/extensions/constraint_validations.rb', line 272 def drop_table(*names) names.each do |name| if !name.is_a?(Hash) && table_exists?(constraint_validations_table) drop_constraint_validations_for(:table=>name) end end super end |