Sha256: 0ed1ff76cc4cc2d920e8efa0a7bd796df7316e9389c076013105a19fef55bdc7

Contents?: true

Size: 966 Bytes

Versions: 2

Compression:

Stored size: 966 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.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.empty?

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

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

      # If number col count is different, I guess 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

2 entries across 2 versions & 1 rubygems

Version Path
muzzy-0.1.13 lib/muzzy/header_detector.rb
muzzy-0.1.12 lib/muzzy/header_detector.rb