easycsv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88fb5ff3bb9bbb9fbda05d3bb4240d497de6fc9e
4
+ data.tar.gz: 377a3a4903ff82e57ee19aeaea0908215b8a4496
5
+ SHA512:
6
+ metadata.gz: 8f920a498f0ba6ba049b8874a16fd5d7e12e59f4eb8249f5269aaac83a74d179aad4a345a42adca9dede5dd4aa5bda9e14056b42dc74458718f3c591b4ce8f09
7
+ data.tar.gz: 0f0a29761904bdbe1cdb388a096c63cf596c6bd133c707f4a4470a23f9995272e81b6cc8e2cb973fbaa26b4fb0b9c43bfb0383aa1f45405e51d3e33614c7f470
@@ -0,0 +1,60 @@
1
+ require_relative "data"
2
+
3
+ module EasyCSV
4
+ class Builder
5
+ attr_accessor :wrapper, :separator, :file_path, :data
6
+ def self.build(&block)
7
+ builder = new
8
+ builder.instance_eval(&block)
9
+ builder.build
10
+ end
11
+
12
+ def initialize
13
+ @data = Data.new
14
+ @wrapper = DoubleQuoteWrapper
15
+ @separator = ComaSeparator
16
+ end
17
+
18
+ def build
19
+ File.open(@file_path, "w") do |f|
20
+ f.write header_row
21
+ f.write data_rows
22
+ end
23
+ end
24
+
25
+ def set_column_separator(separator)
26
+ @separator = separator
27
+ end
28
+
29
+ def set_path(path)
30
+ @file_path = path
31
+ end
32
+
33
+ def method_missing(m, *args, &block)
34
+ @data.send(m, *args, &block)
35
+ end
36
+
37
+ private
38
+
39
+ def header_row
40
+ separate( wrap(@data.headers) ) + "\n"
41
+ end
42
+
43
+ def data_rows
44
+ @data.rows.map{|r| build_row(r)}.join "\n"
45
+ end
46
+
47
+ def build_row(r)
48
+ row = @data.calls.map{|c| r.send(c)}
49
+ separate( wrap(row) )
50
+ end
51
+
52
+ def wrap(row)
53
+ @wrapper.new(row).wrap
54
+ end
55
+
56
+ def separate(row)
57
+ @separator.new(row).separate
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,11 @@
1
+ module EasyCSV; class ComaSeparator; end; end
2
+
3
+ class EasyCSV::ComaSeparator
4
+ def initialize(row)
5
+ @row = row
6
+ end
7
+
8
+ def separate
9
+ @row.join(',')
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ require "delegate"
2
+
3
+ module EasyCSV
4
+ class Data < Struct.new(:columns, :rows)
5
+ def initialize
6
+ super(Array.new, Array.new)
7
+ end
8
+
9
+ def headers
10
+ columns.map(&:header_name)
11
+ end
12
+
13
+ def calls
14
+ columns.map(&:method_call)
15
+ end
16
+
17
+ def add_column(header_name, method_call)
18
+ columns.push(Column.new(header_name, method_call))
19
+ end
20
+
21
+ def add_columns(column_pairs)
22
+ column_pairs.each {|pair| add_column(*pair)}
23
+ end
24
+
25
+ def add_row(data_model)
26
+ rows.push(Row.new(data_model))
27
+ end
28
+
29
+ def add_rows(data_models)
30
+ data_models.each{|d| add_row(d)}
31
+ end
32
+ end
33
+
34
+ Column = Struct.new(:method_call, :header_name)
35
+ class Row < SimpleDelegator; end
36
+ end
@@ -0,0 +1,11 @@
1
+ module EasyCSV; class DoubleQuoteWrapper; end; end
2
+
3
+ class EasyCSV::DoubleQuoteWrapper
4
+ def initialize(row)
5
+ @row = row
6
+ end
7
+
8
+ def wrap
9
+ @row.map{|e| "\"#{e}\""}
10
+ end
11
+ end
data/lib/easycsv.rb ADDED
@@ -0,0 +1,3 @@
1
+ Dir["#{File.dirname(__FILE__)}/**/*.rb"].each do |file|
2
+ require file
3
+ end
@@ -0,0 +1,83 @@
1
+ require_relative "../../lib/easycsv/builder"
2
+
3
+ describe EasyCSV::Builder do
4
+ let(:builder) { described_class.new }
5
+
6
+ describe "#wrapper" do
7
+ subject { builder.wrapper }
8
+
9
+ context "default" do
10
+ it { should eq EasyCSV::DoubleQuoteWrapper }
11
+ end
12
+
13
+ context "setting a separator" do
14
+ let(:wrapper) { Class.new }
15
+ before { builder.wrapper = wrapper }
16
+ it { should eq wrapper }
17
+ end
18
+ end
19
+
20
+ describe "#separator" do
21
+ subject { builder.separator }
22
+
23
+ context "default" do
24
+ it { should eq EasyCSV::ComaSeparator }
25
+ end
26
+
27
+ context "setting a separator" do
28
+ let(:separator) { Class.new }
29
+ before { builder.separator = separator }
30
+ it { should eq separator }
31
+ end
32
+ end
33
+
34
+ describe "#file_path" do
35
+ subject { builder.file_path }
36
+
37
+ context "with a path '~/csv.csv'" do
38
+ before { builder.file_path = "~/csv.csv" }
39
+ it { should eq "~/csv.csv" }
40
+ end
41
+ end
42
+
43
+ describe "#build" do
44
+ let(:path) { "/tmp/test_file.csv" }
45
+ let(:object_1) { double(foo: "Bar", herp: "Derp") }
46
+ let(:object_2) { double(foo: "A", herp: "B") }
47
+ let(:result) { "\"Foo\",\"Herp\"\n\"Bar\",\"Derp\"\n\"A\",\"B\"" }
48
+
49
+ after { `rm #{path}` }
50
+
51
+ context "normal mode" do
52
+ it "should write the file" do
53
+ builder.file_path = path
54
+ builder.add_column(:foo, "Foo")
55
+ builder.add_column(:herp, "Herp")
56
+ builder.add_row(object_1)
57
+ builder.add_row(object_2)
58
+
59
+ builder.build
60
+
61
+ File.read(path).should eq(result)
62
+ end
63
+ end
64
+
65
+ context "block mode" do
66
+ it "should write the file" do
67
+ b_path = path
68
+ b_object_1 = object_1
69
+ b_object_2 = object_2
70
+
71
+ described_class.build do
72
+ set_path(b_path)
73
+ add_column(:foo, "Foo")
74
+ add_column(:herp, "Herp")
75
+ add_row(b_object_1)
76
+ add_row(b_object_2)
77
+ end
78
+
79
+ File.read(b_path).should eq(result)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,14 @@
1
+ require_relative '../../lib/easycsv/coma_separator'
2
+
3
+ describe EasyCSV::ComaSeparator do
4
+ describe '#separate' do
5
+ subject { described_class.new(row).separate }
6
+ let(:row) { ['Foo', 'Bar'] }
7
+
8
+ context "given an array ['Foo', 'Bar']" do
9
+ let(:result) { "Foo,Bar" }
10
+
11
+ it { should eq(result) }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,78 @@
1
+ require_relative "../../lib/easycsv/data"
2
+
3
+ describe EasyCSV::Data do
4
+ let(:data) { described_class.new }
5
+
6
+ describe "#add_column" do
7
+ it "should add a column object" do
8
+ data.add_column(:call, "header name")
9
+ data.add_column(:call, "header name 2")
10
+ data.columns.count.should eq 2
11
+ end
12
+ end
13
+
14
+ describe "#add_columns" do
15
+ it "should add an array of pairs to the columns" do
16
+ data.add_columns([ [:call, "header name"], [:call2, "header name 2"] ])
17
+ data.columns.count.should eq 2
18
+ end
19
+ end
20
+
21
+ describe "#add_row" do
22
+ it "should add a row object" do
23
+ data.add_row(double)
24
+ data.add_row(double)
25
+ data.rows.count.should eq 2
26
+ end
27
+ end
28
+
29
+ describe "#add_rows" do
30
+ it "should add everything from an array to rows" do
31
+ data.add_rows( [double, double] )
32
+ data.add_rows( [double] )
33
+ data.rows.count.should eq 3
34
+ end
35
+ end
36
+
37
+ describe "#headers" do
38
+ subject { data.headers }
39
+ context "with no columns" do
40
+ let(:results) { Array.new }
41
+ it { should eq(results) }
42
+ end
43
+
44
+ context "with column with header 'Foo'" do
45
+ let(:results) { ["Foo"] }
46
+ before { data.add_column(:foo, "Foo") }
47
+ it { should eq(results) }
48
+ end
49
+
50
+ context "with columns with headers 'Foo', 'Bar'" do
51
+ let(:results) { ["Foo", "Bar"] }
52
+ before { data.add_column(:foo, "Foo") }
53
+ before { data.add_column(:foo, "Bar") }
54
+ it { should eq(results) }
55
+ end
56
+ end
57
+
58
+ describe "#calls" do
59
+ subject { data.calls }
60
+ context "with no columns" do
61
+ let(:results) { Array.new }
62
+ it { should eq(results) }
63
+ end
64
+
65
+ context "with column with call 'Foo'" do
66
+ let(:results) { [:foo] }
67
+ before { data.add_column(:foo, "Foo") }
68
+ it { should eq(results) }
69
+ end
70
+
71
+ context "with columns with calls :foo, :bar" do
72
+ let(:results) { [:foo, :bar] }
73
+ before { data.add_column(:foo, "Foo") }
74
+ before { data.add_column(:bar, "Bar") }
75
+ it { should eq(results) }
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,14 @@
1
+ require_relative '../../lib/easycsv/double_quote_wrapper'
2
+
3
+ describe EasyCSV::DoubleQuoteWrapper do
4
+ describe "#wrap" do
5
+ subject { described_class.new(row).wrap }
6
+ let(:row) { ["entry", "yrtne"] }
7
+
8
+ context 'given an array ["entry", "yrtne"]' do
9
+ let(:result) { ["\"entry\"", "\"yrtne\""] }
10
+
11
+ it { should eq(result) }
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easycsv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Stannard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: easier csv creation
14
+ email: kwstannard@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/easycsv/builder.rb
20
+ - lib/easycsv/coma_separator.rb
21
+ - lib/easycsv/data.rb
22
+ - lib/easycsv/double_quote_wrapper.rb
23
+ - lib/easycsv.rb
24
+ - spec/easycsv/builder_spec.rb
25
+ - spec/easycsv/coma_separator_spec.rb
26
+ - spec/easycsv/data_spec.rb
27
+ - spec/easycsv/double_quote_wrapper_spec.rb
28
+ homepage: https://github.com/lendkey/cassava
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.0.3
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: easier csv creation
52
+ test_files:
53
+ - spec/easycsv/builder_spec.rb
54
+ - spec/easycsv/coma_separator_spec.rb
55
+ - spec/easycsv/data_spec.rb
56
+ - spec/easycsv/double_quote_wrapper_spec.rb