Created
October 11, 2016 12:34
-
-
Save cptangry/8fd8b8de7833e55378ed72645e8972d9 to your computer and use it in GitHub Desktop.
Devasa Excel dosyaları için parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'creek' | |
require 'activerecord-import' | |
class ExcelDataParser | |
def initialize(dosya_yolu) | |
@dosya_yolu = dosya_yolu | |
@veriler = [] | |
@sayac = 1 | |
end | |
ENBUYUK_ICEAKTARMA_BOYUTU = 1000 | |
def cagir | |
rows.each do |satir| | |
sayaca_ekle | |
veriler << yeni_kayit_yarat(satir) | |
kayitlari_iceaktar if enbuyuk_iceaktarma_boyutunda? || dosyanin_sonuna_gelindi? | |
end | |
end | |
private | |
attr_reader :dosya_yolu, :veriler | |
attr_accessor :sayac | |
def book | |
@book ||= Creek::Book.new(dosya_yolu) | |
end | |
def rows | |
@rows ||= book.sheets.first.wows | |
end | |
def sayaca_ekle | |
self.sayac += 1 | |
end | |
def satir_sayaci | |
@satir_sayaci ||= rows.count | |
end | |
def yeni_kayit_yarat(row) | |
RecordModel.new(row) | |
end | |
def kayitlari_iceaktar | |
RecordModel.import(veriler) | |
veriler.clear | |
end | |
def enbuyuk_iceaktarma_boyutunda? | |
(sayac % ENBUYUK_ICEAKTARMA_BOYUTU).zero? | |
end | |
def dosyanin_sonuna_gelindi? | |
sayac == satir_sayaci | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment