-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When hashWidget html tables are passed as arguments, they are convert…
…ed to hashes
- Loading branch information
unclebob
committed
Nov 23, 2009
1 parent
a8d601c
commit 6f5650e
Showing
5 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'rexml/document' | ||
|
||
class TableToHashConverter | ||
def self.convert(string) | ||
begin | ||
doc = REXML::Document.new(string.strip) | ||
return html_to_hash(doc) | ||
rescue | ||
return string | ||
end | ||
end | ||
|
||
def self.html_to_hash(doc) | ||
table = doc.elements['table'] | ||
raise ArgumentError if table.nil? | ||
create_hash_from_rows(table) | ||
end | ||
|
||
def self.create_hash_from_rows(rows) | ||
hash = {} | ||
rows.elements.each('tr') do |row| | ||
add_row_to_hash(hash, row) | ||
end | ||
hash | ||
end | ||
|
||
def self.add_row_to_hash(hash, row) | ||
columns = row.get_elements('td') | ||
raise ArgumentError if columns.size != 2 | ||
hash[columns[0].text.strip.to_sym] = columns[1].text.strip | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") | ||
require 'table_to_hash_converter' | ||
|
||
def shouldNotChange(string) | ||
TableToHashConverter.convert(string).should == string | ||
end | ||
|
||
describe TableToHashConverter do | ||
[ | ||
["some string", "not a table"], | ||
["<table>blah", "incomplete table"], | ||
["<table><tr><td>hi</td></tr></table>", "too few columns"], | ||
["<table><tr><td>hi</td><td>med</td><td>lo</td></tr></table>", "too many columns"] | ||
].each do |string, reason| | ||
it "#{reason}: should not change '#{string}'" do | ||
shouldNotChange(string) | ||
end | ||
end | ||
end | ||
|
||
describe TableToHashConverter do | ||
[ | ||
["<table><tr><td>name</td><td>bob</td></tr></table>", {:name=>"bob"}], | ||
[" <table> <tr> <td> name </td> <td> bob </td> </tr> </table> ", {:name=>"bob"}], | ||
["<table><tr><td>name</td><td>bob</td></tr><tr><td>addr</td><td>here</td></tr></table>", {:name=>'bob', :addr=>'here'}] | ||
].each do |table, hash| | ||
it "should match #{table} to #{hash}" do | ||
TableToHashConverter.convert(table).should == hash | ||
end | ||
end | ||
end |