Module: DynamicMigrations::Postgres::Generator::Validation
- Included in:
- DynamicMigrations::Postgres::Generator
- Defined in:
- lib/dynamic_migrations/postgres/generator/validation.rb
Defined Under Namespace
Classes: TemplateAlreadyExistsError, UnexpectedTemplateError
Class Method Summary collapse
-
.add_template(name, template_class) ⇒ Object
install a template into the migration generator, this can be used from outside this library to clean up common migrations (replace common migrations with syntatic sugar).
- .has_template?(template_name) ⇒ Boolean
- .template(template_name) ⇒ Object
Instance Method Summary collapse
- #add_validation(validation, code_comment = nil) ⇒ Object
- #recreate_validation(original_validation, updated_validation) ⇒ Object
- #remove_validation(validation, code_comment = nil) ⇒ Object
-
#remove_validation_comment(validation, code_comment = nil) ⇒ Object
remove the comment from a validation.
-
#set_validation_comment(validation, code_comment = nil) ⇒ Object
add a comment to a validation.
Class Method Details
.add_template(name, template_class) ⇒ Object
install a template into the migration generator, this can be used from outside this library to clean up common migrations (replace common migrations with syntatic sugar)
21 22 23 24 25 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 21 def self.add_template name, template_class @templates ||= {} raise TemplateAlreadyExistsError, name if @templates.key?(name) @templates[name] = template_class end |
.has_template?(template_name) ⇒ Boolean
15 16 17 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 15 def self.has_template? template_name @templates&.key?(template_name) || false end |
.template(template_name) ⇒ Object
11 12 13 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 11 def self.template template_name @templates && @templates[template_name] end |
Instance Method Details
#add_validation(validation, code_comment = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 27 def add_validation validation, code_comment = nil # if we have a corresponding template, then use it if validation.template unless (template_class = Validation.template(validation.template)) raise UnexpectedTemplateError, "Unrecognised template #{validation.template}" end arguments = template_class.new(validation, code_comment).fragment_arguments add_fragment(**arguments) # no template, process this as a default validation (takes all options) else = { name: ":#{validation.name}" } if validation.description.nil? comment_sql = "" else comment_sql = " \#{validation.name}_comment = <<~COMMENT\n \#{indent validation.description || \"\"}\n COMMENT\n RUBY\n options[:comment] = \"\#{validation.name}_comment\"\n end\n\n options_syntax = options.map { |k, v| \"\#{k}: \#{v}\" }.join(\", \")\n\n validation_sql = validation.check_clause.strip\n\n add_fragment schema: validation.table.schema,\n table: validation.table,\n migration_method: :add_validation,\n object: validation,\n code_comment: code_comment,\n migration: comment_sql + <<~RUBY\n add_validation :\#{validation.table.name}, \#{options_syntax} do\n <<~SQL\n \#{indent validation_sql}\n SQL\n end\n RUBY\n end\nend\n" |
#recreate_validation(original_validation, updated_validation) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 85 def recreate_validation original_validation, updated_validation # remove the original validation removal_fragment = remove_validation original_validation, " Removing original validation because it has changed (it is recreated below)\n Changes:\n \#{indent original_validation.differences_descriptions(updated_validation).join(\"\\n\")}\n CODE_COMMENT\n\n # recrete the validation with the new options\n recreation_fragment = add_validation updated_validation, <<~CODE_COMMENT\n Recreating this validation\n CODE_COMMENT\n\n # return the new fragments (the main reason to return them here is for the specs)\n [removal_fragment, recreation_fragment]\nend\n" |
#remove_validation(validation, code_comment = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 74 def remove_validation validation, code_comment = nil add_fragment schema: validation.table.schema, table: validation.table, migration_method: :remove_validation, object: validation, code_comment: code_comment, migration: " remove_validation :\#{validation.table.name}, :\#{validation.name}\n RUBY\nend\n" |
#remove_validation_comment(validation, code_comment = nil) ⇒ Object
remove the comment from a validation
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 123 def remove_validation_comment validation, code_comment = nil add_fragment schema: validation.table.schema, table: validation.table, migration_method: :remove_validation_comment, object: validation, code_comment: code_comment, migration: " remove_validation_comment :\#{validation.table.name}, :\#{validation.name}\n RUBY\nend\n" |
#set_validation_comment(validation, code_comment = nil) ⇒ Object
add a comment to a validation
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/dynamic_migrations/postgres/generator/validation.rb', line 103 def set_validation_comment validation, code_comment = nil description = validation.description if description.nil? raise MissingDescriptionError, "Missing required description for validation `#{validation.name}` in table `#{validation.table.schema.name}.#{validation.table.name}`" end add_fragment schema: validation.table.schema, table: validation.table, migration_method: :set_validation_comment, object: validation, code_comment: code_comment, migration: " set_validation_comment :\#{validation.table.name}, :\#{validation.name}, <<~COMMENT\n \#{indent description}\n COMMENT\n RUBY\nend\n" |