Class: Corneal::Generators::ModelGenerator
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Corneal::Generators::ModelGenerator
- Includes:
- Thor::Actions
- Defined in:
- lib/corneal/generators/model/model_generator.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#migration_class_name ⇒ Object
readonly
Returns the value of attribute migration_class_name.
-
#migration_name ⇒ Object
readonly
Returns the value of attribute migration_name.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
8 9 10 |
# File 'lib/corneal/generators/model/model_generator.rb', line 8 def class_name @class_name end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
8 9 10 |
# File 'lib/corneal/generators/model/model_generator.rb', line 8 def file_name @file_name end |
#migration_class_name ⇒ Object (readonly)
Returns the value of attribute migration_class_name.
8 9 10 |
# File 'lib/corneal/generators/model/model_generator.rb', line 8 def migration_class_name @migration_class_name end |
#migration_name ⇒ Object (readonly)
Returns the value of attribute migration_name.
8 9 10 |
# File 'lib/corneal/generators/model/model_generator.rb', line 8 def migration_name @migration_name end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
8 9 10 |
# File 'lib/corneal/generators/model/model_generator.rb', line 8 def model_name @model_name end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
8 9 10 |
# File 'lib/corneal/generators/model/model_generator.rb', line 8 def table_name @table_name end |
Class Method Details
.source_root ⇒ Object
17 18 19 |
# File 'lib/corneal/generators/model/model_generator.rb', line 17 def self.source_root File.dirname(__FILE__) end |
Instance Method Details
#create_migration ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/corneal/generators/model/model_generator.rb', line 43 def create_migration return unless [:migration] migration_files = Dir.entries("db/migrate").select { |path| !File.directory? path } if duplicate = migration_files.find { |file| file.include?(migration_name) } say_status :identical, "db/migrate/#{duplicate}", :blue else version = Time.now.utc.strftime("%Y%m%d%H%M%S") migration_file_name = "#{version}_#{migration_name}.rb" template "migration.rb.erb", File.join("db/migrate", migration_file_name) end end |
#create_model ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/corneal/generators/model/model_generator.rb', line 35 def create_model unless model_name == name say "[WARNING] The model name '#{name}' was recognized as a plural, using the singular '#{model_name}' instead." end template "model.rb.erb", File.join("app/models", "#{file_name}.rb") end |
#setup ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/corneal/generators/model/model_generator.rb', line 21 def setup @model_name = name.singularize @class_name = model_name.camel_case @file_name = model_name.underscore @table_name = file_name.pluralize @migration_name = "create_#{table_name}" @migration_class_name = migration_name.camel_case attributes.map! do |attribute| field = attribute.split(":") { name: field[0], type: field[1] || "string" } end end |