Module: PGTrunk::Generators Abstract
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/pg_trunk/core/generators.rb
Overview
This module is abstract.
Module to build object-specific generators
Class Method Summary collapse
-
.build_generator(operation) ⇒ Object
rubocop: disable Metrics/MethodLength.
-
.register(operation) ⇒ Object
Add new generator for given operation.
Instance Method Summary collapse
Class Method Details
.build_generator(operation) ⇒ Object
rubocop: disable Metrics/MethodLength
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pg_trunk/core/generators.rb', line 23 def build_generator(operation) Class.new(Rails::Generators::NamedBase) do include Rails::Generators::Migration include PGTrunk::Generators # Add the same arguments as in ruby method operation.ruby_params.each do |name| argument(name, **operation.attributes[name]) end # Add the same options as in ruby method operation.attributes.except(*operation.ruby_params).each do |name, opts| class_option(name, **opts) end # The only command of the generator is to create a migration file create_command(:create_migration_file) end end |
.register(operation) ⇒ Object
Add new generator for given operation
15 16 17 18 19 20 |
# File 'lib/pg_trunk/core/generators.rb', line 15 def register(operation) klass = build_generator(operation) class_name = klass.name.split("::").last.to_sym remove_const(class_name) if const_defined?(class_name) const_set(class_name, klass) end |
Instance Method Details
#create_migration_file ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pg_trunk/core/generators.rb', line 84 def create_migration_file file_name = "db/migrate/#{migration_number}_#{migration_name}.rb" file = create_migration(file_name, nil, {}) do <<~RUBY # frozen_string_literal: true class #{migration_name.camelize} < #{migration_base} def change #{command} end end RUBY end Rails::Generators.add_generated_file(file) end |