csv-as-map 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENCE +9 -0
- data/README +0 -0
- data/lib/csv_as_map.rb +46 -0
- data/test/simple.csv +4 -0
- data/test/test_csv_map.rb +48 -0
- data/test/test_helper.rb +2 -0
- metadata +47 -0
data/LICENCE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
This program is free software: you can redistribute it and/or modify
|
2
|
+
it under the terms of the GNU General Public License as published by
|
3
|
+
the Free Software Foundation, either version 3 of the License, or
|
4
|
+
(at your option) any later version.
|
5
|
+
|
6
|
+
This program is distributed in the hope that it will be useful,
|
7
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
8
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
9
|
+
GNU General Public License for more details.
|
data/README
ADDED
File without changes
|
data/lib/csv_as_map.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'FasterCSV'
|
3
|
+
|
4
|
+
class CsvMap
|
5
|
+
|
6
|
+
def self.foreach(path,&block)
|
7
|
+
|
8
|
+
header = nil
|
9
|
+
|
10
|
+
FasterCSV.foreach(path) do |row|
|
11
|
+
|
12
|
+
if ! header
|
13
|
+
header = self.create_header_hash(row)
|
14
|
+
next
|
15
|
+
end
|
16
|
+
|
17
|
+
row_map = parse(row,header)
|
18
|
+
yield row_map
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.create_header_hash(header_array)
|
23
|
+
header = Hash.new
|
24
|
+
count = 0
|
25
|
+
|
26
|
+
header_array.each do |token|
|
27
|
+
header[token] = count
|
28
|
+
count = count + 1
|
29
|
+
end
|
30
|
+
|
31
|
+
header
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.parse(row_array,header)
|
35
|
+
|
36
|
+
row_hash = Hash.new
|
37
|
+
|
38
|
+
header.each do |name,index|
|
39
|
+
row_hash[name] = row_array[index]
|
40
|
+
end
|
41
|
+
|
42
|
+
row_hash
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
data/test/simple.csv
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestCsvMap < Test::Unit::TestCase
|
4
|
+
def test_class_create_header_hash_normal
|
5
|
+
test_header = ['aaa','bbb','ccc','ddd']
|
6
|
+
|
7
|
+
expected = {'aaa' => 0, 'bbb' => 1, 'ccc' => 2, 'ddd' => 3}
|
8
|
+
result = CsvMap.create_header_hash(test_header)
|
9
|
+
|
10
|
+
assert_equal(expected,result)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_class_parse_normal
|
14
|
+
|
15
|
+
header = {'aaa' => 0, 'bbb' => 1, 'ccc' => 2, 'ddd' => 3}
|
16
|
+
row = ['a','b','c','d']
|
17
|
+
|
18
|
+
expected = {'aaa' => 'a', 'bbb' => 'b', 'ccc' => 'c', 'ddd' => 'd'}
|
19
|
+
result = CsvMap.parse(row,header)
|
20
|
+
|
21
|
+
assert_equal(expected,result)
|
22
|
+
assert_equal('a',result['aaa'])
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_class_foreach
|
27
|
+
|
28
|
+
expected_first = {'aaa' => 'a', 'bbb' => 'b', 'ccc' => 'c', 'ddd' => 'd'}
|
29
|
+
expected_second = {'aaa' => '1', 'bbb' => '2', 'ccc' => '3', 'ddd' => '4'}
|
30
|
+
expected_third = {'aaa' => 'w', 'bbb' => 'x', 'ccc' => 'y', 'ddd' => 'z'}
|
31
|
+
|
32
|
+
simple_csv = File.dirname(__FILE__) + '/simple.csv'
|
33
|
+
|
34
|
+
count = 0
|
35
|
+
CsvMap.foreach(simple_csv) do |row|
|
36
|
+
|
37
|
+
if count == 0
|
38
|
+
assert_equal(expected_first,row)
|
39
|
+
elsif count == 1
|
40
|
+
assert_equal(expected_second,row)
|
41
|
+
elsif count == 2
|
42
|
+
assert_equal(expected_third,row)
|
43
|
+
end
|
44
|
+
|
45
|
+
count = count + 1
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: csv-as-map
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-11-01 00:00:00 +00:00
|
8
|
+
summary: A package for manipulating IPv4/IPv6 address space.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: mail@michaelbarton.me.uk
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: csv_as_map
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
signing_key:
|
28
|
+
cert_chain:
|
29
|
+
post_install_message:
|
30
|
+
authors:
|
31
|
+
- Michael Barton
|
32
|
+
files:
|
33
|
+
- lib/csv_as_map.rb
|
34
|
+
- test/simple.csv
|
35
|
+
- test/test_csv_map.rb
|
36
|
+
- test/test_helper.rb
|
37
|
+
- README
|
38
|
+
- LICENCE
|
39
|
+
test_files: []
|
40
|
+
rdoc_options: []
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
- LICENCE
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
requirements: []
|
47
|
+
dependencies: []
|