yaml2csv 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/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.swp
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ group :development do
5
+ gem 'rspec'
6
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yaml2csv (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rake (0.8.7)
11
+ rspec (2.1.0)
12
+ rspec-core (~> 2.1.0)
13
+ rspec-expectations (~> 2.1.0)
14
+ rspec-mocks (~> 2.1.0)
15
+ rspec-core (2.1.0)
16
+ rspec-expectations (2.1.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.1.0)
19
+ ya2yaml (0.30)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.7)
26
+ rake (~> 0.8.7)
27
+ rspec
28
+ ya2yaml (~> 0.30)
29
+ yaml2csv!
data/README ADDED
@@ -0,0 +1,48 @@
1
+ = Yaml2Csv
2
+
3
+ Transform YAML file into CSV and backwards. CSV files contain
4
+ triplets [path, key, value]. For example:
5
+
6
+ path1:
7
+ path11:
8
+ key11a: value11a
9
+ key11b: value11b
10
+ path12:
11
+ path121:
12
+ key121a: value121a
13
+ path2:
14
+ key2a: value2a
15
+
16
+ Will be converted into:
17
+
18
+ path1/path11,key11a,value11a
19
+ path1/path11,key11b,value11b
20
+ path1/path12/path121,key121a,value121a
21
+ path2,key2a,value2a
22
+
23
+ YAML source files should contain only hashes and string values. While non-string
24
+ values (i.e. arrays, booleans) are allowed, they will be treated as strings
25
+ thus their original format will be lost.
26
+
27
+ == Usage
28
+
29
+ === As a gem
30
+
31
+ In your Gemfile:
32
+
33
+ gem 'yaml2csv'
34
+
35
+ In your code:
36
+
37
+ output_string = Yaml2csv::yaml2csv(input_string)
38
+ output_string = Yaml2csv::csv2yaml(input_string)
39
+
40
+ === rake task
41
+
42
+ Convert file.yml into CSV format:
43
+
44
+ $ rake yaml2csv:yaml2csv INPUT=file.yml OUTPUT=file.csv
45
+
46
+ Convert file.csv into YAML format
47
+
48
+ $ rake yaml2csv:csv2yaml INPUT=file.csv OUTPUT=file.yml
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ require 'bundler/setup'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rake'
6
+ require "rspec/core/rake_task"
7
+
8
+ require File.expand_path("../lib/tasks/yaml2csv", __FILE__)
9
+
10
+ RSpec::Core::RakeTask.new(:spec)
11
+
12
+ task :default => [:spec]
@@ -0,0 +1,4 @@
1
+ path1/path11,key11a,value11a
2
+ path1/path11,key11b,value11b
3
+ path1/path12/path121,key121a,value121a
4
+ path2,key2a,value2a
@@ -0,0 +1,10 @@
1
+ ---
2
+ path1:
3
+ path11:
4
+ key11a: value11a
5
+ key11b: value11b
6
+ path12:
7
+ path121:
8
+ key121a: value121a
9
+ path2:
10
+ key2a: value2a
@@ -0,0 +1,25 @@
1
+ require 'yaml2csv'
2
+
3
+ namespace :yaml2csv do
4
+ desc "Convert a yaml into a csv file"
5
+ task :yaml2csv do
6
+ input = File.read(ENV['INPUT'])
7
+ csv_output = Yaml2csv::yaml2csv(input)
8
+ if !ENV['OUTPUT'].nil?
9
+ File.open(ENV['OUTPUT'], 'w') {|f| f.write(csv_output) }
10
+ else
11
+ puts csv_output
12
+ end
13
+ end
14
+
15
+ desc "Convert a csv into a yaml file"
16
+ task :csv2yaml do
17
+ input = File.read(ENV['INPUT'])
18
+ yaml_output = Yaml2csv::csv2yaml(input)
19
+ if !ENV['OUTPUT'].nil?
20
+ File.open(ENV['OUTPUT'], 'w') {|f| f.write(yaml_output) }
21
+ else
22
+ puts yaml_output
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,57 @@
1
+ module Yaml2csv
2
+ module HashExtensions
3
+ # Merge a hash with a (keys_path, value)
4
+ #
5
+ # hash = {1=>2}
6
+ # hash.merge_path([3,4], 5) #=> {1=>2, 3=>{4=>5}}
7
+ #
8
+ def merge_path(keys, value)
9
+ keys[0...-1].inject(self) do |result, key|
10
+ result[key] ||= {}
11
+ end[keys[-1]] = value
12
+ self
13
+ end
14
+
15
+ # Walk a hash yielding [path, key, value] triplets
16
+ #
17
+ # hash = {
18
+ # "a" => {
19
+ # "b" => 3,
20
+ # "c" => 5,
21
+ # },
22
+ # "d" => 7,
23
+ # }
24
+ #
25
+ # hash.to_enum(:walk).to_a
26
+ # # => [
27
+ # # [[["a"], "b", 3],
28
+ # # [["a"], "c", 5],
29
+ # # [[], "d", 7]
30
+ # # ]
31
+ #
32
+ def walk(path=[], &block)
33
+ self.each do |key, value|
34
+ if value.is_a?(Hash)
35
+ value.walk(path + [key], &block)
36
+ else
37
+ yield [path, key, value]
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ module HashClassExtensions
44
+ # Reverse a Hash#walk array output
45
+ #
46
+ def unwalk_from_array(ary)
47
+ ary.inject({}) do |output, (path, key, value)|
48
+ output.merge_path(path+[key], value)
49
+ end
50
+ end
51
+ end
52
+
53
+ def self.extend_hash(obj = Hash)
54
+ obj.send(:include, Yaml2csv::HashExtensions)
55
+ obj.send(:extend, Yaml2csv::HashClassExtensions)
56
+ end
57
+ end
data/lib/yaml2csv.rb ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/ruby
2
+ require 'rubygems'
3
+ require 'yaml'
4
+ require 'csv'
5
+ require 'ya2yaml'
6
+
7
+ require 'yaml2csv/hash_extensions'
8
+ Yaml2csv.extend_hash
9
+
10
+ module Yaml2csv
11
+ # Convert a string containing YAML data to a CSV string
12
+ def self.yaml2csv(yamldata, options = {})
13
+ hash = YAML::load(yamldata)
14
+
15
+ CSV.generate do |csv_writer|
16
+ hash.to_enum(:walk).each do |path, key, value|
17
+ strvalue = value.is_a?(String) ? value : value.inspect
18
+ csv_writer << [path.join("/"), key, strvalue]
19
+ end
20
+ end
21
+ end
22
+
23
+ # Convert a string containing CSV values to a YAML string
24
+ def self.csv2yaml(csvdata, options = {})
25
+ walk_array = []
26
+
27
+ CSV.parse(csvdata) do |row|
28
+ walk_array << [row[0].split("/").map(&:to_s), row[1].to_s, row[2].to_s]
29
+ end
30
+
31
+ hash = Hash.unwalk_from_array(walk_array)
32
+ hash.ya2yaml.gsub(/\s+$/, '') + "\n"
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ EXAMPLE_HASH = {
4
+ "a" => {
5
+ "b" => 3,
6
+ "c" => 5,
7
+ },
8
+ "d" => 7,
9
+ }
10
+
11
+ EXAMPLE_ARY = [
12
+ [["a"], "b", 3],
13
+ [["a"], "c", 5],
14
+ [[], "d", 7],
15
+ ]
16
+
17
+ describe Hash do
18
+ it "should store paths" do
19
+ {1 => 2, 3 => {4 => 5}}.should == {1 => 2}.merge_path([3, 4], 5)
20
+
21
+ {1 => 2, 3 => {4 => 5, 5 => 50}}.should == {1 => 2, 3 => {5 => 50}}.merge_path([3, 4], 5)
22
+ end
23
+
24
+ it "should walk" do
25
+ EXAMPLE_ARY.should == EXAMPLE_HASH.to_enum(:walk).to_a
26
+ end
27
+
28
+ it "should unwalk" do
29
+ EXAMPLE_HASH.should == Hash.unwalk_from_array(EXAMPLE_ARY)
30
+ end
31
+ end
@@ -0,0 +1 @@
1
+ require 'yaml2csv'
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ YAML_EXAMPLE =<<-EOF
4
+ ---
5
+ path1:
6
+ path11:
7
+ key11a: value11a
8
+ key11b: value11b
9
+ path12:
10
+ path121:
11
+ key121a: value121a
12
+ path2:
13
+ key2a: value2a
14
+ EOF
15
+
16
+ CSV_EXAMPLE = <<-EOF
17
+ path1/path11,key11a,value11a
18
+ path1/path11,key11b,value11b
19
+ path1/path12/path121,key121a,value121a
20
+ path2,key2a,value2a
21
+ EOF
22
+
23
+ describe Yaml2csv do
24
+
25
+ it "should convert yaml to csv" do
26
+ Yaml2csv::yaml2csv(YAML_EXAMPLE).should == CSV_EXAMPLE
27
+ end
28
+
29
+ it "should convert csv to yaml" do
30
+ Yaml2csv::csv2yaml(CSV_EXAMPLE).should == YAML_EXAMPLE
31
+ end
32
+
33
+ end
data/yaml2csv.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "yaml2csv"
5
+ s.homepage = "http://github.com/benhutton/yaml2csv"
6
+ s.authors = ["Ben Hutton", "Arnau Sanchez"]
7
+ s.summary = "Convert YAML to CSV (and backward)"
8
+ s.description = "yaml2csv converts between YAML and CSV files"
9
+ s.email = "benhutton@gmail.com"
10
+ s.require_path = "lib"
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- spec/*`.split("\n")
13
+
14
+ s.version = '0.0.1'
15
+ s.platform = Gem::Platform::RUBY
16
+ s.required_ruby_version = '>= 1.8.6'
17
+ s.required_rubygems_version = '>= 1.3.5'
18
+
19
+ {
20
+ 'bundler' => '~> 1.0.7',
21
+ 'rake' => '~> 0.8.7',
22
+ 'ya2yaml' => '~> 0.30',
23
+ }.each do |lib, version|
24
+ s.add_development_dependency lib, version
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml2csv
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Ben Hutton
13
+ - Arnau Sanchez
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-12 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 7
32
+ version: 1.0.7
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 8
46
+ - 7
47
+ version: 0.8.7
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: ya2yaml
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ - 30
61
+ version: "0.30"
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ description: yaml2csv converts between YAML and CSV files
66
+ email: benhutton@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files: []
72
+
73
+ files:
74
+ - .gitignore
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - README
78
+ - Rakefile
79
+ - examples/example.csv
80
+ - examples/example.yml
81
+ - lib/tasks/yaml2csv.rb
82
+ - lib/yaml2csv.rb
83
+ - lib/yaml2csv/hash_extensions.rb
84
+ - spec/hash_extensions_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/yaml2csv_spec.rb
87
+ - yaml2csv.gemspec
88
+ has_rdoc: true
89
+ homepage: http://github.com/benhutton/yaml2csv
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 1
104
+ - 8
105
+ - 6
106
+ version: 1.8.6
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ segments:
113
+ - 1
114
+ - 3
115
+ - 5
116
+ version: 1.3.5
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.3.7
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Convert YAML to CSV (and backward)
124
+ test_files:
125
+ - spec/hash_extensions_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/yaml2csv_spec.rb