Sha256: 929f09664d455aefa1d10628e051af59a1ef0ac6caa1f8db33bce04eb1e5672c

Contents?: true

Size: 1.78 KB

Versions: 21

Compression:

Stored size: 1.78 KB

Contents

require 'iconv'
require 'csv'
require 'roo'
require 'roo-xls'
module SuperSpreadsheet
  class Loader < SuperProcess::Core
    attr_accessor :file_path
    validate :valid_format

    def self.load!(file)
      new(file)
    end

    def initialize(file_path)
      @file_path = file_path
    end

    callable do
      rows
    end

    def extension
      ::File.extname(file_path).split('.').last.force_encoding('utf-8').downcase
    end

    def valid_format
      if rows == false
        errors.add(:file, '檔案格式錯誤')
      elsif rows.flatten.empty?
        errors.add(:file, '檔案內容錯誤,空白檔案')
      end
    end

    def rows
      @rows ||= rows!
    end

    protected

    def rows!
      case extension
      when 'xls'
        ::Roo::Excel.new(file_path).map { |row| convert_float_to_integer(row) }
      when 'xlsx'
        ::Roo::Excelx.new(file_path).map { |row| convert_float_to_integer(row) }
      when 'csv'
        ::CSV.parse(csv_content!)
      else
        false
      end
    rescue
      false
    end

    def csv_content!
      @decode_csv_content ||= begin
        csv_content_big5!
      rescue Iconv::IllegalSequence
        begin
          csv_content_big5_hkscs!
        rescue Iconv::IllegalSequence
          csv_content
        end
      end
    end

    def csv_content
      @csv_content ||= File.read(file_path)
    end

    private

    def csv_content_big5!
      Iconv.new("utf-8", "Big5//TRANSLIT//IGNORE").iconv(csv_content)
    end

    def csv_content_big5_hkscs!
      Iconv.new("utf-8", "Big5-HKSCS//TRANSLIT//IGNORE").iconv(csv_content)
    end

    def convert_float_to_integer(row)
      row.map do |cell|
        if cell.is_a?(Float) && cell = cell.to_i
          cell.to_i
        else
          cell
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
super_tools-2.1.0 lib/super_spreadsheet/loader.rb
super_tools-0.0.26 lib/super_spreadsheet/loader.rb
super_tools-0.0.25 lib/super_spreadsheet/loader.rb
super_tools-0.0.21 lib/super_spreadsheet/loader.rb
super_tools-0.0.20 lib/super_spreadsheet/loader.rb
super_tools-0.0.17 lib/super_spreadsheet/loader.rb
super_tools-0.0.15 lib/super_spreadsheet/loader.rb
super_tools-0.0.14 lib/super_spreadsheet/loader.rb
super_tools-0.0.13 lib/super_spreadsheet/loader.rb
super_tools-0.0.12 lib/super_spreadsheet/loader.rb
super_tools-0.0.11 lib/super_spreadsheet/loader.rb
super_tools-0.0.10 lib/super_spreadsheet/loader.rb
super_tools-0.0.9 lib/super_spreadsheet/loader.rb
super_tools-0.0.8 lib/super_spreadsheet/loader.rb
super_tools-0.0.7 lib/super_spreadsheet/loader.rb
super_tools-0.0.6 lib/super_spreadsheet/loader.rb
super_tools-0.0.5 lib/super_spreadsheet/loader.rb
super_tools-0.0.4 lib/super_spreadsheet/loader.rb
super_tools-0.0.3 lib/super_spreadsheet/loader.rb
super_tools-0.0.2 lib/super_spreadsheet/loader.rb