This method is the reading counterpart to CSV::dump(). See that method for
a detailed description of the process.
You can customize loading by adding a class method called csv_load() which
will be passed a Hash of meta information, an Array of headers, and an Array of fields for the object the method is
expected to return.
Remember that all fields will be Strings after this load. If you need something else, use
options to setup converters or
provide a custom csv_load() implementation.
# File lib/csv.rb, line 1096
def self.load(io_or_str, options = Hash.new)
csv = new(io_or_str, options)
# load meta information
meta = Hash[*csv.shift]
cls = meta["class".encode(csv.encoding)].split("::".encode(csv.encoding)).
inject(Object) do |c, const|
c.const_get(const)
end
# load headers
headers = csv.shift
# unserialize each object stored in the file
results = csv.inject(Array.new) do |all, row|
begin
obj = cls.csv_load(meta, headers, row)
rescue NoMethodError
obj = cls.allocate
headers.zip(row) do |name, value|
if name[0] == @@
obj.instance_variable_set(name, value)
else
obj.send(name, value)
end
end
end
all << obj
end
csv.close unless io_or_str.is_a? String
results
end