Skip to content

Commit 55c38d3

Browse files
committed
Add Source#to_hash to convert the data to an Array of hashes
1 parent 0f646f9 commit 55c38d3

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/csv-diff/source.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ def save_csv(file_path, options = {})
214214
@data.each{ |rec| csv << rec }
215215
end
216216
end
217+
218+
219+
# Convert the data in this source to Array<Hash> using the field names
220+
# as keys for the Hash in each row.
221+
def to_hash
222+
@data.map do |row|
223+
hsh = {}
224+
@field_names.each_with_index.map{ |fld, i| hsh[fld] = row[i] }
225+
hsh
226+
end
227+
end
217228

218229

219230
private

lib/csv-diff/xml_source.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class XMLSource < Source
1111
attr_accessor :context
1212

1313
# Create a new XMLSource, identified by +path+. Normally this is a path
14-
# to the XML document, but any value is fine, as it isreally just a label
15-
# to identify this data set.
14+
# to the XML document, but any value is fine, as it is just a label to
15+
# identify this data set.
1616
#
1717
# @param path [String] A label for this data set (often a path to the
1818
# XML document used as the source).

0 commit comments

Comments
 (0)