Class: ModelGenerator

Inherits:
Genosaurus
  • Object
show all
Defined in:
lib/mack-data_mapper/model_generator/model_generator.rb

Overview

This will generate an ORM ‘model’ for your application based on the specified ORM you’re using.

Example without columns:

rake generate:model name=user

app/models/user.rb:

class User
  include DataMapper::Resource

end

db/migrations/<number>_create_users.rb:

migration <number>, :create_users do
  up do
  end

  down do
  end
end

Example with columns:

rake generate:model name=user cols=username:string,email_address:string,created_at:datetime,updated_at:datetime

app/models/user.rb:

class User
  include DataMapper::Resource

  property :username, String
  property :email_address, String
  property :created_at, DateTime
  property :updated_at, DateTime
end

db/migrations/<number>_create_users.rb:

migration <number>, :create_users do
  up do
    create_table :users do
      column :username, String, :size => 50
      column :email, String, :size => 50
      column :created_at, DateTime
      column :updated_at, DateTime
    end
  end

  down do
    drop_table :users
  end
end

Instance Method Summary collapse

Instance Method Details

#after_generateObject

:nodoc:



58
59
60
# File 'lib/mack-data_mapper/model_generator/model_generator.rb', line 58

def after_generate # :nodoc:
  MigrationGenerator.run(@options.merge({"name" => "create_#{@name_plural}"}))
end

#migration_columnsObject

:nodoc:



62
63
64
# File 'lib/mack-data_mapper/model_generator/model_generator.rb', line 62

def migration_columns # :nodoc:
  [Mack::Genosaurus::Orm::ModelColumn.new(param(:name), "id:serial"), columns].flatten
end

#setupObject

:nodoc:



51
52
53
54
55
56
# File 'lib/mack-data_mapper/model_generator/model_generator.rb', line 51

def setup # :nodoc:
  @name_singular = param(:name).singular.underscore
  @name_plural = @name_singular.plural.underscore
  @name_singular_camel = @name_singular.camelcase
  @name_plural_camel = @name_plural.camelcase
end