Sha256: 3dbc81ef9cc12f69cb78d6b4ab2e9f64703abb56cce5435d8adbebacc4afaaca

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module Merb::Generators
  
  class ModelGenerator < ComponentGenerator

    def self.source_root
      File.join(super, 'model')
    end
    
    desc <<-DESC
      Generates a new model. You can specify an ORM different from what the rest
      of the application uses.
    DESC
    
    option :testing_framework, :desc => 'Testing framework to use (one of: spec, test_unit)'
    option :orm, :desc => 'Object-Relation Mapper to use (one of: none, activerecord, datamapper, sequel)'
    
    first_argument :name, :required => true, :desc => "model name"
    second_argument :attributes, :as => :hash, :default => {}, :desc => "space separated model properties in form of name:type. Example: state:string"
    
    invoke :migration do |generator|
      generator.new(destination_root, options.merge(:model => true), name, attributes)
    end
    
    [:none, :activerecord, :sequel, :datamapper].each do |orm|
    
      template "model_#{orm}".to_sym, :orm => orm do
        source("#{orm}/app/models/%file_name%.rb")
        destination("app/models/#{file_name}.rb")
      end
    
    end
    
    template :spec, :testing_framework => :rspec do
      source('rspec/spec/models/%file_name%_spec.rb')
      destination('spec/models/' + file_name + '_spec.rb')
    end
    
    template :test_unit, :testing_framework => :test_unit do
      source('test_unit/test/models/%file_name%_test.rb')
      destination('test/models/' + file_name + '_test.rb')
    end
    
    def class_name
      self.name.camel_case
    end
    
    def test_class_name
      self.class_name + "Test"
    end
    
    def file_name
      self.name.snake_case
    end
    
    def attributes?
      self.attributes && !self.attributes.empty?
    end
    
    def attributes_for_accessor
      self.attributes.keys.map{|a| ":#{a}" }.compact.uniq.join(", ")
    end
    
  end
  
  add :model, ModelGenerator
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thorero-gen-0.9.4 lib/merb-gen/model.rb