Sha256: fa0049914df33deac5f09892677906c82b2a4d21d94c669b74ad9e15bb87021d

Contents?: true

Size: 1.17 KB

Versions: 10

Compression:

Stored size: 1.17 KB

Contents

require 'rails/generators/active_record'
require 'generators/csv_import_magic/orm_helpers'

module ActiveRecord
  module Generators
    class CsvImportMagicGenerator < ActiveRecord::Generators::Base
      source_root File.expand_path('../templates', __FILE__)
      class_option :columns, aliases: '-c', type: :array, required: true, desc: 'Select specific columns yout want parser'

      include CsvImportMagic::Generators::OrmHelpers

      def generate_model
        invoke 'active_record:model', [name], migration: false unless model_exists? && behavior == :invoke

        class_path = if name.match(/\:\:/).present?
                       name.to_s.gsub('::', '/')
                     else
                       name
        end

        template 'csv_parser.rb', "app/csv_parsers/#{class_path}_parser.rb"
      end

      def inject_csv_import_magic_content
        content = model_contents(options)
        class_path = [class_name]

        indent_depth = class_path.size - 1
        content = content.split("\n").map { |line| '  ' * indent_depth + line } .join("\n") << "\n"

        inject_into_class(model_path, class_path.last, content) if model_exists?
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
csv_import_magic-0.0.11 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.10 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.8 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.7 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.6 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.5 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.4 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.3 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.2 lib/generators/active_record/csv_import_magic_generator.rb
csv_import_magic-0.0.1 lib/generators/active_record/csv_import_magic_generator.rb