Changeset 716

Show
Ignore:
Timestamp:
10/04/07 06:34:14 (1 year ago)
Author:
has.s..@gmail.com
Message:

[Plugin][Sequel] Adds a model generator to the merb_sequel plugin. Selects rspec / test_unit tests automatically based on the script/generate file.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_sequel/Rakefile

    r697 r716  
    2424  s.require_path = 'lib' 
    2525  s.autorequire = PLUGIN 
    26   s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,merb_generators}/**/*") 
     26  s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,merb_generators,rspec_generators,test_unit_generators}/**/*") 
    2727end 
    2828 
  • plugins/merb_sequel/merb_generators/sequel_migration/sequel_migration_generator.rb

    r697 r716  
    1 require 'merb' 
    21class SequelMigrationGenerator < RubiGen::Base 
    32   
     
    109    usage if args.empty? 
    1110    @name = args.shift 
     11    options[:table_name] ||= runtime_options[:table_name] 
    1212    extract_options 
    1313  end 
     
    2121      # Ensure appropriate folder(s) exists 
    2222      m.directory 'schema/migrations' 
    23  
     23       
    2424      # Create stubs 
    2525      highest_migration = Dir[Dir.pwd+'/schema/migrations/*'].map{|f| File.basename(f) =~ /^(\d+)/; $1}.max 
    2626      filename = format("%03d_%s", (highest_migration.to_i+1), @name.snake_case) 
    27       m.template "new_migration.erb", "schema/migrations/#{filename}.rb", :assigns => { :class_name => @name } 
     27      m.template "new_migration.erb", "schema/migrations/#{filename}.rb",  
     28        :assigns => { :class_name => @name,  
     29                      :table_name => options[:table_name], 
     30                      :table_attributes => options[:table_attributes] } 
    2831 
    2932    end 
     
    3538Creates a new migration for merb using Sequel 
    3639 
    37 USAGE: #{$0} #{spec.name} NameOfMigration 
     40USAGE: #{$0} #{spec.name} NameOfMigration [field:type field:type] 
    3841 
    3942Example: 
     
    4245  If you already have 3 migrations, this will create the AddPeople migration in 
    4346  schema/migration/004_add_people.rb 
     47   
     48  #{$0} #{spec.name} project --table-name projects_table name:string created_at:timestamp 
     49   
     50  This will create a migration that creates a table call projects_table with these attributes: 
     51    string :name 
     52    timestamp :created_at 
     53     
    4454EOS 
    4555    end 
    4656 
    4757    def add_options!(opts) 
    48       # opts.separator '' 
    49       # opts.separator 'Options:' 
     58      opts.separator '' 
     59      opts.separator 'Options:' 
    5060      # For each option below, place the default 
    5161      # at the top of the file next to "default_options" 
     
    5464      #         "Default: none") { |options[:author]| } 
    5565      # opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.") 
     66      opts.on( "--table-name=\"table_name_for_migration\"",  
     67                String, 
     68                "Include a create table with the given table name"){ |options[:table_name]| } 
    5669    end 
    5770     
     
    6174      # raw instance variable value. 
    6275      # @author = options[:author] 
     76      if !options[:table_attributes] 
     77        attribute = Struct.new(:name, :type) 
     78        options[:table_attributes] = args.map{ |b| b.split(":").size == 2 ? attribute.new(*b.split(":")) : nil }.compact 
     79      end 
    6380    end 
    6481end 
  • plugins/merb_sequel/merb_generators/sequel_migration/templates/new_migration.erb

    r697 r716  
    11class <%= class_name.snake_case.camel_case %> < Sequel::Migration 
    22  def self.up 
     3    <%= "create_table :#{table_name} do" if table_name %> 
     4<% for attribute in table_attributes -%> 
     5      <%= attribute.type %> :<%= attribute.name %> 
     6<% end -%> 
     7    <%= "end" if table_name %>     
    38  end 
    49 
    510  def self.down 
     11<% if table_name -%> 
     12    execute 'DROP TABLE <%= table_name %>' 
     13<% end -%> 
    614  end 
    715end 
     16 
     17