Sha256: e698b8f2e0ee28871daca9564c772e26512d14ff0f12a47319c4bcfda4754c27

Contents?: true

Size: 1019 Bytes

Versions: 1

Compression:

Stored size: 1019 Bytes

Contents

module Muzzy
  class HeaderDetector
    # true: first row is header
    # false: first row is not header
    # nil: could not detect
    def self.detect(rows)
      first_row, second_row = rows || []
      return nil if first_row.nil? || first_row.empty?

      if first_row.any?{|str| str.to_s.match(/_id/i) }
        return true
      elsif first_row.any?{|str| str.to_s.match(/NULL/) }
        return false
      elsif first_row.any?{|str| str.to_s == '' }
        return false
      end

      return nil if second_row.nil? || second_row.empty?

      # I can't detect first_row is header or not, so guess now.

      # header row is not contain numbers in most cases
      first_row_number_count = first_row.select{|str| str.to_f > 0}.length
      if first_row_number_count > 0
        return false
      end

      # If number col count is different, first_row is header.
      if first_row_number_count != second_row.select{|x| x.to_f > 0}.count
        return true
      end

      return nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
muzzy-0.1.14 lib/muzzy/header_detector.rb