tabler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tabler.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Klaas Speller
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.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # tabler
2
+
3
+ Tabler generates csv from an array of hashes.
4
+
5
+ It will transform hashes with inconsistent keys into consistant tables.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ gem install tabler
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+
17
+ data = [
18
+ { :col1 => "val 1", :col2 => "val 2"},
19
+ { :col1 => "row 2 val 1", :col2 => "row 2 val 2"},
20
+ { :col1 => "row 3 val 1", :col3 => "row 3 val 3", :col2 => "row 3 val 2"},
21
+ { :col1 => "row 4 val 1", :col3 => "row 4 val 3" }
22
+ ]
23
+
24
+ Tabler.generate(data, :col_sep => ";") # => Generate the csv string
25
+
26
+ ```
27
+
28
+ The string the above exaple will look like:
29
+
30
+ ```
31
+ col1;col2;col3
32
+ val 1;val 2;
33
+ row 2 val 1;row 2 val 2;
34
+ row 3 val 1;row 3 val 3;row 3 val 2
35
+ row 4 val 1;;row 4 val 3
36
+
37
+ ```
38
+
39
+ ## Copyright
40
+
41
+ Copyright (c) 2012 Klaas Speller. See LICENSE for details.
42
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/tabler/csv.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "csv"
2
+
3
+ module Tabler
4
+ class CSV
5
+ attr_reader :data
6
+
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+
11
+ # Create a csv string from the data
12
+ #
13
+ def generate(opts={})
14
+ nd = normalized_data
15
+ ::CSV.generate(opts) do |csv|
16
+ # set header row
17
+ csv << nd[:headers]
18
+ nd[:values].each do |row|
19
+ csv << row
20
+ end
21
+ end
22
+ end
23
+
24
+ # Creates a hash with a :headers field
25
+ # and a :values field
26
+ #
27
+ def normalized_data
28
+ result = {}
29
+ result[:headers] = data.inject([]) do |h,a|
30
+ a.each { |k,v| h << k unless h.include?(k) }
31
+ h
32
+ end
33
+
34
+ result[:values] = data.map do |h|
35
+ row = []
36
+ h.each do |k,v|
37
+ index_of_key = result[:headers].index(k)
38
+ row[index_of_key] = v
39
+ end
40
+ row
41
+ end
42
+ result
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Tabler
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tabler.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "tabler/version"
2
+ require "tabler/csv"
3
+
4
+ module Tabler
5
+ def self.generate(data, opts={})
6
+ Tabler::CSV.new(data).generate(opts)
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+ require "tabler/csv"
3
+
4
+ require "csv"
5
+
6
+ describe Tabler::CSV do
7
+ let(:data) {
8
+ [
9
+ { :col1 => "val 1", :col2 => "val 2"},
10
+ { :col1 => "row 2 val 1", :col2 => "row 2 val 2"},
11
+ { :col1 => "row 3 val 1", :col3 => "row 3 val 3", :col2 => "row 3 val 2"},
12
+ { :col1 => "row 4 val 1", :col3 => "row 4 val 3" }
13
+ ]
14
+ }
15
+
16
+ describe "#generate" do
17
+ subject do
18
+ csv_string = Tabler::CSV.new(data).generate(:col_sep => "\t")
19
+ CSV.parse(csv_string, :col_sep => "\t", :headers => true)
20
+ end
21
+
22
+ it 'sets the column headers' do
23
+ subject.headers.should == ["col1", "col2", "col3"]
24
+ end
25
+
26
+ it 'contains all the rows' do
27
+ row_count = 0
28
+ subject.each { row_count += 1 }
29
+ row_count.should == 4
30
+ end
31
+ end
32
+
33
+ describe ".normalized_data" do
34
+ subject do
35
+ Tabler::CSV.new(data).normalized_data
36
+ end
37
+
38
+ it 'sets the :headers' do
39
+ subject[:headers].should == [:col1, :col2, :col3]
40
+ end
41
+
42
+ it 'create a row in :values for each row in the data' do
43
+ subject[:values].size.should == 4
44
+ end
45
+
46
+ it 'creates the correct values for each row' do
47
+ subject[:values][0].should == ["val 1", "val 2"]
48
+ subject[:values][1].should == ["row 2 val 1", "row 2 val 2"]
49
+ subject[:values][2].should == ["row 3 val 1", "row 3 val 2", "row 3 val 3"]
50
+ subject[:values][3].should == ["row 4 val 1", nil, "row 4 val 3"]
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,10 @@
1
+ require "spec_helper"
2
+ require "tabler"
3
+
4
+ describe Tabler do
5
+ describe ".generate" do
6
+ it 'returns a CSV string' do
7
+ Tabler.generate([{ :col1 => "val 1", :col2 => "val 2" }], :col_sep => ";").should == "col1;col2\nval 1;val 2\n"
8
+ end
9
+ end
10
+ end
data/tabler.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tabler/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tabler"
7
+ s.version = Tabler::VERSION
8
+ s.authors = ["Klaas Speller"]
9
+ s.email = ["klaasspeller@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Create csv a hash}
12
+ s.description = %q{Create csv data from a hash of an array of hashes}
13
+
14
+ s.rubyforge_project = "tabler"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+
24
+ # s.add_runtime_dependency "rest-client"
25
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Klaas Speller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70131602845460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70131602845460
25
+ description: Create csv data from a hash of an array of hashes
26
+ email:
27
+ - klaasspeller@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - lib/tabler.rb
39
+ - lib/tabler/csv.rb
40
+ - lib/tabler/version.rb
41
+ - spec/spec_helper.rb
42
+ - spec/tabler/csv_spec.rb
43
+ - spec/tabler_spec.rb
44
+ - tabler.gemspec
45
+ homepage: ''
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project: tabler
65
+ rubygems_version: 1.8.11
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Create csv a hash
69
+ test_files:
70
+ - spec/spec_helper.rb
71
+ - spec/tabler/csv_spec.rb
72
+ - spec/tabler_spec.rb