Class: Adjective::AddColumnsMigration
- Inherits:
-
Object
- Object
- Adjective::AddColumnsMigration
- Defined in:
- lib/templates/add_columns_migration.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#module_classes ⇒ Object
readonly
Returns the value of attribute module_classes.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
Instance Method Summary collapse
- #attribute_down_fields ⇒ Object
- #attribute_up_fields ⇒ Object
- #base_template ⇒ Object
- #file_name ⇒ Object
- #given_modules ⇒ Object
-
#initialize(class_name, modules = []) ⇒ AddColumnsMigration
constructor
A new instance of AddColumnsMigration.
- #migration_class ⇒ Object
- #render ⇒ Object
Constructor Details
#initialize(class_name, modules = []) ⇒ AddColumnsMigration
Returns a new instance of AddColumnsMigration.
5 6 7 8 9 10 11 |
# File 'lib/templates/add_columns_migration.rb', line 5 def initialize(class_name, modules = []) # raise ArgumentError, "Provided class_name of: #{class_name} was not a valid String." if (!!class_name || !class_name.is_a?(String)) @class_name = class_name @modules = modules.map{|m| m&.downcase} @version = "7.0" @module_classes = given_modules end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
3 4 5 |
# File 'lib/templates/add_columns_migration.rb', line 3 def class_name @class_name end |
#module_classes ⇒ Object (readonly)
Returns the value of attribute module_classes.
3 4 5 |
# File 'lib/templates/add_columns_migration.rb', line 3 def module_classes @module_classes end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
3 4 5 |
# File 'lib/templates/add_columns_migration.rb', line 3 def modules @modules end |
Instance Method Details
#attribute_down_fields ⇒ Object
36 37 38 |
# File 'lib/templates/add_columns_migration.rb', line 36 def attribute_down_fields attribute_up_fields.gsub("add_column", "remove_column") end |
#attribute_up_fields ⇒ Object
30 31 32 33 34 |
# File 'lib/templates/add_columns_migration.rb', line 30 def attribute_up_fields @module_classes.map do |mod| mod.adjective_add_columns(@class_name) end.join("\n").chomp end |
#base_template ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/templates/add_columns_migration.rb', line 40 def base_template <<~TEMPLATE class {{migration_class}} < ActiveRecord::Migration[{{version}}] def up {{add_methods}} end def down {{remove_methods}} end end TEMPLATE end |
#file_name ⇒ Object
18 19 20 21 22 |
# File 'lib/templates/add_columns_migration.rb', line 18 def file_name = Time.now.strftime('%Y%m%d%H%M%S') migration_name = migration_class.underscore return "#{}_#{migration_name}.rb" end |
#given_modules ⇒ Object
24 25 26 27 28 |
# File 'lib/templates/add_columns_migration.rb', line 24 def given_modules @modules.map do |mod| klass = "Adjective::#{mod.capitalize}".constantize end end |
#migration_class ⇒ Object
13 14 15 16 |
# File 'lib/templates/add_columns_migration.rb', line 13 def migration_class modules_string = @modules.map {|m| m.capitalize}.join("And") "Add#{modules_string}To#{@class_name.camelize}" end |
#render ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/templates/add_columns_migration.rb', line 54 def render base = base_template base .gsub("{{migration_class}}", migration_class.lstrip) .gsub("{{add_methods}}", attribute_up_fields.lstrip) .gsub("{{remove_methods}}", attribute_down_fields.lstrip) .gsub("{{version}}", @version) end |