Sha256: 30df87b400ee2fdb5892cc057a3dad677726a64e9c99fd1df802138d4bd0f0e6

Contents?: true

Size: 887 Bytes

Versions: 1

Compression:

Stored size: 887 Bytes

Contents

require "delegate"
      
module Chop
  class DefinitionList < SimpleDelegator
    def self.diff! table, &block
      new.diff! table
    end

    def initialize selector = "dl", session: Capybara.current_session
      super(session)
      @selector = selector
    end

    def to_a
      rows.collect do |row|
        row_to_text(row)
      end
    end

    def normalized_to_a
      raw = to_a
      max = raw.map(&:count).max
      raw.select { |row| row.count == max }
    end

    def diff! table
      table.diff! normalized_to_a
    end

    private

    def rows
      node.all("dfn")
    end

    def node
      @node ||= find(@selector)
    end

    def row_to_text row
      row.all("dt,dd").collect do |cell|
        text = cell.text
        if text.blank? and image = cell.all("img").first
          text = image["alt"]
        end
        text
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chop-0.7.0 lib/chop/definition_list.rb