sepparator 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/sepparator.gemspec +1 -1
- metadata +1 -4
- data/lib/sepparator/schema_detector.rb +0 -50
- data/spec/sepparator/schema_detector_spec.rb +0 -62
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ccf7dde4f64b081fb5014eb763d95e112c79233
|
4
|
+
data.tar.gz: 4160d2d8940adcbb79112fcb672c65eeb1d14fee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dd59a02e1a3c6e27702c9bbf3f4e6f642859826f5826fc8867bcc0b24a4c79d6bc4ac976e3c55f1f3eb4ef53f8fb6aa4503e4a2d3f391cca1124cbf6dde3a69
|
7
|
+
data.tar.gz: 4175441d828bfb3a7a7cd9a78285ecdbea7845d34a71e77432c92f18ae350852c1cd98c3463747ad8299badc19590a81aec1e4c18fbbb9cc7a063dafb9618a95
|
data/Gemfile.lock
CHANGED
data/sepparator.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "sepparator"
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.2'
|
8
8
|
spec.authors = ["Andreas Brandl"]
|
9
9
|
spec.email = ["github@andreas-brandl.de"]
|
10
10
|
spec.description = %q{tool for working with CSV files, it supports conversion to Excel files and import to databases.}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sepparator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Brandl
|
@@ -143,11 +143,9 @@ files:
|
|
143
143
|
- bin/sepp
|
144
144
|
- lib/sepparator.rb
|
145
145
|
- lib/sepparator/console.rb
|
146
|
-
- lib/sepparator/schema_detector.rb
|
147
146
|
- lib/sepparator/spreadsheet_converter.rb
|
148
147
|
- sepparator.gemspec
|
149
148
|
- spec/sepparator/example.csv
|
150
|
-
- spec/sepparator/schema_detector_spec.rb
|
151
149
|
- spec/sepparator/spreadsheet_converter.rb
|
152
150
|
- spec/sepparator/spreadsheet_converter_spec.rb
|
153
151
|
- spec/spec_helper.rb
|
@@ -178,7 +176,6 @@ summary: tool for working with CSV files, it supports conversion to Excel files
|
|
178
176
|
import to databases.
|
179
177
|
test_files:
|
180
178
|
- spec/sepparator/example.csv
|
181
|
-
- spec/sepparator/schema_detector_spec.rb
|
182
179
|
- spec/sepparator/spreadsheet_converter.rb
|
183
180
|
- spec/sepparator/spreadsheet_converter_spec.rb
|
184
181
|
- spec/spec_helper.rb
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module Sepparator
|
2
|
-
|
3
|
-
class SchemaDetector
|
4
|
-
|
5
|
-
def for_hash(hash)
|
6
|
-
schema = {}
|
7
|
-
hash.each_with_object(schema) do |(k,v),schema|
|
8
|
-
schema[k] = type_of_value(v)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def for_array(array)
|
13
|
-
return {} if array.empty?
|
14
|
-
start_schema = for_hash(array.first)
|
15
|
-
schema = array.reduce(start_schema) do |schema, entry|
|
16
|
-
fit_schema entry, schema
|
17
|
-
end
|
18
|
-
schema.each do |key, type|
|
19
|
-
if type.nil?
|
20
|
-
# when all values were nil, set to Object
|
21
|
-
schema[key] = Object
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
def fit_schema(entry, schema=nil)
|
28
|
-
entry_schema = for_hash entry
|
29
|
-
return entry_schema unless schema
|
30
|
-
raise ArgumentError, 'different key entries not (yet) supported' if entry_schema.keys != schema.keys
|
31
|
-
|
32
|
-
schema.each do |key, type|
|
33
|
-
if type == nil && entry_schema[key] != nil
|
34
|
-
schema[key] = entry_schema[key] # first not-nil value
|
35
|
-
elsif type != nil && entry_schema[key] == nil
|
36
|
-
# just ignore subsequent nil-values
|
37
|
-
elsif type != Object && type != entry_schema[key]
|
38
|
-
schema[key] = Object # differing types, generalize to Object
|
39
|
-
end
|
40
|
-
end
|
41
|
-
schema
|
42
|
-
end
|
43
|
-
|
44
|
-
def type_of_value(value)
|
45
|
-
(value.nil?) ? nil : value.class
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Sepparator::SchemaDetector do
|
4
|
-
let(:hash) {
|
5
|
-
{
|
6
|
-
integer: 1,
|
7
|
-
float: 0.1,
|
8
|
-
string: 'foobar',
|
9
|
-
date: Date.today,
|
10
|
-
timestamp: DateTime.now
|
11
|
-
}
|
12
|
-
}
|
13
|
-
|
14
|
-
let(:expected_schema) {
|
15
|
-
Hash[hash.keys.zip(hash.values.map { |v| v.class })]
|
16
|
-
}
|
17
|
-
|
18
|
-
context '#for_hash' do
|
19
|
-
it 'detects a schema given a hash' do
|
20
|
-
schema = subject.for_hash(hash)
|
21
|
-
expect(schema).to eq(expected_schema)
|
22
|
-
end
|
23
|
-
it 'ignores nil values' do
|
24
|
-
schema = subject.for_hash(hash.merge({nilvalue: nil}))
|
25
|
-
expect(schema[:nilvalue]).to be_nil
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context '#for_array' do
|
30
|
-
it 'detects a schema given an array of hashes' do
|
31
|
-
input = 10.times.map { |_| hash }
|
32
|
-
schema = subject.for_array(input)
|
33
|
-
expect(schema).to eq(expected_schema)
|
34
|
-
end
|
35
|
-
it 'detects Object type when different types present' do
|
36
|
-
differing_type_hash = hash.merge({integer: 'not-an-int'})
|
37
|
-
input = 10.times.map { |_| hash } + [differing_type_hash]
|
38
|
-
schema = subject.for_array(input)
|
39
|
-
expect(schema[:integer]).to eq(Object)
|
40
|
-
end
|
41
|
-
it 'raises when different key entries are given' do
|
42
|
-
different_keys = hash.dup.merge({extra: 'extra-column'})
|
43
|
-
different_keys.delete(:integer) # remove one column
|
44
|
-
input = 10.times.map { |_| hash } + [different_keys]
|
45
|
-
expect {subject.for_array(input)}.to raise_error(ArgumentError, "different key entries not (yet) supported")
|
46
|
-
end
|
47
|
-
it 'ignores nil values' do
|
48
|
-
input = [hash.dup.merge({date: nil})] + 10.times.map { |_| hash } + [hash.dup.merge({integer: nil})]
|
49
|
-
schema = subject.for_array(input)
|
50
|
-
expect(schema).to eq(expected_schema)
|
51
|
-
end
|
52
|
-
it 'set type to Object when all values are nil for one key' do
|
53
|
-
input = [hash.merge({allnil: nil})]
|
54
|
-
schema = subject.for_array input
|
55
|
-
expect(schema[:allnil]).to eq(Object)
|
56
|
-
end
|
57
|
-
it 'returns empty hash when given empty array' do
|
58
|
-
expect(subject.for_array([])).to eq({})
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|