Sha256: 438ce5abd6fd47af39e8c6f9e1aa0d431ccbba369c930c08e7336cb8fe66e457

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Saxlsx
  class RowsCollectionParser < Ox::Sax

    def self.parse(index, file_system, shared_strings, &block)
      SaxParser.parse self.new(shared_strings, &block), file_system.sheets[index]
    end

    def initialize(shared_strings, &block)
      @shared_strings = shared_strings
      @block = block
    end

    def start_element(name)
      @current_element = name

      if name == :row
        @current_row = []
        @next_column = 'A'
      end

      @current_type = nil if name == :c
    end

    def end_element(name)
      if name == :row
        @block.call @current_row
        @current_row = nil
      end
    end

    def attr(name, value)
      if @current_element == :c
        @current_type = value if name == :t
        @current_column = value.gsub(/\d/, '') if name == :r
      end
    end

    def text(value)
      if @current_row && @current_element == :v
        while @next_column != @current_column
          @current_row << nil
          @next_column = ColumnNameGenerator.next_to(@next_column)
        end
        @current_row << value_of(value)
        @next_column = ColumnNameGenerator.next_to(@next_column)
      end
    end

    private

    def value_of(text)
      case @current_type
        when 's'
          @shared_strings[text.to_i]
        when 'b'
          BooleanParser.parse text
        when 'n'
          text.to_f
        else
          text
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saxlsx-0.1.0 lib/saxlsx/rows_collection_parser.rb