Module: Torque::PostgreSQL::VersionedCommands::Generator

Included in:
Generators::FunctionGenerator, Generators::TypeGenerator, Generators::ViewGenerator
Defined in:
lib/torque/postgresql/versioned_commands/generator.rb

Constant Summary collapse

TEMPLATES_PATH =
'../../../generators/torque/templates'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



12
13
14
# File 'lib/torque/postgresql/versioned_commands/generator.rb', line 12

def file_name
  @file_name
end

Class Method Details

.included(base) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/torque/postgresql/versioned_commands/generator.rb', line 14

def self.included(base)
  type = base.name.demodulize.chomp('Generator').underscore

  base.send(:source_root, File.expand_path(TEMPLATES_PATH, __dir__))
  base.include(ActiveRecord::Generators::Migration)

  base.instance_variable_set(:@type, type)
  base.instance_variable_set(:@desc, <<~DESC.squish)
    Generates a migration for creating, updating, or removing a #{type}.
  DESC

  base.class_option :operation, type: :string, aliases: %i(--op),
    desc: 'The name for the operation'

  base.argument :name, type: :string,
    desc: "The name of the #{type}"
end

Instance Method Details

#count_object_entriesObject



45
46
47
# File 'lib/torque/postgresql/versioned_commands/generator.rb', line 45

def count_object_entries
  Dir.glob("#{db_migrate_path}/*_#{type}_#{name.underscore}_v*.sql").size
end

#create_migration_fileObject



36
37
38
39
40
41
42
43
# File 'lib/torque/postgresql/versioned_commands/generator.rb', line 36

def create_migration_file
  version = count_object_entries
  operation = options[:operation] || (version == 0 ? 'create' : 'update')
  @file_name = "#{operation}_#{type}_#{name.underscore}_v#{version + 1}"

  validate_file_name!
  migration_template "#{type}.sql.erb", File.join(db_migrate_path, "#{file_name}.sql")
end

#typeObject



32
33
34
# File 'lib/torque/postgresql/versioned_commands/generator.rb', line 32

def type
  self.class.instance_variable_get(:@type)
end

#validate_file_name!Object



49
50
51
52
53
# File 'lib/torque/postgresql/versioned_commands/generator.rb', line 49

def validate_file_name!
  unless /^[_a-z0-9]+$/.match?(file_name)
    raise ActiveRecord::IllegalMigrationNameError.new(file_name)
  end
end