Sha256: e85adbb51008b663b274deaa93bbefca268c5e62f75ef80a73ba5017f3853ebd

Contents?: true

Size: 803 Bytes

Versions: 1

Compression:

Stored size: 803 Bytes

Contents

module SeedGimmick
  class SeedFile
    include Finder

    attr_reader :inflector, :seed_file

    def initialize(seed_dir, seed_file)
      @inflector = Inflector.new(seed_dir)
      @seed_file = seed_file
    end

    def load_file
      SeedIO.get(seed_file).load_data
    end

    def bootstrap!
      custom_action do |model, data|
        ActiveRecord::Migration.say_with_time(model.model_name.plural) do
          model.transaction do
            model.delete_all
            model.import(data.map {|rec| model.new(rec) })
          end
        end
      end
    rescue LoadFailed => e
      puts e.message
    end

    def custom_action
      yield(_model, load_file) if block_given?
    end

    private
      def _model
        @_model ||= inflector.model_for(seed_file)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seed_gimmick-0.0.1 lib/seed_gimmick/seed_file.rb