Class: GeneratedSchemaValidations::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/generated_schema_validations/dumper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define(info = {}, &block) ⇒ Object



22
23
24
# File 'lib/generated_schema_validations/dumper.rb', line 22

def self.define(info = {}, &block)
  new.define(info, &block)
end

.generateObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/generated_schema_validations/dumper.rb', line 4

def self.generate
  return unless Rails.env.development?

  file = Tempfile.new(['schema', '.rb'])
  begin
    schema_content = File.read(Rails.root.join('db/schema.rb'))
    schema_content.gsub!(/ActiveRecord::Schema(\[[^\]]+\])?/, 'GeneratedSchemaValidations::Dumper')
    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

Instance Method Details

#create_table(table_name, &block) ⇒ Object



40
41
42
43
# File 'lib/generated_schema_validations/dumper.rb', line 40

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



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generated_schema_validations/dumper.rb', line 26

def define(info, &block)
  instance_eval(&block)

  template_ruby = File.read(File.expand_path('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)

  File.write(Rails.root.join('app/models/concerns/schema_validations.rb'), template_ruby)
end

#do_nothingObject Also known as: enable_extension, add_foreign_key



45
# File 'lib/generated_schema_validations/dumper.rb', line 45

def do_nothing(*); end