Sha256: f7fc6b4a5662b068f34045a599506bcceb3b8d9ac1f022967a571438913744c3

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module RSpec
  module XlsxMatchers
    # common base for columns matchers
    module InColumn
      attr_reader :column_name

      def in_column(column_name)
        @column_name = column_name
        self
      end

      def in_column_by_index(index)
        @column_index = index
        self
      end

      private

      def column_index
        return @column_index unless @column_index.nil?

        return if sheet.nil? || column_row.nil?

        find_column
        @column_index
      end

      def column_row
        return @column_row unless @column_row.nil?

        @column_row = axlsx_column_row
        @column_row = roo_column_row if @column_row.nil?
        @column_row
      end

      def axlsx_column_row
        sheet.rows[0]&.map(&:value) if defined?(Axlsx::Worksheet) && sheet.is_a?(Axlsx::Worksheet)
      end

      def roo_column_row
        sheet.row(1) if defined?(Roo::Excelx) && sheet.is_a?(Roo::Excelx::Sheet)
      rescue StandardError
        # do nothing
      end

      def find_column
        column_row.each_with_index do |c, i|
          break unless @column_index.nil?

          @column_index = i if c.to_s == column_name
        end
      end

      def header_column_not_found?
        column_row.nil? || column_row.compact.empty?
      end

      def header_column_not_found_message
        "First row not found" if header_column_not_found?
      end

      def column_not_found?
        column_index.nil?
      end

      def column_not_found_message
        "Column #{column_name} not found"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-xlsx_matchers-0.1.6 lib/rspec/xlsx_matchers/in_column.rb
rspec-xlsx_matchers-0.1.5 lib/rspec/xlsx_matchers/in_column.rb
rspec-xlsx_matchers-0.1.4 lib/rspec/xlsx_matchers/in_column.rb