Class: GeneratedSchemaValidations::Dumper
- Inherits:
-
Object
- Object
- GeneratedSchemaValidations::Dumper
- Defined in:
- lib/generated_schema_validations/dumper.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #create_table(table_name, &block) ⇒ Object
- #define(info, &block) ⇒ Object
- #do_nothing ⇒ Object (also: #enable_extension, #add_foreign_key)
- #write_schema_validations(template_ruby) ⇒ Object
Class Method Details
.define(info = {}, &block) ⇒ Object
23 24 25 |
# File 'lib/generated_schema_validations/dumper.rb', line 23 def self.define(info = {}, &block) new.define(info, &block) end |
.generate ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/generated_schema_validations/dumper.rb', line 4 def self.generate file = Tempfile.new(['schema', '.rb']) begin schema_content = read_schema_content.gsub(/ActiveRecord::Schema(\[[^\]]+\])?/, self.name) raise 'The scheme is not well-formed.' if schema_content.include?('ActiveRecord') file.write(schema_content) file.close load file.path ensure file.unlink end end |
.read_schema_content ⇒ Object
19 20 21 |
# File 'lib/generated_schema_validations/dumper.rb', line 19 def self.read_schema_content File.read(Rails.root.join('db/schema.rb')) end |
Instance Method Details
#create_table(table_name, &block) ⇒ Object
47 48 49 50 |
# File 'lib/generated_schema_validations/dumper.rb', line 47 def create_table(table_name, *, &block) @table_validations_ruby ||= '' @table_validations_ruby += GeneratedSchemaValidations::Table.new(table_name, &block).to_s end |
#define(info, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/generated_schema_validations/dumper.rb', line 27 def define(info, &block) instance_eval(&block) template_ruby = File.read(File.('template.rb', File.dirname(__FILE__))) template_ruby.gsub!('VERSION_INFO', info[:version].to_s) indention_spaces = template_ruby.match(/( +)TABLE_VALIDATIONS/)[1] table_validations_ruby = @table_validations_ruby.lines.map do |line| line.strip.present? ? "#{indention_spaces}#{line}" : "\n" end.join template_ruby.gsub!("#{indention_spaces}TABLE_VALIDATIONS", table_validations_ruby) write_schema_validations(template_ruby) end |
#do_nothing ⇒ Object Also known as: enable_extension, add_foreign_key
52 |
# File 'lib/generated_schema_validations/dumper.rb', line 52 def do_nothing(*); end |
#write_schema_validations(template_ruby) ⇒ Object
41 42 43 44 45 |
# File 'lib/generated_schema_validations/dumper.rb', line 41 def write_schema_validations(template_ruby) return unless Rails.env.development? File.write(Rails.root.join('app/models/concerns/schema_validations.rb'), template_ruby) end |