active_report 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -6
- data/.rspec +3 -3
- data/LICENSE.txt +17 -18
- data/README.md +26 -8
- data/active_report.gemspec +5 -4
- data/bin/console +14 -0
- data/bin/rake +16 -0
- data/bin/setup +7 -0
- data/lib/active_report/array.rb +5 -5
- data/lib/active_report/hash.rb +18 -16
- data/lib/active_report/record.rb +17 -2
- data/lib/active_report/version.rb +1 -1
- metadata +9 -40
- data/spec/lib/array_spec.rb +0 -104
- data/spec/lib/hash_spec.rb +0 -154
- data/spec/sample/multi_all.csv +0 -4
- data/spec/sample/multi_except.csv +0 -4
- data/spec/sample/multi_headerless.csv +0 -3
- data/spec/sample/multi_headerless_options.csv +0 -3
- data/spec/sample/multi_headers.csv +0 -4
- data/spec/sample/multi_only.csv +0 -4
- data/spec/sample/multi_options.csv +0 -4
- data/spec/sample/solo_all.csv +0 -2
- data/spec/sample/solo_except.csv +0 -2
- data/spec/sample/solo_headerless.csv +0 -1
- data/spec/sample/solo_headerless_options.csv +0 -1
- data/spec/sample/solo_headers.csv +0 -2
- data/spec/sample/solo_only.csv +0 -2
- data/spec/sample/solo_options.csv +0 -2
- data/spec/spec_helper.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37f10deede44a50d38076ffabed636ee17ef30f4
|
4
|
+
data.tar.gz: 8ab4b879e20c5334d13e2342bf73643fcd453c2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fc0c00963384824b860794270e92dadf16f2b6f1745722afd5839d85806957f756747d14c017456e168658af07a66f643237e2e4aa40cbed7819f835829bb34
|
7
|
+
data.tar.gz: 8509952f25685019875416f361071106c95f7bc74c17e474e8051b7d0850f7848e59ced45b2383b7e4235d265d428b0c1d565b332d77c5e926496748ac396fa0
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/LICENSE.txt
CHANGED
@@ -1,22 +1,21 @@
|
|
1
|
-
|
1
|
+
The MIT License (MIT)
|
2
2
|
|
3
|
-
|
3
|
+
Copyright (c) 2015 Juan Gomez
|
4
4
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
the following conditions:
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
12
11
|
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
15
14
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
OF
|
22
|
-
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -22,9 +22,13 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
$ gem install active_report
|
24
24
|
|
25
|
-
##
|
25
|
+
## Table of Contents
|
26
26
|
|
27
|
-
|
27
|
+
* [Array](#array)
|
28
|
+
* [Hash](#hash)
|
29
|
+
* [Record](#record)
|
30
|
+
|
31
|
+
## Array
|
28
32
|
|
29
33
|
**Export:** Convert an array or array of arrays to a CSV.
|
30
34
|
|
@@ -54,13 +58,13 @@ ActiveReport::Array.import("sample.csv")
|
|
54
58
|
ActiveReport::Array.import("sample.csv", headers: ["ID", "Task", "Completed"], options: { col_sep: ";" })
|
55
59
|
```
|
56
60
|
|
57
|
-
|
61
|
+
## Hash
|
58
62
|
|
59
63
|
**Export:** Convert a hash or an array of hashes to a CSV.
|
60
64
|
|
61
65
|
**Options:**
|
62
|
-
* only:
|
63
|
-
* except:
|
66
|
+
* only: keys of pairs to be used on generation
|
67
|
+
* except: keys of pairs not to be used on generation
|
64
68
|
* headers: column titles of CSV data
|
65
69
|
* options: CSV options to be use on generation
|
66
70
|
|
@@ -78,8 +82,8 @@ ActiveReport::Hash.export(@list, only: [:id, :item], headers: ["ID", "Task"], op
|
|
78
82
|
**Import:** Convert a CSV into an array of hashes.
|
79
83
|
|
80
84
|
**Options:**
|
81
|
-
* only:
|
82
|
-
* except:
|
85
|
+
* only: keys of pairs to be used on generation
|
86
|
+
* except: keys of pairs not to be used on generation
|
83
87
|
* headers: column titles of CSV data **Required**
|
84
88
|
* options: CSV options to be use on parsing
|
85
89
|
|
@@ -88,7 +92,7 @@ ActiveReport::Hash.import("sample.csv")
|
|
88
92
|
ActiveReport::Hash.import("sample.csv", except: :completed, headers: ["ID", "Task"], options: { col_sep: ";" })
|
89
93
|
```
|
90
94
|
|
91
|
-
|
95
|
+
## Record
|
92
96
|
|
93
97
|
**Export:** Convert an ActiveRecord/Relation object(s) to a CSV.
|
94
98
|
|
@@ -109,6 +113,20 @@ ActiveReport::Record.export(@list)
|
|
109
113
|
ActiveReport::Record.export(@list, only: [:id, :item], headers: ["ID", "Task"], options: { col_sep: ";" })
|
110
114
|
```
|
111
115
|
|
116
|
+
**Import:** Create new database records from a CSV.
|
117
|
+
|
118
|
+
**Options:**
|
119
|
+
* model: model of objects being generated
|
120
|
+
* only: keys of pairs to be used on generation
|
121
|
+
* except: keys of pairs not to be used on generation
|
122
|
+
* headers: column titles of CSV data **Required**
|
123
|
+
* options: CSV options to be use on parsing
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
ActiveReport::Record.import("sample.csv", User)
|
127
|
+
ActiveReport::Record.import("sample.csv", User, except: :completed, headers: ["ID", "Task"], options: { col_sep: ";" })
|
128
|
+
```
|
129
|
+
|
112
130
|
## Contributing
|
113
131
|
|
114
132
|
1. Fork it ( https://github.com/[my-github-username]/active_report/fork )
|
data/active_report.gemspec
CHANGED
@@ -8,14 +8,15 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = ActiveReport::VERSION
|
9
9
|
spec.authors = ["Juan Gomez"]
|
10
10
|
spec.email = ["j.gomez@drexed.com"]
|
11
|
+
|
11
12
|
spec.summary = %q{Export or import ruby objects to flat files vice versa.}
|
12
13
|
spec.description = %q{Export or import data from multiple input formats such as arrays, hashes, and active record or vice versa.}
|
13
|
-
spec.homepage = ""
|
14
|
+
spec.homepage = "https://github.com/drexed/active_report"
|
14
15
|
spec.license = "MIT"
|
15
16
|
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.
|
18
|
-
spec.
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
20
|
spec.require_paths = ["lib"]
|
20
21
|
|
21
22
|
spec.add_development_dependency "bundler"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_report"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/rake
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rake' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rake', 'rake')
|
data/bin/setup
ADDED
data/lib/active_report/array.rb
CHANGED
@@ -12,6 +12,10 @@ class ActiveReport::Array
|
|
12
12
|
new(datum, headers: headers, options: options).export
|
13
13
|
end
|
14
14
|
|
15
|
+
def self.import(datum, headers: nil, options: {})
|
16
|
+
new(datum, headers: headers, options: options).import
|
17
|
+
end
|
18
|
+
|
15
19
|
def export
|
16
20
|
@datum = [].push(@datum).compact unless @datum.first.is_a?(Array)
|
17
21
|
|
@@ -21,16 +25,12 @@ class ActiveReport::Array
|
|
21
25
|
end
|
22
26
|
end
|
23
27
|
|
24
|
-
def self.import(datum, headers: nil, options: {})
|
25
|
-
new(datum, headers: headers, options: options).import
|
26
|
-
end
|
27
|
-
|
28
28
|
def import
|
29
29
|
processed_datum = [].push(@headers).compact
|
30
30
|
CSV.foreach(@datum, @options) do |data|
|
31
31
|
processed_datum.push(data)
|
32
32
|
end
|
33
|
-
processed_datum.count < 2 ? processed_datum.flatten : processed_datum
|
33
|
+
return(processed_datum.count < 2 ? processed_datum.flatten : processed_datum)
|
34
34
|
end
|
35
35
|
|
36
36
|
end
|
data/lib/active_report/hash.rb
CHANGED
@@ -14,6 +14,10 @@ class ActiveReport::Hash
|
|
14
14
|
new(datum, only: only, except: except, headers: headers, options: options).export
|
15
15
|
end
|
16
16
|
|
17
|
+
def self.import(datum, only: nil, except: nil, headers: nil, options: {})
|
18
|
+
new(datum, only: only, except: except, headers: headers, options: options).import
|
19
|
+
end
|
20
|
+
|
17
21
|
def export
|
18
22
|
@datum = [].push(@datum).compact unless @datum.is_a?(Array)
|
19
23
|
@only = [].push(@only).compact unless @only.is_a?(Array)
|
@@ -32,29 +36,27 @@ class ActiveReport::Hash
|
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
35
|
-
def self.import(datum, only: nil, except: nil, headers: nil, options: {})
|
36
|
-
new(datum, only: only, except: except, headers: headers, options: options).import
|
37
|
-
end
|
38
|
-
|
39
39
|
def import
|
40
|
-
return(nil) if @headers.nil?
|
41
|
-
|
42
40
|
@only = [].push(@only).compact unless @only.is_a?(Array)
|
43
41
|
@except = [].push(@except).compact unless @except.is_a?(Array)
|
44
42
|
|
45
43
|
processed_datum = []
|
46
|
-
CSV.foreach(@datum, @options) do |data|
|
47
|
-
|
48
|
-
|
49
|
-
|
44
|
+
CSV.foreach(@datum, @options).each_with_index do |data, line|
|
45
|
+
if @headers.nil? && line.zero?
|
46
|
+
@headers = data
|
47
|
+
else
|
48
|
+
processed_data = {}
|
49
|
+
@headers.each_with_index do |v,i|
|
50
|
+
processed_data.store(v.to_s, data.fetch(i, nil) )
|
51
|
+
end
|
52
|
+
|
53
|
+
processed_data.keep_if { |k,v| @only.include?(k) } unless @only.empty?
|
54
|
+
processed_data.delete_if { |k,v| @except.include?(k) } unless @except.empty?
|
55
|
+
|
56
|
+
processed_datum.push(processed_data)
|
50
57
|
end
|
51
|
-
|
52
|
-
processed_data.keep_if { |k,v| @only.include?(k) } unless @only.empty?
|
53
|
-
processed_data.delete_if { |k,v| @except.include?(k) } unless @except.empty?
|
54
|
-
|
55
|
-
processed_datum.push(processed_data)
|
56
58
|
end
|
57
|
-
processed_datum
|
59
|
+
return(processed_datum)
|
58
60
|
end
|
59
61
|
|
60
62
|
end
|
data/lib/active_report/record.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require "json"
|
2
2
|
class ActiveReport::Record
|
3
3
|
|
4
|
-
attr_accessor :datum, :only, :except, :headers, :options
|
4
|
+
attr_accessor :datum, :model, :only, :except, :headers, :options
|
5
5
|
|
6
|
-
def initialize(datum, only: nil, except: nil, headers: nil, options: {})
|
6
|
+
def initialize(datum, model: nil, only: nil, except: nil, headers: nil, options: {})
|
7
7
|
@datum = datum
|
8
|
+
@model = model
|
8
9
|
@only = only
|
9
10
|
@except = except
|
10
11
|
@headers = headers
|
@@ -15,6 +16,10 @@ class ActiveReport::Record
|
|
15
16
|
new(datum, only: only, except: except, headers: headers, options: options).export
|
16
17
|
end
|
17
18
|
|
19
|
+
def self.import(datum, model: nil, only: nil, except: nil, headers: nil, options: {})
|
20
|
+
new(datum, model: model, only: only, except: except, headers: headers, options: options).import
|
21
|
+
end
|
22
|
+
|
18
23
|
def export
|
19
24
|
@datum = @datum.is_a?(ActiveRecord::Relation) ? JSON.parse(@datum.to_json).flatten : [].push(@datum.attributes).compact
|
20
25
|
@only = (@only.is_a?(Array) ? @only : [].push(@only).compact).map(&:to_s)
|
@@ -33,4 +38,14 @@ class ActiveReport::Record
|
|
33
38
|
end
|
34
39
|
end
|
35
40
|
|
41
|
+
def import
|
42
|
+
if @model.nil? || (@model.superclass != ActiveRecord::Base)
|
43
|
+
raise ArgumentError,
|
44
|
+
"Model must be an ActiveRecord::Base object."
|
45
|
+
end
|
46
|
+
|
47
|
+
processed_datum = ActiveReport::Hash.import(@datum, headers: @headers, options: @options)
|
48
|
+
processed_datum.each { |data| @model.create(data) }
|
49
|
+
end
|
50
|
+
|
36
51
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_report
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,29 +83,15 @@ files:
|
|
83
83
|
- README.md
|
84
84
|
- Rakefile
|
85
85
|
- active_report.gemspec
|
86
|
+
- bin/console
|
87
|
+
- bin/rake
|
88
|
+
- bin/setup
|
86
89
|
- lib/active_report.rb
|
87
90
|
- lib/active_report/array.rb
|
88
91
|
- lib/active_report/hash.rb
|
89
92
|
- lib/active_report/record.rb
|
90
93
|
- lib/active_report/version.rb
|
91
|
-
|
92
|
-
- spec/lib/hash_spec.rb
|
93
|
-
- spec/sample/multi_all.csv
|
94
|
-
- spec/sample/multi_except.csv
|
95
|
-
- spec/sample/multi_headerless.csv
|
96
|
-
- spec/sample/multi_headerless_options.csv
|
97
|
-
- spec/sample/multi_headers.csv
|
98
|
-
- spec/sample/multi_only.csv
|
99
|
-
- spec/sample/multi_options.csv
|
100
|
-
- spec/sample/solo_all.csv
|
101
|
-
- spec/sample/solo_except.csv
|
102
|
-
- spec/sample/solo_headerless.csv
|
103
|
-
- spec/sample/solo_headerless_options.csv
|
104
|
-
- spec/sample/solo_headers.csv
|
105
|
-
- spec/sample/solo_only.csv
|
106
|
-
- spec/sample/solo_options.csv
|
107
|
-
- spec/spec_helper.rb
|
108
|
-
homepage: ''
|
94
|
+
homepage: https://github.com/drexed/active_report
|
109
95
|
licenses:
|
110
96
|
- MIT
|
111
97
|
metadata: {}
|
@@ -125,25 +111,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
111
|
version: '0'
|
126
112
|
requirements: []
|
127
113
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.4.
|
114
|
+
rubygems_version: 2.4.8
|
129
115
|
signing_key:
|
130
116
|
specification_version: 4
|
131
117
|
summary: Export or import ruby objects to flat files vice versa.
|
132
|
-
test_files:
|
133
|
-
- spec/lib/array_spec.rb
|
134
|
-
- spec/lib/hash_spec.rb
|
135
|
-
- spec/sample/multi_all.csv
|
136
|
-
- spec/sample/multi_except.csv
|
137
|
-
- spec/sample/multi_headerless.csv
|
138
|
-
- spec/sample/multi_headerless_options.csv
|
139
|
-
- spec/sample/multi_headers.csv
|
140
|
-
- spec/sample/multi_only.csv
|
141
|
-
- spec/sample/multi_options.csv
|
142
|
-
- spec/sample/solo_all.csv
|
143
|
-
- spec/sample/solo_except.csv
|
144
|
-
- spec/sample/solo_headerless.csv
|
145
|
-
- spec/sample/solo_headerless_options.csv
|
146
|
-
- spec/sample/solo_headers.csv
|
147
|
-
- spec/sample/solo_only.csv
|
148
|
-
- spec/sample/solo_options.csv
|
149
|
-
- spec/spec_helper.rb
|
118
|
+
test_files: []
|
data/spec/lib/array_spec.rb
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe ActiveReport::Array do
|
4
|
-
|
5
|
-
sample_header = ["Id", "Name", "Speed", "Hp", "Crash safety rated", "Created at"]
|
6
|
-
sample_header_alt = ["No.", "Model", "Speed", "Horse Power", "Crash Safety Rated", "Driven On"]
|
7
|
-
sample_content = ["1", "Porche", "225", "430", "true", "2014-08-22T20:59:34.000Z"]
|
8
|
-
sample_content_alt = [
|
9
|
-
["1", "Ferrari", "235", "630", "true", "2014-08-23T20:59:34.000Z" ],
|
10
|
-
["2", "Lamborghini", "245", "720", "true", "2014-08-24T20:59:34.000Z" ],
|
11
|
-
["3", "Bugatti", "256", "1001", "false", "2014-08-25T20:59:34.000Z" ]
|
12
|
-
]
|
13
|
-
|
14
|
-
context 'export to csv without headers for an' do
|
15
|
-
it 'array of arrays' do
|
16
|
-
sample_csv = File.read('spec/sample/multi_headerless.csv')
|
17
|
-
constructed_csv = ActiveReport::Array.export(sample_content_alt)
|
18
|
-
|
19
|
-
expect(constructed_csv).to eq(sample_csv)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'array' do
|
23
|
-
sample_csv = File.read('spec/sample/solo_headerless.csv')
|
24
|
-
constructed_csv = ActiveReport::Array.export(sample_content)
|
25
|
-
|
26
|
-
expect(constructed_csv).to eq(sample_csv)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'export to csv with headers for an' do
|
31
|
-
it 'array of arrays' do
|
32
|
-
sample_csv = File.read('spec/sample/multi_headers.csv')
|
33
|
-
constructed_csv = ActiveReport::Array.export(sample_content_alt, headers: sample_header_alt)
|
34
|
-
|
35
|
-
expect(constructed_csv).to eq(sample_csv)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'array' do
|
39
|
-
sample_csv = File.read('spec/sample/solo_headers.csv')
|
40
|
-
constructed_csv = ActiveReport::Array.export(sample_content, headers: sample_header_alt)
|
41
|
-
|
42
|
-
expect(constructed_csv).to eq(sample_csv)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'export to csv with options for an' do
|
47
|
-
it 'array of arrays' do
|
48
|
-
sample_csv = File.read('spec/sample/multi_options.csv')
|
49
|
-
constructed_csv = ActiveReport::Array.export(sample_content_alt, headers: sample_header, options: { col_sep: ";" })
|
50
|
-
|
51
|
-
expect(constructed_csv).to eq(sample_csv)
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'array' do
|
55
|
-
sample_csv = File.read('spec/sample/solo_options.csv')
|
56
|
-
constructed_csv = ActiveReport::Array.export(sample_content, headers: sample_header, options: { col_sep: ";" })
|
57
|
-
|
58
|
-
expect(constructed_csv).to eq(sample_csv)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'import csv without headers returns an' do
|
63
|
-
it 'array of arrays' do
|
64
|
-
constructed_array = ActiveReport::Array.import('spec/sample/multi_headerless.csv')
|
65
|
-
|
66
|
-
expect(constructed_array).to eq(sample_content_alt)
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'array' do
|
70
|
-
constructed_array = ActiveReport::Array.import('spec/sample/solo_headerless.csv')
|
71
|
-
|
72
|
-
expect(constructed_array).to eq(sample_content)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
context 'import csv with headers returns an' do
|
77
|
-
it 'array of arrays' do
|
78
|
-
constructed_array = ActiveReport::Array.import('spec/sample/multi_headerless.csv', headers: sample_header)
|
79
|
-
|
80
|
-
expect(constructed_array).to eq(sample_content_alt.dup.unshift(sample_header))
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'array' do
|
84
|
-
constructed_array = ActiveReport::Array.import('spec/sample/solo_headerless.csv', headers: sample_header)
|
85
|
-
|
86
|
-
expect(constructed_array).to eq([].push(sample_header).push(sample_content))
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
context 'import csv with options returns an' do
|
91
|
-
it 'array of arrays' do
|
92
|
-
constructed_array = ActiveReport::Array.import('spec/sample/multi_options.csv', options: { col_sep: ";" })
|
93
|
-
|
94
|
-
expect(constructed_array).to eq([].push(sample_header).concat(sample_content_alt))
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'array' do
|
98
|
-
constructed_array = ActiveReport::Array.import('spec/sample/solo_options.csv', options: { col_sep: ";" })
|
99
|
-
|
100
|
-
expect(constructed_array).to eq([].push(sample_header).push(sample_content))
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
data/spec/lib/hash_spec.rb
DELETED
@@ -1,154 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe ActiveReport::Hash do
|
4
|
-
|
5
|
-
sample_header = ["Id", "Name", "Speed", "Hp", "Crash safety rated", "Created at"]
|
6
|
-
sample_header_alt = ["No.", "Model", "Speed", "Horse Power", "Crash Safety Rated", "Driven On"]
|
7
|
-
sample_content = { id: "1", name: "Porche", speed: "225", hp: "430", crash_safety_rated: "true", created_at: "2014-08-22T20:59:34.000Z" }
|
8
|
-
sample_content_alt = [
|
9
|
-
{ id: "1", name: "Ferrari", speed: "235", hp: "630", crash_safety_rated: "true", created_at: "2014-08-23T20:59:34.000Z" },
|
10
|
-
{ id: "2", name: "Lamborghini", speed: "245", hp: "720", crash_safety_rated: "true", created_at: "2014-08-24T20:59:34.000Z" },
|
11
|
-
{ id: "3", name: "Bugatti", speed: "256", hp: "1001", crash_safety_rated: "false", created_at: "2014-08-25T20:59:34.000Z" }
|
12
|
-
]
|
13
|
-
|
14
|
-
context 'export to csv all data for an' do
|
15
|
-
it 'array of hashes' do
|
16
|
-
sample_csv = File.read('spec/sample/multi_all.csv')
|
17
|
-
constructed_csv = ActiveReport::Hash.export(sample_content_alt)
|
18
|
-
|
19
|
-
expect(constructed_csv).to eq(sample_csv)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'hash' do
|
23
|
-
sample_csv = File.read('spec/sample/solo_all.csv')
|
24
|
-
constructed_csv = ActiveReport::Hash.export(sample_content)
|
25
|
-
|
26
|
-
expect(constructed_csv).to eq(sample_csv)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'export to csv only values for an' do
|
31
|
-
it 'array of hashes' do
|
32
|
-
sample_csv = File.read('spec/sample/multi_only.csv')
|
33
|
-
constructed_csv = ActiveReport::Hash.export(sample_content_alt, only: [:id, :name])
|
34
|
-
|
35
|
-
expect(constructed_csv).to eq(sample_csv)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'hash' do
|
39
|
-
sample_csv = File.read('spec/sample/solo_only.csv')
|
40
|
-
constructed_csv = ActiveReport::Hash.export(sample_content, only: :name)
|
41
|
-
|
42
|
-
expect(constructed_csv).to eq(sample_csv)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'export to csv except values for an' do
|
47
|
-
it 'array of hashes' do
|
48
|
-
sample_csv = File.read('spec/sample/multi_except.csv')
|
49
|
-
constructed_csv = ActiveReport::Hash.export(sample_content_alt, except: [:id, :name])
|
50
|
-
|
51
|
-
expect(constructed_csv).to eq(sample_csv)
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'hash' do
|
55
|
-
sample_csv = File.read('spec/sample/solo_except.csv')
|
56
|
-
constructed_csv = ActiveReport::Hash.export(sample_content, except: :name)
|
57
|
-
|
58
|
-
expect(constructed_csv).to eq(sample_csv)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'export to csv with headers for an' do
|
63
|
-
it 'array of hashes' do
|
64
|
-
sample_csv = File.read('spec/sample/multi_headers.csv')
|
65
|
-
constructed_csv = ActiveReport::Hash.export(sample_content_alt, headers: sample_header_alt)
|
66
|
-
|
67
|
-
expect(constructed_csv).to eq(sample_csv)
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'hash' do
|
71
|
-
sample_csv = File.read('spec/sample/solo_headers.csv')
|
72
|
-
constructed_csv = ActiveReport::Hash.export(sample_content, headers: sample_header_alt)
|
73
|
-
|
74
|
-
expect(constructed_csv).to eq(sample_csv)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
context 'export to csv with options for an' do
|
79
|
-
it 'array of hashes' do
|
80
|
-
sample_csv = File.read('spec/sample/multi_options.csv')
|
81
|
-
constructed_csv = ActiveReport::Hash.export(sample_content_alt, options: { col_sep: ";" })
|
82
|
-
|
83
|
-
expect(constructed_csv).to eq(sample_csv)
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'hash' do
|
87
|
-
sample_csv = File.read('spec/sample/solo_options.csv')
|
88
|
-
constructed_csv = ActiveReport::Hash.export(sample_content, options: { col_sep: ";" })
|
89
|
-
|
90
|
-
expect(constructed_csv).to eq(sample_csv)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
context 'import csv with headers returns an' do
|
95
|
-
it 'array of arrays' do
|
96
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless.csv', headers: sample_header)
|
97
|
-
|
98
|
-
expect(constructed_array).to eq(sample_content_alt)
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'array with a hash' do
|
102
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless.csv', headers: sample_header)
|
103
|
-
|
104
|
-
expect(constructed_array).to eq([].push(sample_content))
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
context 'import csv only values returns an' do
|
109
|
-
it 'array of arrays' do
|
110
|
-
sample_array = sample_content_alt.dup.map { |v| v.dup.keep_if { |k,v| [:id, :name].include?(k) } }
|
111
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless.csv', headers: sample_header, only: [:id, :name])
|
112
|
-
|
113
|
-
expect(constructed_array).to eq(sample_array)
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'array with a hash' do
|
117
|
-
sample_array = sample_content.dup.keep_if { |k,v| [:name].include?(k) }
|
118
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless.csv', headers: sample_header, only: :name)
|
119
|
-
|
120
|
-
expect(constructed_array).to eq([].push(sample_array))
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
context 'import csv except values returns an' do
|
125
|
-
it 'array of arrays' do
|
126
|
-
sample_array = sample_content_alt.dup.map { |v| v.dup.delete_if { |k,v| [:id, :name].include?(k) } }
|
127
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless.csv', headers: sample_header, except: [:id, :name])
|
128
|
-
|
129
|
-
expect(constructed_array).to eq(sample_array)
|
130
|
-
end
|
131
|
-
|
132
|
-
it 'array with a hash' do
|
133
|
-
sample_array = sample_content.dup.delete_if { |k,v| [:name].include?(k) }
|
134
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless.csv', headers: sample_header, except: :name)
|
135
|
-
|
136
|
-
expect(constructed_array).to eq([].push(sample_array))
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
context 'import csv with options returns an' do
|
141
|
-
it 'array of hashes' do
|
142
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless_options.csv', headers: sample_header, options: { col_sep: ";" })
|
143
|
-
|
144
|
-
expect(constructed_array).to eq(sample_content_alt)
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'array with a hash' do
|
148
|
-
constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless_options.csv', headers: sample_header, options: { col_sep: ";" })
|
149
|
-
|
150
|
-
expect(constructed_array).to eq([].push(sample_content))
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
end
|
data/spec/sample/multi_all.csv
DELETED
data/spec/sample/multi_only.csv
DELETED
data/spec/sample/solo_all.csv
DELETED
data/spec/sample/solo_except.csv
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1,Porche,225,430,true,2014-08-22T20:59:34.000Z
|
@@ -1 +0,0 @@
|
|
1
|
-
1;Porche;225;430;true;2014-08-22T20:59:34.000Z
|
data/spec/sample/solo_only.csv
DELETED
data/spec/spec_helper.rb
DELETED