Sha256: 194647539a5299de2bc35f3d652f3fe9c1449c9f32f1faf7751cefd948cb0ad6

Contents?: true

Size: 1.91 KB

Versions: 9

Compression:

Stored size: 1.91 KB

Contents

module RubyMutator
  def remove_superclass
    self.gsub! /(class\s+\w+\s*)<\s*(\w|::)+/, '\1'
  end
  
  def inherit_from superclass
    self.gsub! /(class\s+(\w|::)+)/, '\1' + " < #{superclass.to_s.camelize}\n"
  end
end

module Rails3::Assist::Artifact::CRUD
  module Create             
    include Rails3::Assist::Artifact::Marker
    
    def create_artifact name, options={}, &block
      type = get_type(options)
      file = make_file_name(name, type)
      return nil if File.exist?(file) && options[:no_overwrite]

      create_artifact_dir(file)      
      content = get_content(name, type, options, &block)
      
      superclass = options[:superclass] if options[:superclass]

      if superclass
        content.extend(RubyMutator)
        content.remove_superclass
        content.inherit_from "#{superclass}_permit"
      end
      
      return if content.blank?

      File.overwrite file, content      
    end

    protected
    
    # def new_artifact_content name, type, content=nil, &block
    def new_artifact_content name, options = {}, &block
      type = get_type(options)
      content = extract_content type, options, &block
      %Q{class #{marker(name, type)}
  #{content}
end}
    end

    def create_artifact_dir file
      # make dir        
      dir = File.dirname(file)
      FileUtils.mkdir_p dir if !File.directory?(dir)
    end

    def content_method type 
      method = :"new_#{type}_content"      
      raise "Content method #{content_method} not found #{orm_notify}" if !respond_to?(method)
      method
    end

    def extract_content type, options, &block
      content = block ? yield : options[:content]
      content = type == :model ? options.merge(:content => content) : content
    end    

    def get_content name, type, options = {}, &block
      content = extract_content type, options, &block
      method = content_method(type)
      send method, name, content
    end        
  end 
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails3_artifactor-0.4.0 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.3.2 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.3.1 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.3.0 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.2.8 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.2.7 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.2.6 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.2.5 lib/rails3_artifactor/base/crud/create.rb
rails3_artifactor-0.2.4 lib/rails3_artifactor/base/crud/create.rb