millisami-csv-hash 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tal Atlas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ = csv-hash
2
+
3
+ Will import a CSV as an array of hashes. Or will export a CSV from an array of hashes (if given a column list).
4
+
5
+
6
+ == Usage
7
+
8
+ === Parse a csv to an array of hashes
9
+ array_of_hashes, columns = CSVHash('path_to_csv.csv')
10
+
11
+ Note the hash will have strings for keys and will remove all surrounding whitespace
12
+
13
+ === Generate a string from an array of hashes and a column list
14
+ string = CSVHash(array_of_hashes,[:column,"names","in",:order])
15
+
16
+
17
+ === Nested Hashes
18
+
19
+ csv-hash supports nested hashes. A column tree of the structure (note the double underscore):
20
+ one,two__foo,two__bar__three,two__bar__four
21
+ 1, 2, 3, 4,
22
+
23
+ will generate:
24
+ {
25
+ 'one' => '1',
26
+ 'two' => {
27
+ 'foo' => '2',
28
+ 'bar' => {'three' => '3', 'four' => '4'}
29
+ }
30
+ }
31
+
32
+ == Todo
33
+
34
+ * Remove dependency on Fast CSV
35
+ * Support auto column generation
36
+ * Generate hash of arrays
37
+ * Turn into CSVHash object which contains column structure and other metadata
38
+ * Sequel integration
39
+ * Arbitrary model support
40
+
41
+ == Note on Patches/Pull Requests
42
+
43
+ * Fork the project.
44
+ * Make your feature addition or bug fix.
45
+ * Add tests for it. This is important so I don't break it in a
46
+ future version unintentionally.
47
+ * Commit, do not mess with rakefile, version, or history.
48
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
49
+ * Send me a pull request. Bonus points for topic branches.
50
+
51
+ == Copyright
52
+
53
+ Copyright (c) 2010 Tal Atlas. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "csv-hash"
8
+ gem.summary = %Q{A gem for interacting with CSVs as hashes}
9
+ gem.description = %Q{Will import a CSV as an array of hashes. Or will export a CSV from an array of hashes (if given a column list).}
10
+ gem.email = "me@talatlas.com"
11
+ gem.homepage = "http://github.com/Talby/csv-hash"
12
+ gem.authors = ["Tal Atlas"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency 'fastercsv', '>= 1.5.0'
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "csv-hash #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{millisami-csv-hash}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tal Atlas, Millisami"]
12
+ s.date = %q{2010-05-26}
13
+ s.description = %q{Will import a CSV as an array of hashes. Or will export a CSV from an array of hashes (if given a column list).}
14
+ s.email = %q{millisami@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "csv-hash.gemspec",
27
+ "lib/csv-hash.rb",
28
+ "spec/assets/clean_test.csv",
29
+ "spec/assets/test.csv",
30
+ "spec/csv-hash_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/Talby/csv-hash}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{A gem for interacting with CSVs as hashes}
39
+ s.test_files = [
40
+ "spec/csv-hash_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
+ # s.add_runtime_dependency(%q<fastercsv>, [">= 1.5.0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ # s.add_dependency(%q<fastercsv>, [">= 1.5.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ # s.add_dependency(%q<fastercsv>, [">= 1.5.0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,79 @@
1
+ require 'csv'
2
+ module CSVHash
3
+ module_function
4
+
5
+ def from_file file
6
+ num = 0
7
+
8
+ data = []
9
+ columns = []
10
+
11
+ CSV.foreach(file) do |row|
12
+ num += 1
13
+ if num == 1
14
+ columns = row.collect {|c| c.strip if c}
15
+ next
16
+ end
17
+
18
+ hash = {}
19
+ columns.each_with_index do |col, i|
20
+ next unless col
21
+ col = col ? col.strip : nil
22
+ val = row[i] ? row[i].strip : nil
23
+
24
+ setter = hash
25
+ sp = col.split('__')
26
+ sp.each_with_index do |key, j|
27
+ if j == sp.size - 1
28
+ setter[key] = val
29
+ else
30
+ setter[key] ||= {}
31
+
32
+ setter = setter[key]
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ data << hash
39
+ end
40
+
41
+ return data, columns.compact
42
+ end
43
+
44
+ def to_string hashes, columns
45
+ rows = hashes.collect do |hash|
46
+ vals = columns.collect do |col|
47
+ sp = col.to_s.split('__')
48
+ ret = hash
49
+ sp.each do |key|
50
+ puts key unless ret
51
+ ret = ret[key]
52
+ end
53
+ ret
54
+ end
55
+
56
+ vals
57
+ end
58
+
59
+
60
+ csv_string = CSV.generate do |csv|
61
+ csv << columns
62
+ rows.each do |val|
63
+ csv << val
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ # Pass either a path to a csv file to parse which will return an array of hashes (stringified keys) or pass an array of hashes and an array of column names
70
+ # See readme.rdoc for more detaild information
71
+ def CSVHash arg, columns=nil
72
+ if arg.is_a?(File)
73
+ CSVHash.from_file(arg.path)
74
+ elsif arg.is_a?(String)
75
+ CSVHash.from_file(arg)
76
+ elsif arg.is_a?(Array) && columns.is_a?(Array)
77
+ CSVHash.to_string(arg,columns)
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ one,two,foo__bar__three,foo__bar__four,foo__five,bar__six,nil,bar__seven
2
+ 1,2,3,4,5,6,,7
3
+ 14,13,12,11,10,9,,8
@@ -0,0 +1,3 @@
1
+ one,two,foo__bar__three,foo__bar__four, foo__five,bar__six,nil,,bar__seven
2
+ 1,2,3,4,5, 6,,,7
3
+ 14,13,12,11,10,9,,,8
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe CSVHash do
4
+ before(:all) do
5
+ @csv_path = File.expand_path(File.join(File.dirname(__FILE__),'assets','test.csv'))
6
+ @clean_csv_path = File.expand_path(File.join(File.dirname(__FILE__),'assets','clean_test.csv'))
7
+ end
8
+
9
+ describe "from_file" do
10
+ before(:all) do
11
+ @array, @columns = CSVHash.from_file(@csv_path)
12
+ end
13
+
14
+ it "should get correct column names (no spaces)" do
15
+ @columns.should == ["one", "two", "foo__bar__three", "foo__bar__four", "foo__five", "bar__six", "nil", "bar__seven"]
16
+ end
17
+
18
+ it "should only include data rows" do
19
+ @array.should have(2).rows_of_hashes
20
+ end
21
+
22
+ it "should parse properly" do
23
+ first = {
24
+ "one" => "1",
25
+ "two" => "2",
26
+ "foo" => {
27
+ "bar" => {"three" => "3", "four" => "4"},
28
+ "five" => "5"
29
+ },
30
+ "bar" => {"six" => "6", "seven" => "7"},
31
+ "nil" => nil
32
+ }
33
+ @array.first.should == first
34
+
35
+ second = {
36
+ "one" => "14",
37
+ "two" => "13",
38
+ "foo" => {
39
+ "bar" => {"three" => "12", "four" => "11"},
40
+ "five" => "10"
41
+ },
42
+ "bar" => {"six" => "9", "seven" => "8"},
43
+ "nil" => nil
44
+ }
45
+ @array.last.should == second
46
+ end
47
+ end
48
+
49
+
50
+ it "should generate an expected file" do
51
+ CSVHash.to_string(*CSVHash.from_file(@csv_path)).chomp.should == File.open(@clean_csv_path).read.chomp
52
+ end
53
+
54
+ it "should do a round trip" do
55
+ pending "Desire to add nil columns"
56
+ CSVHash.to_string(*CSVHash.from_file(@csv_path)).chomp.should == File.open(@csv_path).read.chomp
57
+ end
58
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'csv-hash'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: millisami-csv-hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tal Atlas, Millisami
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2010-05-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2158978940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.9
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2158978940
25
+ description: Will import a CSV as an array of hashes. Or will export a CSV from an
26
+ array of hashes (if given a column list).
27
+ email: millisami@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files:
31
+ - LICENSE
32
+ - README.rdoc
33
+ files:
34
+ - .document
35
+ - .gitignore
36
+ - LICENSE
37
+ - README.rdoc
38
+ - Rakefile
39
+ - VERSION
40
+ - csv-hash.gemspec
41
+ - lib/csv-hash.rb
42
+ - spec/assets/clean_test.csv
43
+ - spec/assets/test.csv
44
+ - spec/csv-hash_spec.rb
45
+ - spec/spec.opts
46
+ - spec/spec_helper.rb
47
+ homepage: http://github.com/Talby/csv-hash
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.6
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: A gem for interacting with CSVs as hashes
72
+ test_files:
73
+ - spec/csv-hash_spec.rb
74
+ - spec/spec_helper.rb