Module: DynamicMigrations::Postgres::Server::Database::Schema::Table::Validations

Included in:
DynamicMigrations::Postgres::Server::Database::Schema::Table
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb

Overview

This module has all the tables methods for working with validations

Defined Under Namespace

Classes: ValidationAlreadyExistsError, ValidationDoesNotExistError

Instance Method Summary collapse

Instance Method Details

#add_validation(name, column_names, check_clause, **validation_options) ⇒ Object

adds a new validation to this table, and returns it



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb', line 41

def add_validation name, column_names, check_clause, **validation_options
  if has_validation? name
    raise(ValidationAlreadyExistsError, "Validation #{name} already exists")
  end
  columns = column_names&.map { |column_name| column column_name }
  included_target = self
  if included_target.is_a? Table
    new_validation = @validations[name] = Validation.new source, included_target, columns, name, check_clause, **validation_options
  else
    raise ModuleIncludedIntoUnexpectedTargetError, included_target
  end
  # sort the hash so that the validations are in alphabetical order by name
  sorted_validations = {}
  @validations.keys.sort.each do |name|
    sorted_validations[name] = @validations[name]
  end
  @validations = sorted_validations
  # return the new validation
  new_validation
end

#has_validation?(name) ⇒ Boolean

returns true if this table has a validation with the provided name, otherwise false

Returns:

  • (Boolean)

Raises:



26
27
28
29
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb', line 26

def has_validation? name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @validations.key? name
end

#validation(name) ⇒ Object

returns the validation object for the provided validation name, and raises an error if the validation does not exist



19
20
21
22
23
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb', line 19

def validation name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  raise ValidationDoesNotExistError unless has_validation? name
  @validations[name]
end

#validationsObject

returns an array of this tables validations



32
33
34
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb', line 32

def validations
  @validations.values
end

#validations_hashObject



36
37
38
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/validations.rb', line 36

def validations_hash
  @validations
end