Changeset 721

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

[Plugin][merb_sequel] Updated the generators to use the new merb_generator_helper module. Also removed the rspec and test generators from the gem and to coincide with [720]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_sequel/merb_generators/sequel_migration/sequel_migration_generator.rb

    r716 r721  
    1 class SequelMigrationGenerator < RubiGen::Base 
     1require 'merb/generators/merb_generator_helpers' 
     2 
     3class SequelMigrationGenerator < Merb::GeneratorHelpers::MigrationGeneratorBase 
    24   
    3   default_options :author => nil 
     5  def initialize( *args ) 
     6    super( *args ) 
     7    @migration_template_name = "new_migration.erb" 
     8  end 
    49   
    5   attr_reader :name 
    6    
    7   def initialize(runtime_args, runtime_options = {}) 
    8     super 
    9     usage if args.empty? 
    10     @name = args.shift 
    11     options[:table_name] ||= runtime_options[:table_name] 
    12     extract_options 
     10  def self.superclass 
     11    RubiGen::Base 
    1312  end 
    14  
    15   def manifest 
    16     unless @name 
    17       puts banner 
    18       exit 1 
    19     end 
    20     record do |m| 
    21       # Ensure appropriate folder(s) exists 
    22       m.directory 'schema/migrations' 
    23        
    24       # Create stubs 
    25       highest_migration = Dir[Dir.pwd+'/schema/migrations/*'].map{|f| File.basename(f) =~ /^(\d+)/; $1}.max 
    26       filename = format("%03d_%s", (highest_migration.to_i+1), @name.snake_case) 
    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] } 
    31  
    32     end 
    33   end 
    34  
    35   protected 
    36     def banner 
    37       <<-EOS 
    38 Creates a new migration for merb using Sequel 
    39  
    40 USAGE: #{$0} #{spec.name} NameOfMigration [field:type field:type] 
    41  
    42 Example: 
    43   #{$0} #{spec.name} AddPeople 
    44  
    45   If you already have 3 migrations, this will create the AddPeople migration in 
    46   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 
    5313     
    54 EOS 
    55     end 
    56  
    57     def add_options!(opts) 
    58       opts.separator '' 
    59       opts.separator 'Options:' 
    60       # For each option below, place the default 
    61       # at the top of the file next to "default_options" 
    62       # opts.on("-a", "--author=\"Your Name\"", String, 
    63       #         "Some comment about this option", 
    64       #         "Default: none") { |options[:author]| } 
    65       # 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]| } 
    69     end 
    70      
    71     def extract_options 
    72       # for each option, extract it into a local variable (and create an "attr_reader :author" at the top) 
    73       # Templates can access these value via the attr_reader-generated methods, but not the 
    74       # raw instance variable value. 
    75       # @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 
    80     end 
    8114end 
  • plugins/merb_sequel/merb_generators/sequel_model/sequel_model_generator.rb

    r717 r721  
    1 class SequelModelGenerator < RubiGen::Base 
     1require 'merb/generators/merb_generator_helpers' 
     2 
     3class SequelModelGenerator < Merb::GeneratorHelpers::ModelGeneratorBase 
    24   
    3   default_options :author => nil 
     5  def initialize( *args ) 
     6    super( *args ) 
     7    @model_template_name = "sequel_model_template.erb" 
     8    @migration_generator_name = "sequel_migration" 
     9    @model_test_generator_name = "merb_model_test" 
     10  end 
    411   
    5   attr_reader :name, :model_attributes 
    6    
    7   def initialize(runtime_args, runtime_options = {}) 
    8     super 
    9     usage if args.empty? 
    10     @name = args.shift.snake_case.to_const_string 
    11     extract_options 
     12  def self.superclass 
     13    RubiGen::Base 
    1214  end 
    13  
    14   def manifest 
    15     unless @name 
    16       puts banner 
    17       exit 1 
    18     end 
    19     record do |m| 
    20        
    21       # ensure there are no other definitions of this model already defined. 
    22       m.class_collisions(@name) 
    23       # Ensure appropriate folder(s) exists 
    24       m.directory 'app/models' 
    25       #  
    26       model_filename = @name.snake_case 
    27       spec_filename = @name.snake_case.pluralize 
    28       table_name = spec_filename 
    29        
    30        
    31       # Create stubs 
    32       m.template "sequel_model_template.erb", "app/models/#{model_filename}.rb", :assigns => {:class_name => @name} 
    33        
    34       unless options[:skip_migration] 
    35         m.dependency "sequel_migration",["add_model_#{spec_filename}"], :table_name => table_name, :table_attributes => model_attributes 
    36       end 
    37        
    38       # Check to see if a scope has been set for which test framework to use. 
    39       # If we try to run the dependency without an :rspec or :test_unit scope an 
    40       # error will be raised. 
    41       scopes =RubiGen::Base.sources.map{|a| a.filters if a.respond_to?(:filters)}.compact.uniq.flatten 
    42       if scopes.include?(:rspec) || scopes.include?(:test_unit) 
    43         unless options[:skip_testing]  
    44           m.dependency "sequel_model_test", [@name] 
    45         end 
    46       else 
    47         puts "\nSelect a scope for :rspec or :test_unit in script/generate if you want to generate test stubs\n\n" 
    48       end 
    49  
    50     end     
    51   end 
    52  
    53   protected 
    54     def banner 
    55       <<-EOS 
    56 Creates a new model for merb using Sequel 
    57  
    58 USAGE: #{$0} #{spec.name} NameOfModel [field:type field:type] 
    59  
    60 Example: 
    61   #{$0} #{spec.name} project 
    62  
    63   If you already have 3 migrations, this will create the AddPeople migration in 
    64   schema/migration/004_add_people.rb 
    65    
    66 Options:  
    67   --skip-migration will not create a migration file 
    68  
    69  
    70  
    71 EOS 
    72     end 
    73  
    74     def add_options!(opts) 
    75       opts.separator '' 
    76       opts.separator 'Options:' 
    77       # For each option below, place the default 
    78       # at the top of the file next to "default_options" 
    79       # opts.on("-a", "--author=\"Your Name\"", String, 
    80       #         "Some comment about this option", 
    81       #         "Default: none") { |options[:author]| } 
    82       # opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.") 
    83       opts.on( "--skip-migration", "Don't generate a migration for this model") { |options[:skip_migration]| } 
    84       opts.on( "--skip-testing", "Don't generate a test or spec file for this model") { |options[:skip_testing]| } 
    85        
    86        
    87     end 
    8815     
    89     def extract_options 
    90       # for each option, extract it into a local variable (and create an "attr_reader :author" at the top) 
    91       # Templates can access these value via the attr_reader-generated methods, but not the 
    92       # raw instance variable value. 
    93       # @author = options[:author] 
    94        
    95       # get the attributes into a format that can be used. 
    96       attribute = Struct.new(:name, :type) 
    97       @model_attributes = args.map{ |b| b.split(":").size > 1 ? attribute.new(*b.split(":")) : nil }.compact 
    98     end 
    9916end