Sha256: 6a320f0f56428fa18d1cee7ae43a1858224756b898412a08a9b74b329fb9b7ec

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

module FastSeeder
  module Seeders
    class BaseSeeder
      def initialize(model_class, declared_columns, default_values)
	@model_class      = model_class
	@declared_columns = declared_columns

        # Ordered hash is necessary to not mess columns and wrong values
	@default_values = ActiveSupport::OrderedHash[*default_values.to_a.flatten]
      end

      def seed!
	record_set = RecordSet.new(@model_class, columns)
	specific_values.each do |record_vals|
	  record_set << merged_values_for(record_vals)
	end
	adapter = adapter_class.new(conn, record_set)
	adapter.insert!
      end

      private

      def specific_values
	raise NotImplementedError
      end


      def columns
	@columns ||= @declared_columns + @default_values.keys
      end

      def merged_values_for(specific_vals)
	specific_vals + @default_values.values
      end

      def adapter_class
	case conn.adapter_name
	when "PostgreSQL"      then Adapters::PostgresqlAdapter
	when "Mysql2", "MySQL" then Adapters::MysqlAdapter
	when "SQLite"          then Adapters::SqliteAdapter
	else raise("Unsupported connection adapter: #{conn.adapter_name}")
	end
      end

      def conn
	ActiveRecord::Base.connection
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fast_seeder-0.1.0 lib/fast_seeder/seeders/base_seeder.rb
fast_seeder-0.0.2 lib/fast_seeder/seeders/base_seeder.rb
fast_seeder-0.0.1 lib/fast_seeder/seeders/base_seeder.rb