Sha256: c420ebcca101799958b2a15b764893bb527647926dc75009c982cf3be69ff99d

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

require 'rails_artifactor/ruby_mutator'

module RailsAssist::Artifact::CRUD
  module Create             
    include RailsAssist::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(RailsAssist::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, 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)
      options[:content] = content
      send method, name, options, &block 
    end        
  end 
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_artifactor-0.5.0 lib/rails_artifactor/base/crud/create.rb
rails_artifactor-0.4.0 lib/rails_artifactor/base/crud/create.rb
rails_artifactor-0.3.6 lib/rails_artifactor/base/crud/create.rb
rails_artifactor-0.3.5 lib/rails_artifactor/base/crud/create.rb
rails_artifactor-0.3.4 lib/rails_artifactor/base/crud/create.rb
rails_artifactor-0.3.3 lib/rails_artifactor/base/crud/create.rb