Changeset 721
- Timestamp:
- 10/07/07 00:14:18 (1 year ago)
- 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 1 require 'merb/generators/merb_generator_helpers' 2 3 class SequelMigrationGenerator < Merb::GeneratorHelpers::MigrationGeneratorBase 2 4 3 default_options :author => nil 5 def initialize( *args ) 6 super( *args ) 7 @migration_template_name = "new_migration.erb" 8 end 4 9 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 13 12 end 14 15 def manifest16 unless @name17 puts banner18 exit 119 end20 record do |m|21 # Ensure appropriate folder(s) exists22 m.directory 'schema/migrations'23 24 # Create stubs25 highest_migration = Dir[Dir.pwd+'/schema/migrations/*'].map{|f| File.basename(f) =~ /^(\d+)/; $1}.max26 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 end33 end34 35 protected36 def banner37 <<-EOS38 Creates a new migration for merb using Sequel39 40 USAGE: #{$0} #{spec.name} NameOfMigration [field:type field:type]41 42 Example:43 #{$0} #{spec.name} AddPeople44 45 If you already have 3 migrations, this will create the AddPeople migration in46 schema/migration/004_add_people.rb47 48 #{$0} #{spec.name} project --table-name projects_table name:string created_at:timestamp49 50 This will create a migration that creates a table call projects_table with these attributes:51 string :name52 timestamp :created_at53 13 54 EOS55 end56 57 def add_options!(opts)58 opts.separator ''59 opts.separator 'Options:'60 # For each option below, place the default61 # 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 end70 71 def extract_options72 # 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 the74 # 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 }.compact79 end80 end81 14 end plugins/merb_sequel/merb_generators/sequel_model/sequel_model_generator.rb
r717 r721 1 class SequelModelGenerator < RubiGen::Base 1 require 'merb/generators/merb_generator_helpers' 2 3 class SequelModelGenerator < Merb::GeneratorHelpers::ModelGeneratorBase 2 4 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 4 11 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 12 14 end 13 14 def manifest15 unless @name16 puts banner17 exit 118 end19 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) exists24 m.directory 'app/models'25 #26 model_filename = @name.snake_case27 spec_filename = @name.snake_case.pluralize28 table_name = spec_filename29 30 31 # Create stubs32 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_attributes36 end37 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 an40 # error will be raised.41 scopes =RubiGen::Base.sources.map{|a| a.filters if a.respond_to?(:filters)}.compact.uniq.flatten42 if scopes.include?(:rspec) || scopes.include?(:test_unit)43 unless options[:skip_testing]44 m.dependency "sequel_model_test", [@name]45 end46 else47 puts "\nSelect a scope for :rspec or :test_unit in script/generate if you want to generate test stubs\n\n"48 end49 50 end51 end52 53 protected54 def banner55 <<-EOS56 Creates a new model for merb using Sequel57 58 USAGE: #{$0} #{spec.name} NameOfModel [field:type field:type]59 60 Example:61 #{$0} #{spec.name} project62 63 If you already have 3 migrations, this will create the AddPeople migration in64 schema/migration/004_add_people.rb65 66 Options:67 --skip-migration will not create a migration file68 69 70 71 EOS72 end73 74 def add_options!(opts)75 opts.separator ''76 opts.separator 'Options:'77 # For each option below, place the default78 # 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 end88 15 89 def extract_options90 # 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 the92 # 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 }.compact98 end99 16 end
