Sha256: eea2490e3c15d6e98535c885bbd69c39b01e542b16fc2c9165bfb1b741d5c9da

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

module Druid
  module Elements
    class Table < Element
      #
      # Return the Druid::Elements::Table for the index provided.  Index
      # is zero based. If the index provided is a String then it
      # will be matched with the text from any column. The text can be
      # a substring of the full column text.
      #
      # @return [Druid::Elements::Table]
      #
      def [](idx)
        idx = find_index_by_title(idx) if idx.kind_of?(String)
        return nil unless idx
        Druid::Elements::TableRow.new(element[idx])
      end
      #
      # Returns the number of rows in the table.
      #
      def rows
        element.rows.size
      end

      def each
        for index in 1..self.rows do
          yield self[index-1]
        end
      end

      #
      # return the first row
      #
      # @return Druid::Elements::TableRow
      #
      def first_row
        self[0]
      end

      #
      # return the last row
      #
      # @return Druid::Elements::TableRow
      #
      def last_row
        self[-1]
      end

      private

      def find_index_by_title(row_title)
        element.rows.find_index do |row|
          row.cells.any? { |col| col.text.include? row_title}
        end
      end

    end

    Druid::Elements.tag_to_class[:table] = Druid::Elements::Table
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
druid-ts-1.1.8 lib/druid/elements/table.rb
druid-ts-1.1.7 lib/druid/elements/table.rb
druid-ts-1.1.6 lib/druid/elements/table.rb
druid-ts-1.1.5 lib/druid/elements/table.rb
druid-ts-1.1.4 lib/druid/elements/table.rb